<?php
  

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


	// API parameter
    
	if(isset($_REQUEST['apikey']) && isset($_REQUEST['network']) && isset($_REQUEST['MobileNumber']) && isset($_REQUEST['DataPlan']) && isset($_REQUEST['ref']) ){
	
	$apikey = $_REQUEST['apikey'];
	$serviceID = $_REQUEST['network'];
	$MobileNumber = $_REQUEST['MobileNumber'];
	$Dataplan = $_REQUEST['DataPlan'];
	$ref = $_REQUEST['ref'];
	$network = $_REQUEST['network'];
	$auth = "paid";
	
	
	$apikey = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $apikey);
	$serviceID = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $serviceID);
	$MobileNumber  = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $MobileNumber);
	$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);
	
	
    $servName = $_SERVER['HTTP_HOST'];
	


	$charge = '0';
	$channel = "API";
	$view = "View";
	date_default_timezone_set('Africa/Lagos');
    $dateTime = date('Y-m-d h:i A');
	
	$action = substr(str_shuffle("0123456789678901"), 0, 8);
	$del = substr(str_shuffle("0123456789678901"), 0, 7);
	
	// 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 sme_data");
	$rate = mysqli_fetch_array($query_com);

	$mtnA = $rate['mtncA'];
	$mtnB = $rate['mtncB'];
	$mtnC = $rate['mtncC'];
	$mtnD = $rate['mtncD'];
	$mtnE = $rate['mtncE'];
	$mtnF = $rate['mtncF'];
	
	
	
		//mtn Data
		if($serviceID == '05' && $Dataplan == '500'  ){
		
		$amount = $mtnA;
		
		$valu = "MTN 500MB";
		$mobplan = "8";
		
		
			
			}elseif($serviceID == '05' && $Dataplan == '1000' ){
				
    		$amount = $mtnB;
    		
    		$valu = "MTN 1GB";
			$mobplan = "9";
			
			
			}elseif($serviceID == '05' && $Dataplan == '2000' ){
				
    		$amount = $mtnC;
    		
    		$valu = "MTN 2GB";
			$mobplan = "10";
			
				}elseif($serviceID == '05' && $Dataplan == '3000' ){
					
				$amount = $mtnD;
				
				$valu = "MTN 3B";
				$mobplan = "11";
				
					}elseif($serviceID == '05' && $Dataplan == '5000' ){
						
						$amount = $mtnE;
						
						$valu = "MTN 5GB";
						$mobplan = "12";
						
			}elseif($serviceID == '05' && $Dataplan == '10000'){	
										
			        $amount = $mtnF;
			        
			        $valu = "MTN 10GB"; 
					$mobplan = "13";
								
							}else{
							
							$mobplan = "Nil";
							$amount = $Dataplan;	
								
							
								}	
								
								
		  if($network === '01'){
	$gladNet = "1";
	
	}elseif($network === '02'){
		
		$gladNet = "3";
		}elseif($network === '03'){
		
		$gladNet = "4";	
			
			}elseif($network === '04'){
				
				$gladNet = "2";

            }elseif($network === '05'){
				
				$gladNet = "1";
				$service = "MTN Corporate DATA";
				}else{
				$gladNet = "Nil";	 }						
	     
	
		$comi = ($per/100)*$amount;
		
	$debit = $amount;
	
	$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 = '$ref' ") 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/corporate-data?apikey=$key&service=$network&MobileNumber=$MobileNumber&DataPlan=$Dataplan&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);

 $resdata = curl_exec($ch);
$result = json_decode($resdata);

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

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


if($result->code =='101'){
    
    $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,phone,amount,debit,ref,status,email,customer,balanceB,balanceA,del,date) VALUES('$service ($valu)','$channel','$MobileNumber','$amount','$debit','$ref','$stat','$email','$fname $lname','$blc','$balA','$del','$dateTime')") or die(mysqli_error());



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



}else{
	$stat = "Failed";
response(204,array("status"=>$stat,"message"=>$mgs,"ref"=>$ref,"Product"=>$service,"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;
}
    
    ?>
    
  
   	

