<?php
  

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


	// API parameter
    
	if(isset($_REQUEST['apikey']) && isset($_REQUEST['service']) && isset($_REQUEST['smartNo']) && isset($_REQUEST['vcode']) && isset($_REQUEST['ref']) ){
	
	$apikey = $_REQUEST['apikey'];
	$serviceID = $_REQUEST['service'];
	$vcode = $_REQUEST['vcode'];
	$smartNo = $_REQUEST['smartNo'];
	$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);
	$vcode  = str_replace( array( '+', '-', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $vcode);
	$smartNo = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $smartNo);
	$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);
	
	
	$variation_code = $vcode;
	
	$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 billing");
	$rate = mysqli_fetch_array($query_com);


$query_regular = mysqli_query($conn,"SELECT * FROM regular_billing");
$Regubil = mysqli_fetch_array($query_regular);
	
	$access = 'free';
	$dstv = $rate['dstv'];
	$gotv = $rate['gotv'];
	$startimes = $rate['startimes'];
	$showmax = $rate['showmax'];

		
		
		if($network === 'gotv' && $level !== $access ){
		
		$per = $gotv;
        $charge = 0;		
		
			
			}elseif($network === 'dstv' && $level !== $access){
				
			$per = $dstv;
			$charge = 0;
			
			
				}elseif($network === 'startimes' && $level !== $access){
					
				$per = $startimes;
				$charge = 0;

          }elseif($network === 'showmax' && $level !== $access){
					
				$per = $showmax;
                $charge = 0;				
				
		
				}
				
				
				if($network === 'gotv' ){
	
		     $cable = "1";
		     $product = "GOTV Payment";
			
			}elseif($network === 'dstv'){
			
			$cable = "2";
			
			$product = "DSTV Payment";
				
			}elseif($network === 'startimes'){
					
					$cable = "3";
					
					$product = "Startimes Payment";
					
					
								}elseif($network === 'showmax'){
					
					$cable = "3";
					
					$product = "Showmax Payment";
				
						}else{ 
						$cable = "Unknown";
						$affpay = 0;	
						$product = "";}
						
						
						
			$query_go = mysqli_query($conn,"SELECT * FROM gotv");
	$mygotv = mysqli_fetch_array($query_go);

$query_ds = mysqli_query($conn,"SELECT * FROM dstv");
	$mydstv = mysqli_fetch_array($query_ds);

$query_st = mysqli_query($conn,"SELECT * FROM startimes");
	$mystar = mysqli_fetch_array($query_st);

	$gotv_smallieA = $mygotv['smallieA'];
	$gotv_smallieB = $mygotv['smallieB'];
	$gotv_smallieC = $mygotv['smallieC'];
	$gotv_jinja = $mygotv['jinja'];
	$gotv_jolli = $mygotv['jolli'];
	$gotv_max = $mygotv['max'];
	
	
	$startimes_novaA = $mystar['novaA'];
        $startimes_novaB = $mystar['novaB'];
        $startimes_nova = $mystar['nova'];

	$startimes_basicA = $mystar['basicA'];
        $startimes_basicB = $mystar['basicB'];
        $startimes_basic = $mystar['basic'];

	$startimes_smartA = $mystar['smartA'];
        $startimes_smartB = $mystar['smartB'];
        $startimes_smart = $mystar['smart'];

	$startimes_classicA = $mystar['classicA'];
        $startimes_classicB = $mystar['classicB'];
        $startimes_classic = $mystar['classic'];

	$startimes_uniqueA = $mystar['uniqueA'];
        $startimes_uniqueB = $mystar['uniqueB'];
        $startimes_unique = $mystar['uniqueC'];

	$startimes_superA = $mystar['superA'];
        $startimes_superB = $mystar['superB'];
        $startimes_super = $mystar['super'];
	
	$dstv_padi = $mydstv['padi'];
	$dstv_yanga = $mydstv['yanga'];
	$dstv_confam = $mydstv['confam'];
	$dstv_compact = $mydstv['compact'];
	$dstv_compact_plus = $mydstv['compact_plus'];
	$dstv_premium = $mydstv['premium'];
	$dstv_asia = $mydstv['asia'];
	$dstv_premium_french = $mydstv['premium_french'];			

		
								
								
	
				
				
				
		
