<?php
include_once("../db.php");

?>

<?php

  if($json = file_get_contents("php://input")) {

file_put_contents('getpaylogs.txt', $json);

}

?>






<?php 



	

$response = file_get_contents("getpaylogs.txt");

$res = json_decode($response);

 

    
    
  
         $accountRef =  $res->customer->accountReference;
         $email = $res->customer->customerEmail;
	 $phone = $res->customer->customerPhone;
	 $fname = $res->customer->firstName;
	 $lname = $res->customer->lastName;
    
    $amount = $res->paymentDetails->amountPaid;
	$charge = $res->paymentDetails->charge;
	$payable = $res->paymentDetails->payable;
	$stampDuty = $res->paymentDetails->stampDuty;
        $bankcode = $res->paymentDetails->bankCode;
        $bankName = $res->paymentDetails->bankName;
	
	
    
	$payref = $res->data->paymentReference;
    $ref = $res->data->referenceID;
	$status = $res->data->status;
	$message = $res->data->message;
	$service = $res->data->service;
        $method = $res->data->chargeMethod;
	$date = $res->data->dateTime;
    
    $payment_status =  $res->paymentSource->paymentStatus;
	$description = "GETPAY Auto Wallet Funding";
    
    
	
	$eventRef = $res->eventData->accountReference;
	$eventStatus = $res->eventData->status;
	$eventAction = $res->eventData->action;
	$eventEmail = $res->eventData->customerEmail;
	$eventFname = $res->eventData->firstName;
	$eventLname = $res->eventData->lastName;
	$eventPhone = $res->eventData->customerPhone;
	
	
	$eventBankF = $res->eventData->bankFile;
	$eventAcctF = $res->eventData->accountFile;
	$eventNameF = $res->eventData->namefile;
	
	$eventBank = $res->eventData->bankName;
	$eventAcct = $res->eventData->accountNumber;
	$eventName = $res->eventData->accountName;
	$eventHost = $res->eventData->Host;
	

	
 
  $qryApi = mysqli_query($conn,"SELECT * FROM payment");
    $apidata = mysqli_fetch_array($qryApi);


	
	 $getpaykey = $apidata['getpaykey']; 
    $getpaysec = $apidata['getpaysec'];
     $getpayurl = $apidata['getpayurl'];
    
	
    $per = $apidata['comi'];
    $charges = $apidata['charge'];


	
      $headers = getallheaders();
	  $Xsignature = $headers['Authorization'];
	  
	  
	  $passcode = "$getpaysec:$payref";
	  $authkey = hash('sha512', $passcode);
          $authkey = strtoupper($authkey);
	


			
       if($Xsignature === $authkey){
	  
	
      date_default_timezone_set('Africa/Lagos');
          $dateTime = date(' Y-m-d h:i A');
          $date = date(' Y-m-d');
          $action = substr(str_shuffle("0123456789678901"), 0, 8);
  

    $query_rec = mysqli_query($conn,"SELECT * FROM settings");			
    $settings = mysqli_fetch_array($query_rec);
	$sitename = $settings['sitename'];
	

  $mnfy_user = "SELECT  * FROM users WHERE email = '$email'  ";
  
 $mnfy_user_result = mysqli_query($conn, $mnfy_user);

 $mnfy_count = mysqli_num_rows($mnfy_user_result);
 
 if ($mnfy_count > 0) {



while ($row = mysqli_fetch_assoc($mnfy_user_result)) {
          
     $mnfy_username = $row['email'];
      $email = $row['email'];
      $user_wallet   = $row['bal'];
      $bal  = $row['bal'];	  
      $fname   = $row['firstname']; 
      $lname   = $row['lastname']; 


	
}  


  $mnfy_tranx_ref = "SELECT  * FROM deposit WHERE ref = '$ref' ";

 $mnfy_trans_ref_result = mysqli_query($conn, $mnfy_tranx_ref);

 $mnfy_trans_ref_count = mysqli_num_rows( $mnfy_trans_ref_result);



 
 if ($mnfy_trans_ref_count < 1) {
     


	
	
	//verify transaction
	
	
 $host = "https://$getpayurl/api/v1/merchant-verify/transactions/?secretkey=$getpaysec&referenceID=$ref";


//Initialize cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);

//Close the cURL handle.
curl_close($ch);

$result = json_decode($response);

if ($result->transaction->status == "Completed" ) {
    
  $amount_paid = $result->transaction->amount;
  $payable = $result->transaction->payable;
  $charge = $result->transaction->charge;
  $status = $result->transaction->status;
  $stampDuty = $result->transaction->stampDuty;
  $vat = $result->transaction->VAT;
  $paidon = $result->transaction->date;


$comi = ($per/100)*$amount_paid;

   if($method ==  "Charge"){
	   
	$sitecharge = $charges; 

   }elseif($method ==  "Percentage"){	
	   
	 $sitecharge = $comi; 
	 
	   
   }else{
	   
	 $sitecharge = $charges;   
   }



     $paid = $amount_paid - $sitecharge - $stampDuty;

     $fund = $user_wallet + $amount_paid - $sitecharge - $stampDuty;


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



$qsel = "INSERT INTO deposit (ref,amount,charge,user,email,status,token,method,process,service,balanceB,balanceA,date) VALUES ('$ref','$paid','$sitecharge','$fname $lname','$email','$status','$accountRef','GETPAY','$description','Auto Wallet Funding','$bal','$fund','$dateTime')";
							$sav = $conn->query($qsel);






// Send Email Alert  

$from = "$sitename<$siteemail>"; //the email address from which this is sent
$to = "$email"; //the email address you're sending the message to
$subject = "Wallet Funding"; //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: SITE_NAME<SITE_EMAIL>\r\n";
$headers .= "Organization: SITE_NAME\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = "

<html>
<head>
<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;
}


.button {
  background-color: #008CBA; /* Blue */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  border-radius: 8px;
  cursor: pointer;
  
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
}

.button:hover {
  background-color: #4CAF50; /* Green */
  color: white;
}

</style>
</head>

<body>






<h3>Dear $fname $lname,</h3>

Wallet Credit Transaction has occur on your $sitename account.  <br>

<strong>Your transaction details are as follows:</strong> <p>

<table class='table' >
  
  <tr>
    <td>Amount Paid</td>
    <td>N$amount_paid</td>
  </tr>

  <tr>
    <td>Stamp Duty</td>
    <td>N$stampDuty</td>
  </tr>
  
<tr>
    <td>Amount Charged</td>
    <td>N$sitecharge</td>
  </tr>
  
  <tr>
    <td>Amount Credited</td>
    <td>N$paid</td>
  </tr>

<tr>
    <td>Balance Before</td>
    <td> N$user_wallet</td>
  </tr>

<tr>
    <td>New Balance</td>
    <td> N$fund</td>
  </tr>

  <tr>
    <td>Transaction ID</td>
    <td> $ref</td>
  </tr>

<tr>
    <td>Transaction Type</td>
    <td>GETPAY Auto Wallet Funding</td>
  </tr>

<tr>
    <td>Transaction Status</td>
    <td> $status</td>
  </tr>
  
   <tr>
    <td>Date</td>
    <td>$paidon</td>
  </tr>
  
   
  
</table> <p>




<p>
Our support team is standby to assist you wherever you need help.<p>
Thank you for choosing www.$siturl.
 

<p>Warm Regards, <p>



<p>



</body><html>";

//now mail
$DoMail = mail($to,$subject,$message,$headers);

 
   
}else{
    echo "Transaction Failed";
} 
	
 
     
 }else{
     echo "Reference Already exist. Account Already Funded";
 }
 

     
 }else{
     echo "User Not found";
 }



}else{
//Invalid XAuth details   

if($eventAction == "Add" || $eventAction == "Update" ){

 
   $doupt = mysqli_query($conn, "UPDATE reserved_accounts SET $eventBankF='$eventBank',$eventAcctF='$eventAcct',$eventNameF='$eventName' WHERE accountref='$eventRef' ") or die(mysqli_error());
   
   echo "Reserve Account Updated Successfully.";
   
}elseif($eventAction == "Delete" ){
	
	 $doupt = mysqli_query($conn, "UPDATE reserved_accounts SET $eventBankF='',$eventAcctF='',$eventNameF='' WHERE accountref='$eventRef' ") or die(mysqli_error());
	 
	 echo "Reserve Account Deleted Successfully.";
	 
	 }elseif($eventAction == "Create" ){
	
	  	$monniify_info = "INSERT INTO reserved_accounts (accountref,$eventBankF,$eventAcctF,$eventNameF,firstname,lastname,email,phone,status,date,action) VALUES ('$eventRef','$eventBank','$eventAcct','$eventName','$eventFname','$eventLname','$eventEmail','$eventPhone','Active','$dateTime','$action' )";
							
   $monnify_info_result = mysqli_query($conn, $monniify_info);
   
   echo "New Reserve Account Created Successfully.";
   
   }elseif($eventAction == "HostURL" || $eventAction == "Host"){
	
   $doupt = mysqli_query($conn, "UPDATE payment SET getpayurl='$eventHost' ") or die(mysqli_error());
   
   echo "New HostURL Updated Successfully.";
	 
}else{

echo "Invalid Parameters";
	
}


}	







   


?>
