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


    
	if(isset($_REQUEST['apikey']) && isset($_REQUEST['network']) && isset($_REQUEST['sender']) && isset($_REQUEST['amount']) && isset($_REQUEST['ref'])){
	
	$auth = $_REQUEST['apikey'];
	$network = $_REQUEST['network'];
	$phone = $_REQUEST['phone'];
	$amount2 = $_REQUEST['amount'];
	$ref = $_REQUEST['ref'];
	$user = $_REQUEST['sender'];
    
        $limit = "99";

$amount2 = filter_var($amount2, FILTER_SANITIZE_STRING);
$amount2 = mysqli_real_escape_string ($conn, $amount2);
		
	if($amount2 >= '100'){
	    $amount = $_REQUEST['amount'];
	}else{
	    $amount = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
	}
        
	
$apikey = filter_var($apikey, FILTER_SANITIZE_STRING);
$network = filter_var($network, FILTER_SANITIZE_STRING);
$phone = filter_var($phone, FILTER_SANITIZE_STRING);
$ref = filter_var($ref, FILTER_SANITIZE_STRING);
$amount = filter_var($amount, FILTER_SANITIZE_STRING);
$amount2 = filter_var($amount2, FILTER_SANITIZE_STRING);


$apikey = mysqli_real_escape_string ($conn, $apikey);
$network = mysqli_real_escape_string ($conn, $network);
$phone = mysqli_real_escape_string ($conn, $phone);
$ref = mysqli_real_escape_string ($conn, $ref);
$amount = mysqli_real_escape_string ($conn, $amount);
$amount2 = mysqli_real_escape_string ($conn, $amount2);
$amount = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',   '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);

	
	$convee = '';
	
	$customer = '';
	
	$xname = '';
	
	$action = "Pay";
	
	
	$email = $user;
	$proc = '_pay-tv';
	$charge = '';
	$billersCode = $phone;
	$serviceID = 'Airtime';
	$channel = "API";
	$view = "View";
	
	// check if the account is valid
	
	if($param){
	    
	    response(107,"BAD REQUEST");} else{
	
	$qryApi = mysqli_query($conn,"SELECT * FROM api_setting");
         $apidata = mysqli_fetch_array($qryApi);

         $apikey = $apidata['APIkey'];
			
	
	if($apikey === $auth){
	    
	    // check if the user have balance
	
		$gb = mysqli_query($conn,"SElECT * FROM users WHERE email = '$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($conn,"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 = 0;
}

date_default_timezone_set('Africa/Lagos');

$dateTime = date(' Y-m-d h:i A');		
		

	$charge = ($per/100)*$amount;
	$credit = $amount-$charge;
						
	$amountpaid = $credit;
	
	
$gbs = mysqli_query($conn,"SElECT * FROM transactions WHERE ref = '$ref' ") or die(mysqli_error());	
	$rec = mysqli_fetch_array($gbs);
	
	$reseller = $rec['email'];

	if($reseller == $user ){
	    
	    // check if ref number exist
	    
	  $req = mysqli_query($conn,"SElECT * FROM deposit WHERE ref = '$ref' ") or die(mysqli_error());	
	
	$nu = mysqli_num_rows($req);

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

$add = mysqli_query($conn,"INSERT INTO deposit (ref,amount,charge,user,status,method,service,del,balanceB,balanceA) VALUES('$ref','$amount','$charge','$email $fname $lname','Completed','Airtime2Cash','$service','Delete','$blc','$balA')") or die(mysqli_error());

$qsel = "UPDATE transactions SET status='Completed',balanceA='$balA',channel='Wallet' WHERE ref='$ref' ";	
$sav = $conn->query($qsel);

response(101,"TRANSACTION SUCCESSFUL");

include('alert-user.php'); 

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


    
} 

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

    
    ?>