<?php
  

 header('Content-Type : application/json');
  
    require_once('../../db.php');


	// API parameter
    
	if(isset($_REQUEST['apikey']) && isset($_REQUEST['service']) && isset($_REQUEST['product_code']) && isset($_REQUEST['quantity']) && isset($_REQUEST['ref']) ){
	
	$apikey = $_REQUEST['apikey'];
	$serviceID = $_REQUEST['service'];
	$quantity = $_REQUEST['quantity'];
	$Dataplan = $_REQUEST['product_code'];
	$ref = $_REQUEST['ref'];
	$network = $_REQUEST['service'];
	$name = $_REQUEST['name'];
	$auth = "paid";
	
	
	$apikey = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $apikey);
	$serviceID = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $serviceID);
	$quantity  = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $quantity);
	$Dataplan = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $Dataplan);
	$ref = str_replace( array( '/', '(', ')',   'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $ref);
	$network = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $network);
	$name = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $name);
	  
	  if($name == "") {
	  $serName = $_SERVER['SERVER_NAME'];
	  }else{
	  $serName = $name; }	  
	
     
	
    $action = substr(str_shuffle("0123456789678901"), 0, 8);
	$del = substr(str_shuffle("0123456789678901"), 0, 7);
	$channel = "API";
	$view = "View";
	date_default_timezone_set('Africa/Lagos');
    $dateTime = date('Y-m-d h:i A');
	
	// check if the account is valid
	

	
	if($param){
	    
	    response(107,"BAD REQUEST");} else{
	
	$retr = "SELECT * FROM users WHERE apikey='$apikey' ";
	
	$exe = mysqli_query($conn,$retr);
	$rob = mysqli_fetch_array($exe);
	
	$user = $rob['apikey'];
	$aut = $rob['level'];
	
	$arr = array("$apikey","$auth");
	
	$pair = array("$user","$aut");		
	
	if($arr === $pair){
	    
	    // check if the user have balance
	
		$gb = mysqli_query($conn,"SElECT * FROM users WHERE apikey = '$user' ") or die(mysqli_error());	
	$reco = mysqli_fetch_array($gb);
	
	$blc = $reco['bal'];
	$level = $reco['level'];
	$email = $reco['email'];
	$fname = $reco['firstname'];
	$lname = $reco['lastname'];
	
	$query_com = mysqli_query($conn,"SELECT * FROM exam_billing");
	$rate = mysqli_fetch_array($query_com);

	$waec = $rate['waec'];
	$waecGCE = $rate['waecGCE'];
	$nysc = $rate['nysc'];
	
	
	$neco = $rate['neco'];
	$necoGCE = $rate['necoGCE'];
	$airtelC = $rate['airteldC'];

	
	$nabteb = $rate['nabteb'];
	$nabtebGCE = $rate['nabtebGCE'];
	$gloC = $rate['glodC'];

	
	
	$JAMB = $rate['jambutme'];
	$JAMBDE = $rate['jambde'];
	$etisalatC = $rate['etisalatdC'];

		
		//mtn Data
		if($serviceID == 'waec' && $Dataplan == '1'  ){
		
		$amount = $waec;
		
		$valu = "MTN 500MB";
		$mobplan = "1";
		$service = "WAEC Result Checking PIN";
		
		
			
			}elseif($serviceID == 'waec' && $Dataplan == '2' ){
				
    		$amount = $waecGCE;
    		
    		$valu = "MTN 1GB";
			$mobplan = "1";
		    $service = "WAEC GCE Registration PIN";
			
			
			}elseif($serviceID == 'waec' && $Dataplan == '3' ){
				
    		$amount = $nysc;
    		
    		$valu = "MTN 1.5GB";
			$mobplan = "1";
		    $service = "WAEC Verification PIN";
						
						
						
					// NECO Result Checking Token	
						
	 }elseif($serviceID == 'neco' && $Dataplan == '1'  ){
		
		$amount = $neco;
		
		$valu = "Airtel 500MB";
		$mobplan = "2";
		$service = "NECO Result Checking Token";
		
			
			}elseif($serviceID == 'neco' && $Dataplan == '2' ){
				
    		$amount = $necoGCE;
    		
    		$valu = "Airtel 1GB";
			$mobplan = "2";
		    $service = "NECO GCE Registration PIN";
			
			}elseif($serviceID == 'neco' && $Dataplan == '3' ){
				
    		$amount = $airtelC;
    		
    		$valu = "Airtel 2GB";
			$mobplan = "42";
		    $service = "None";
			
							
						//NABTEB Result Checking PIN
							
			}elseif($serviceID == 'nabteb' && $Dataplan == '1'  ){
		
		$amount = $nabteb;
		
		$valu = "Glo 500MB";
		$mobplan = "3";
		$service = "NABTEB Result Checking PIN";
		
			
			}elseif($serviceID == 'nabteb' && $Dataplan == '2' ){
				
    		$amount = $nabtebGCE;
    		
    		$valu = "Glo 1GB";
			$mobplan = "3";
		    $service = "NABTEB GCE Registration PIN";
			
			}elseif($serviceID == 'nabteb' && $Dataplan == '3' ){
				
    		$amount = $gloC;
    		
    		$valu = "Glo 2GB";
			$mobplan = "82";
		    $service = "None";
			
							
							
							//JAMB UTME
							
							}elseif($serviceID == 'jamb' && $Dataplan == '1'  ){
		
		$amount = $JAMB;
		
		$valu = "9Mobile 500MB";
		$mobplan = "9";
		$service = "JAMB UTME Registration PIN";
		
			
			}elseif($serviceID == 'jamb' && $Dataplan == '2' ){
				
    		$amount = $JAMBDE;
    		
    		$valu = "9Mobile 1GB";
			$mobplan = "10";
		    $service = "JAMB Direct Entry Registration PIN";
			
			}elseif($serviceID == 'jamb' && $Dataplan == '3' ){
				
    		$amount = $etisalatC;
    		
    		$valu = "9Mobile 2GB";
			$mobplan = "90";
		    $service = "None";
			
								
							}else{
							
							$mobplan = "Nil";
							$amount = 10000000000000000;	
							$gladNet = "1";
		                    $service = "Unknown";	
							
								}	
								
								
	$amount = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
				
				
				
		
				
				
	$charge = 0;	
	     
	
		$comi = ($per/100)*$amount;
		
	$debit = $amount + $charge;
	
	$debit = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $debit);
	
	if($debit < $blc ){ 
	    
	    // check if ref number exist
	    
	  $req = mysqli_query($conn,"SElECT * FROM transactions WHERE ref = '$requestid' ") or die(mysqli_error());	
	$nu = mysqli_num_rows($req);

	if($nu == 0){
	    
       // debit account
	 // $doupt = mysqli_query($conn,"UPDATE users set bal=$blc-$debit WHERE email='$email' ") or die(mysqli_error());

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

$key = $apidata['APIkey'];		    

$host = "https://vtuafrica.com.ng/portal/api/exam-pin/?apikey=$key&service=$network&product_code=$Dataplan&quantity=$quantity&ref=$ref";


//Initialize cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 $vdata = curl_exec($ch);
$result = json_decode($vdata, true);
$result1 = json_decode($vdata);
//Close the cURL handle.
curl_close($ch);


$pin =  $result->description->pin;
$mgs = $result->description->message;


if($result->code =='101'){
 


  $message = "TRANSACTION SUCCESSFUL";
    
    $stat = "Completed";

$balA = $blc - $debit;

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


$add = mysqli_query($conn,"INSERT INTO transactions (serviceid,channel,vcode,amount,debit,charge,ref,status,email,customer,balanceB,balanceA,del,date) VALUES('$service','$channel','$Dataplan','$amount','$debit','$charge','$ref','$stat','$email','$fname $lname','$blc','$balA','$del','$dateTime')") or die(mysqli_error());



$delimiter = ' ';
$words = explode($delimiter, $pin);
 
foreach ($words as $show) {
	
 $sto = "INSERT INTO mypin (net,pins,email,ref,amount)VALUES('$serviceID','$show','$email','$ref','$debit')";
	    $prox = mysqli_query($conn,$sto);

}


 

response(100,array("status"=>$stat,"Product"=>$service,"pin"=>$pin,"Amount_Charged"=>$debit,"Available_Balance"=>$balA,"Transaction_Ref"=>$ref,"transaction_date"=>$dateTime,"message"=>$message));



}else{
	$stat = "Failed";
response(204,array("status"=>$stat,"message"=>$mgs,"ref"=>$ref,"amount"=>$amount,"Amount_Charged"=>'0',"Available_Balance"=>$blc));

}

}else{ response(104,"TRANSACTION ID ALREADY EXIST");
    
    
}
}
// echo low balance
else{ 


    response(102,"LOW BALANCE"); 


    
} 

// close account not found
} else{  
    
    response(103,"INVALID ACCOUNT"); 


    }


} // close wrong parameter



	}   else{ 
	   
	   response(400,"INVALID PARAMETER"); 
	       
	       
	   }
	    

function response($status,$status_message)
{
	
	
	$response['code']=$status;
	$response['description']=$status_message;
	
	
	$json_response = json_encode($response);
	echo $json_response;
}
    
    ?>
    
  
   	

