<?php
  

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


	// API parameter
    
	if(isset($_REQUEST['apikey']) && isset($_REQUEST['site']) && isset($_REQUEST['network']) && isset($_REQUEST['sender']) && isset($_REQUEST['sendernumber']) && isset($_REQUEST['amount']) && isset($_REQUEST['ref'])){
	
	$apikey = $_REQUEST['apikey'];
	$site = $_REQUEST['site'];
	$network = $_REQUEST['network'];
	$sender = $_REQUEST['sender'];
	$phone = $_REQUEST['sendernumber'];
	$amount = $_REQUEST['amount'];
	$ref = $_REQUEST['ref'];	
	$sitephone = $_REQUEST['sitephone'];
	
	$auth = "paid";
	
	
	$apikey = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $apikey);
	$network = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $network);
	$phone  = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $phone );
	$amount = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
	$ref = str_replace( array( '/', '(', ')',   'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $ref);
     
	$sender  = str_replace( array( '+', '/', '(', ')',    'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $sender);
	$sitephone = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $sitephone);

	$limit = "99";

	
	
	

	$serviceID = 'Airtime';
	$channel = "API";
	$action = substr(str_shuffle("0123456789678901"), 0, 8);
	$del = substr(str_shuffle("0123456789678901"), 0, 7);
        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 = mysqli_query($conn,"SELECT * FROM users WHERE apikey='$apikey' ");

	$rob = mysqli_fetch_array($retr);
	
	
	$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'];
	
	
	$qrypinBil = mysqli_query($kamm,"SELECT * FROM acash_billing");
 $pin = mysqli_fetch_array($qrypinBil);


$mtn = $pin['mtn'];
$airtel = $pin['airtel'];
$glo = $pin['glo'];
$etisalat = $pin['9mobile'];
				

if($network === 'mtn'){
$pnt = "MTN Airtime2Cash";
$per = $mtn;

}elseif($network === 'etisalat'){
$pnt = "9mobile Airtime2Cash";
$per = $etisalat;

}elseif($network === 'airtel'){
$pnt = "Airtel Airtime2Cash";
$per = $airtel;

}elseif($network === 'glo'){
$pnt = "Glo Airtime2Cash";
$per = $glo;

}else{
$pnt = "Invalid Network";
$per = $etisalat;
}		
						
	
	$charge = ($per/100)*$amount;
	$credit = $amount-$charge;
						
	$amountpaid = $credit;
	
	
	if($amount > $limit ){ 
	    
		
			    // check if ref number exist
	    
	  $req = mysqli_query($conn,"SElECT * FROM transactions WHERE ref = '$ref' ") or die(mysqli_error());	
	
	$nu = mysqli_num_rows($req);

	if($nu == 0){
	    

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

$key = $apidata['APIkey']; 


$host = "https://vtuafrica.com.ng/portal/api/airtime-cash/?apikey=$key&site=$request_dir&network=$network&sender=$sender&sendernumber=$phone&amount=$amount&sitephone=$sitephone&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);



//Close the cURL handle.

curl_close($ch);

$status = $result->description->status;

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

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

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

$balA = $blc - $debit;

 $message = "TRANSACTION SUCCESSFUL";


 $add = mysqli_query($conn,"INSERT INTO transactions (serviceid,channel,phone,meterno,amount,charge,ref,status,email,customer,sender,balanceB,balanceA,del) VALUES ('$pnt','$channel','$phone','$sitephone','$amount','$charge','$ref','$status','$email','$fname $lname','$sender','$blc','$blc','$del')") or die(mysqli_error());
 

response(100,array("status"=>$status,"service"=>$pnt,"message"=>$message,"ref"=>$ref,"amount"=>$amount,"Amount_Charged"=>$charge,"Available_Balance"=>$blc,"transaction_date"=>$dateTime));


}else{ 

$stat = "Failed";

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


}else{ 

response(104,"TRANSACTION ID ALREADY EXIST");
    
    
}

 
 }else{ 

response(404,"INVALID AMOUNT. YOU CAN ONLY CONVERT FROM N100 AND ABOVE.");
    
    
}

// 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;
}
    
    ?>
    
  
   	

