<?php
require("database.php");

if(isset($_GET['MerchantTransactionID'])){ 

$transid = $_GET['MerchantTransactionID'];
$merchant_id = "ACC-OPMHT000000190";

$curl = curl_init();
        $data = "MerchantRegID=".$merchant_id."&MerchantTransID=".$transid;
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://www.onepay.com.ng/api/ValidateTrans/getTrans.php",
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_POSTREDIR => 3,
          CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS =>$data, 
          CURLOPT_CUSTOMREQUEST => "POST"
        ));

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

        curl_close($curl);

        if ($err) 
        {
          echo "cURL Error #:" . $err;
        } else {
         //var_dump($response);
         
         $mydata = json_decode($response, true);
         extract($mydata); 
          
                 
         ////echo "<br>Response Code: ".$response_code;
        //// echo "<br>Status: ".$status;
         
         //print_r($mydata);
         
         $myresponse_code = $mydata["data"]["response_code"];
         $myresponse_message = $mydata["data"]["response_message"];
         
       
       //  die();
         
          if(($myresponse_code == "00") || ($myresponse_code == "0"))
          {
            echo "<br>Successful...<br> You will be redirected to the application portal proceed with your application";
            //update my transaction table
		$myTransID = substr($_GET['MerchantTransactionID'],9);
		
            //get details
            	$qry = mysqli_query ($con, "SELECT * FROM examTransactions WHERE transID='$myTransID'");
   		while ($row = mysqli_fetch_array($qry)){
   		  $memberNo = $row["memberNo"];
   		  $successurl= $row["successurl"];
   		  $regSet= $row["regSet"];
   		  $examCentre= $row["examCentre"];
   		  $amount= $row["amount"];
   		  
   		  //echo $memberNo;
   		   $successurl;
   		   $successurl = $successurl."?memberNumber=".$memberNo."&regSet=".$regSet."&examCentre=".$examCentre."&amount=".$amount;
   		}
            
            //updating transaction record
            mysqli_query($con, "update examTransactions set status='Paid' where transID='$myTransID'");
            
            echo '<script>window.location = "'.$successurl.'";</script>';
            
          }else{
            //send to failed page
            echo '<script>alert("This payment was not successful. \nYou will be redirected.");window.location = "http://niqs.org.ng/onlineapplication/admin";</script>';
            exit();
            
          }
          
          
        }
        
        
        
        
	
	
} else {
   //invalid token
   echo "Invalid Token";
}

?>