<?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'];
	

	
	$action = substr(str_shuffle("0123456789678901"), 0, 8);
	$del = substr(str_shuffle("0123456789678901"), 0, 7);
	

	$charge = '0';
	$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 sme_data");
	$rate = mysqli_fetch_array($query_com);

	$mtnA = $rate['mtnA'];
	$mtnB = $rate['mtnB'];
	$mtnC = $rate['mtnC'];
	$mtnD = $rate['mtnD'];
	$mtnE = $rate['mtnE'];
	$mtnF = $rate['mtnF'];
	
	
	$airtelA = $rate['airtelA'];
	$airtelB = $rate['airtelB'];
	$airtelC = $rate['airtelC'];
	$airtelD = $rate['airtelD'];
	$airtelE = $rate['airtelE'];
	$airtelF = $rate['airtelF'];
	
	$gloA = $rate['gloA'];
	$gloB = $rate['gloB'];
	$gloC = $rate['gloC'];
	$gloD = $rate['gloD'];
	$gloE = $rate['gloE'];
	$gloF = $rate['gloF'];
	$gloG = $rate['gloG'];
	$gloH = $rate['gloH'];
	$gloI = $rate['gloI'];
	
	
	$etisalatA = $rate['etisalatA'];
	$etisalatB = $rate['etisalatB'];
	$etisalatC = $rate['etisalatC'];
	$etisalatD = $rate['etisalatD'];
	$etisalatE = $rate['etisalatE'];
	$etisalatF = $rate['etisalatF'];
	$etisalatG = $rate['etisalatG'];
	$etisalatH = $rate['etisalatH'];
	$etisalatI = $rate['etisalatI'];
	$etisalatJ = $rate['etisalatJ'];
		
		//mtn Data
		if($serviceID == '01' && $Dataplan == '500'  ){
		
		$amount = $mtnA;
		
		$valu = "MTN 500MB";
		$mobplan = "1";
		
		
			
			}elseif($serviceID == '01' && $Dataplan == '1000' ){
				
    		$amount = $mtnB;
    		
    		$valu = "MTN 1GB";
			$mobplan = "2";
			
			
			}elseif($serviceID == '01' && $Dataplan == '2000' ){
				
    		$amount = $mtnC;
    		
    		$valu = "MTN 2GB";
			$mobplan = "3";
			
				}elseif($serviceID == '01' && $Dataplan == '3000' ){
					
				$amount = $mtnD;
				
				$valu = "MTN 3B";
				$mobplan = "4";
				
					}elseif($serviceID == '01' && $Dataplan == '5000' ){
						
						$amount = $mtnE;
						
						$valu = "MTN 5GB";
						$mobplan = "5";
						
			}elseif($serviceID == '01' && $Dataplan == '10000'){	
										
			        $amount = $mtnF;
			        
			       $valu = "MTN 10GB"; 
					$mobplan = "6";
						
						
						
					// airtel Data	
						
	 }elseif($serviceID == '04' && $Dataplan == '500'  ){
		
		$amount = $airtelA;
		
		$valu = "Airtel 500MB";
		$mobplan = "40";
		
		
			
			}elseif($serviceID == '04' && $Dataplan == '1000' ){
				
    		$amount = $airtelB;
    		
    		$valu = "Airtel 1GB";
			$mobplan = "41";
			
			
			}elseif($serviceID == '04' && $Dataplan == '2000' ){
				
    		$amount = $airtelC;
    		
    		$valu = "Airtel 2GB";
			$mobplan = "42";
			
				}elseif($serviceID == '04' && $Dataplan == '3000' ){
					
				$amount = $airtelD;
				
				$valu = "Airtel 3B";
				$mobplan = "Nil";
				
					}elseif($serviceID == '04' && $Dataplan == '5000' ){
						
						$amount = $airtelE;
						
						$valu = "Airtel 5GB";
						$mobplan = "43";
						
			}elseif($serviceID == '04' && $Dataplan == '10000'){	
										
			        $amount = $airtelF;
			        
			$valu = "Airtel 10GB"; 
			$mobplan = "44";
							
						//glo data	
							
			}elseif($serviceID == '02' && $Dataplan == '500'  ){
		
		$amount = $gloB;
		
		$valu = "Glo 500MB";
		$mobplan = "80";
		
		
			
			}elseif($serviceID == '02' && $Dataplan == '1000' ){
				
    		$amount = $gloC;
    		
    		$valu = "Glo 1GB";
			$mobplan = "81";
			
			
			}elseif($serviceID == '02' && $Dataplan == '2000' ){
				
    		$amount = $gloD;
    		
    		$valu = "Glo 2GB";
			$mobplan = "82";
			
				}elseif($serviceID == '02' && $Dataplan == '3000' ){
					
				$amount = $gloE;
				
				$valu = "Glo 3GB";
				$mobplan = "83";
				
					}elseif($serviceID == '02' && $Dataplan == '5000' ){
						
						$amount = $gloG;
						
						$valu = "Glo 5GB";
						$mobplan = "84";
						
			}elseif($serviceID == '02' && $Dataplan == '10000'){	
										
			        $amount = $gloH;
			        
			       $valu = "Glo 10GB"; 
					$mobplan = "85";
					
					
				}elseif($serviceID == '02' && $Dataplan == '15000'){	
										
			        $amount = $gloF;
			        
			        $valu = "Glo 15GB";
					$mobplan = "86";
							
							
							//etisalat data
							
							}elseif($serviceID == '03' && $Dataplan == '500'  ){
		
		$amount = $etisalatB;
		
		$valu = "9Mobile 500MB";
		$mobplan = "88";
		
		
			
			}elseif($serviceID == '03' && $Dataplan == '1000' ){
				
    		$amount = $etisalatC;
    		
    		$valu = "9Mobile 1GB";
			$mobplan = "89";
			
			
			}elseif($serviceID == '03' && $Dataplan == '2000' ){
				
    		$amount = $etisalatE;
    		
    		$valu = "9Mobile 2GB";
			$mobplan = "90";
			
				}elseif($serviceID == '03' && $Dataplan == '3000' ){
					
				$amount = $etisalatF;
				
				$valu = "9Mobile 3GB";
				$mobplan = "91";
				
					}elseif($serviceID == '03' && $Dataplan == '4000' ){
						
						$amount = $etisalatG;
						
						$valu = "9Mobile 4GB";
						$mobplan = "92";
						
						}elseif($serviceID == '03' && $Dataplan == '5000' ){
						
						$amount = $etisalatI;
						
						$valu = "9Mobile 5GB";
						$mobplan = "Nil";
						
			}elseif($serviceID == '03' && $Dataplan == '10000'){	
										
			        $amount = $etisalatJ;
			        
			        $valu = "9Mobile 10GB";
					$mobplan = "Nil";
								
							}			
							
							else{
							
							$mobplan = "Nil";
							$amount = $Dataplan;	
								
							
								}	
								
								
		  if($network === '01'){
	$gladNet = "1";
		$service = "MTN SME DATA";
	
	}elseif($network === '02'){
		
		$gladNet = "3";
		$service = "GLO SME DATA";
		
		}elseif($network === '03'){
		
		$gladNet = "4";
		$service = "9Mobile SME DATA";
			
			}elseif($network === '04'){
				
				$gladNet = "2";
				$service = "Airtel SME DATA";
				
				}else{
				$gladNet = "Nil";
				$service = "Unknown"; }	
				
				
		
	     
	
		$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 = '$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/sme-data?apikey=$key&service=$serviceID&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,"message"=>$mgs));



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

