<?php
session_start();
require('db.php');
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: index.php");
  exit;
		}
		
	include('inc/func.php');							
	$user = $_SESSION['user'];
								

		





if(isset($_GET['reference']) ){
$reference = $_GET['reference'];
$reference = filter_var($reference, FILTER_SANITIZE_STRING);
$reference = mysqli_real_escape_string ($conn, $reference);
$ref = $reference;

    
$check=mysqli_query($conn,"SELECT * FROM deposit WHERE ref='$ref' ");
$num = mysqli_num_rows($check);


$query_rec = mysqli_query($conn,"SELECT * FROM payment");
$apib = mysqli_fetch_array($query_rec);
			
$paykey = $apib['paystackSecret'];
$paykey = filter_var($paykey, FILTER_SANITIZE_STRING);
$paykey= mysqli_real_escape_string ($conn, $paykey);


   $query_rec = mysqli_query($conn,"SELECT * FROM settings");			
        $settings = mysqli_fetch_array($query_rec);
	$sitename = $settings['sitename'];
	$servName = $_SERVER['HTTP_HOST'];
    
    
    if ($num == 1){
	
		$ret = mysqli_query($conn,"SELECT * FROM deposit WHERE ref='$ref' ");
		$da = mysqli_fetch_array($ret);
		
		$amount = $da['amount'];
		$tid = $da['ref'];
		$token = $da['token'];
		$refer = $da['refer'];
		$status = $da['status'];
		$email = $da['email'];
								
	$ins = mysqli_query($conn,"SELECT * FROM users WHERE email='$email' ");
	$data = mysqli_fetch_array($ins);
								
      $email = $data['email'];
	   $fname = $data['firstname'];
	  $bal  = $data['bal'];
      $fname   = $data['firstname']; 
      $lname   = $data['lastname'];
	
	
	
    if($token === $refer && $status !== 'Completed'){
	
	
	
 // confirm  payment    
$curl = curl_init();	
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "accept: application/json",
    "authorization: Bearer $paykey",
    "cache-control: no-cache"
  ],
));

$response = curl_exec($curl);
$err = curl_error($curl);

$tranx = json_decode($response);

//echo $tranx->data->status;

if($tranx->data->status === 'success'){
  
$status = "Completed";
$method = "Paystack";
  
  
    // amount to credit minus fees

//$paidamt =  substr($tranx->data->amount,0,-2);
//$charge = substr($tranx->data->fees,0,-2);

$paidamt =  $tranx->data->amount;
$charge = $tranx->data->fees;

$paidamt = $paidamt/100;
$charge = $charge/100;

$payamt = $paidamt-$charge;

$amtshow = number_format($payamt,2,'.',',');
$fund = $bal + $payamt;
  

//$sitecharge = ($per/100)*$amount;
   
   


	$doupt = mysqli_query($conn,"UPDATE users SET bal='$fund' WHERE email = '$email' ") or die(mysqli_error());
               
                

                
 $up = "UPDATE deposit SET token='$ref',refer='N/A',status='$status',charge='$charge',method='$method',balanceA='$fund' WHERE ref='$tid' ";
			$conn->query($up);
				
		
				// send email....
				
				
$from = " ".$sitename."<support@$servName>"; //the email address from which this is sent
$to = "$email"; //the email address you're sending the message to
$subject = "Transaction Notification"; //the subject of the message

// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: ".$settings['sitename']."<support@$serName>\r\n";
$headers .= "Organization: ".$settings['sitename']."\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = "

<html>
<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 50%;
}

td {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>

<body>

<img src='https://".$_SERVER['SERVER_NAME']."/portal/assets/images/logo.png'>

<h3>Dear $fname,</h3>

Your account has been credited <br> 

<h2 style='color:#06AF3E'>N$paid</h2> <p>

<strong>Transaction Summary</strong> <p>

<table class='table' >
  
  <tr>
    <td>Description</td>
    <td>Wallet Credit</td>
  </tr>
  <tr>
    <td>Transaction Date</td>
    <td> $dateTime </td>
  </tr>
  
  <tr>
    <td>Transaction Ref</td>
    <td> $ref </td>
  </tr>
  
   <tr>
    <td>Payment Method</td>
    <td> $method</td>
  </tr>
  
  <tr>
    <td>Card Fee</td>
    <td> $charge</td>
  </tr>
  
   <tr>
    <td>Transaction Status</td>
    <td> $status </td>
  </tr>
  
</table> <p>


<a href='https://$serName'>Click here</a> to login and confirm <p>

If you have questions/complaints as regards this transaction, please reply 

the email support@$serName or <strong>Call:</strong> $sitephone
<p>


Thanks for your Patronage. <p>

<strong>$serName</strong>

</body><html>";

//now mail
$DoMail = mail($to,$subject,$message,$headers);
             
               
 ?>
<script>window.location="buy_credit.php?ref='<?php echo $_GET['reference']?>' ";</script> <?php 
    

                }else{
                     echo "Payment failed";
                     
                      ?>
<script>window.location="buy_credit.php ";</script> <?php
                }  
				
	}else{
	     echo "Already Funded";
	     
	      ?>
<script>window.location="buy_credit.php?funded='<?php echo $_GET['reference']?>'";</script> <?php 
	} 

           }else{
echo "Transaction does not exit.";
                     
 ?>
<script>window.location="buy_credit.php ";</script> <?php
}	

}else{
    
    echo "Invalid Parameters Supplied";
    
 ?>
<script>window.location="buy_credit.php ";</script> <?php

}




         

?>