if($variation_code === 'gotv_smallie' ){

$amount = $gotv_smallieA;
$mobplan = "62";
		

			
				
}elseif($variation_code === 'gotv_smallie_3months' ){
				
$amount = $gotv_smallieB;
$mobplan = "63";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "gotv-smallie-3months";
$bizcode = "";

	
			
}elseif($variation_code === 'gotv_smallie_1year' ){
					
$amount = $gotv_smallieC;
$mobplan = "64";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "gotv-smallie-1year";
$bizcode = "";

				
}elseif($variation_code === 'gotv_jinja' ){

$amount = $gotv_jinja ;
$mobplan = "61";
$smartcode = "gotv_jinja";
$mobcode = "gotv_jinja";
$nigcode = "";
$clubcode = "gotv-jinja";
$bizcode = "";


}elseif($variation_code === 'gotv_jolli' ){
				
$amount = $gotv_jolli;
$mobplan = "60";
$smartcode = "gotv_jolli";
$mobcode = "gotv_jolli";
$nigcode = "";
$clubcode = "gotv-jolli";
$bizcode = "";	

	
			
}elseif($variation_code === 'gotv_max' ){
					
$amount = $gotv_max;
$mobplan = "59";
$smartcode = "gotv_max";
$mobcode = "gotv_max";
$nigcode = "";
$clubcode = "gotv-max";
$bizcode = "";

		
				
}elseif($variation_code === 'startimes_nova_daily' ){

$amount = $startimes_novaA;
$mobplan = "76";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

}elseif($variation_code === 'startimes_nova_weekly' ){

$amount = $startimes_novaB;
$mobplan = "71";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

}elseif($variation_code === 'startimes_nova' ){

$amount = $startimes_nova;
$mobplan = "66";
$smartcode = "startimes_nova";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_basic_daily' ){
				
$amount = $startimes_basicA;
$mobplan = "77";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_basic_weekly' ){
				
$amount = $startimes_basicB;
$mobplan = "72";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_basic' ){
				
$amount = $startimes_basic;
$mobplan = "67";
$smartcode = "startimes_basic";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";	



}elseif($variation_code === 'startimes_smart_daily' ){
					
$amount = $startimes_smartA;
$mobplan = "78";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_smart_weekly' ){
					
$amount = $startimes_smartB;
$mobplan = "73";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

	
			
}elseif($variation_code === 'startimes_smart' ){
					
$amount = $startimes_smart;
$mobplan = "68";
$smartcode = "startimes_smart";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_classic_daily' ){

$amount = $startimes_classicA;
$mobplan = "79";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_classic_weekly' ){

$amount = $startimes_classicB;
$mobplan = "74";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

				
}elseif($variation_code === 'startimes_classic' ){

$amount = $startimes_classic;
$mobplan = "69";
$smartcode = "startimes_classic";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_super_daily' ){
				
$amount = $startimes_superA;
$mobplan = "80";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_super_weekly' ){
				
$amount = $startimes_superB;
$mobplan = "75";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_super' ){
				
$amount = $startimes_super;
$mobplan = "70";
$smartcode = "startimes_super";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_unique_daily' ){
					
$amount = $startimes_uniqueA;
$mobplan = "unique";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'startimes_unique_weekly' ){
					
$amount = $startimes_uniqueB;
$mobplan = "unique";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

	
			
}elseif($variation_code === 'startimes_unique' ){
					
$amount = $startimes_unique;
$mobplan = "unique";
$smartcode = "startimes_unique";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


                           	
				
}elseif($variation_code === 'dstv_padi' ){

$amount = $dstv_padi;
$mobplan = "1";
$smartcode = "dstv_padi";
$mobcode = "dstv_padi";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'dstv_yanga' ){
					
$amount = $dstv_yanga;
$mobplan = "2";
$smartcode = "dstv_yanga";
$mobcode = "dstv_yanga";
$nigcode = "";
$clubcode = "";
$bizcode = "";	

				
}elseif($variation_code === 'dstv_confam' ){

$amount = $dstv_confam;
$mobplan = "3";
$smartcode = "dstv_confam";
$mobcode = "dstv_confam";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'dstv_compact' ){
					
$amount = $dstv_compact;
$mobplan = "4";
$smartcode = "dstv_compact";
$mobcode = "dstv_compact";
$nigcode = "";
$clubcode = "";
$bizcode = "";

				
}elseif($variation_code === 'dstv_compact_plus' ){

$amount = $dstv_compact_plus;
$mobplan = "";
$smartcode = "dstv_compact_plus";
$mobcode = "dstv_compact_plus";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'dstv_premium' ){

$amount = $dstv_premium;
$mobplan = "dstv3";
$smartcode = "dstv_premium";
$mobcode = "dstv_premium";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'dstv_asia' ){
					
$amount = $dstv_asia;
$mobplan = "dstv6";
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

				
}elseif($variation_code === 'dstv_premium_french'){ 

$amount = $dstv_premium_french;
$mobplan = "dstv9"; 
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'dstv1' ){

$amount = 1000;
$mobplan = "d"; 
$smartcode = "";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";


}elseif($variation_code === 'gotv1' ){
					
$amount = 600;
$mobplan = "go";
$smartcode = "gotv_max";
$mobcode = "gotv_max";
$nigcode = "";
$clubcode = "gotv-max";
$bizcode = "";

		
				
}elseif($variation_code === 'startimes1' ){

$amount = 970;
$mobplan = "no";
$smartcode = "startimes_nova";
$mobcode = "";
$nigcode = "";
$clubcode = "";
$bizcode = "";

						
		}else{

$amount = 10000000000000000000000000000000000;

		}			
				
		
$amount = str_replace( array( '+', '-', '--', '/', '(', ')',   '_',  'drop',  '//', 'delete', 'update', 'insert', '$', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);	     
	
		$comi = ($per/100)*$amount;
		
	$debit1 = $amount - $comi;
	
	$debit = $debit1 + $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 = '$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/paytv?apikey=$key&service=$network&smartno=$smartNo&vcode=$vcode&amount=$amount&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);


$dataR = curl_exec($ch);

$result = json_decode($dataR);



//Close the cURL handle.

curl_close($ch);



$resp = json_decode($dataR);


$msg = $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,meterno,amount,debit,charge,comi,ref,status,email,customer,balanceB,balanceA,del,date) VALUES('$product','$channel','$smartNo','$amount','$debit','$charge','$comi','$ref','$stat','$email','$fname $lname','$blc','$balA','$del','$dateTime')") or die(mysqli_error());




response(100,array("status"=>$stat,"Service"=>$product,"smartNo"=>$smartNo,"Variation_Code"=>$variation_code,"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;
}
    
    ?>
    
  
   	

