<?php
	include'../connection.php';
	include'../functions.php';
	
	ini_set('memory_limit', '-1');
	
    $time_start = microtime(true);
    
	$userid       = $_GET['userid'];
	$token        = $_GET['token'];
	$partnerId    = $_GET['partnerid'];
	$OpCode       = $_GET['opcode'];
	$mnum         = $_GET['mobile'];
	$amount       = $_GET['amount'];
    
    $dataUser=getUser($userid);
    $usrType=$dataUser['Type'];
    
    $Pdate = date("Y-m-d H:i:s");
    $Ptime = strtotime($Pdate);
    $Ptime = $Ptime - (1 * 60);
    $PdatePtime = date("Y-m-d H:i:s", $Ptime);
    
    $quPendingRech="SELECT * FROM recharge WHERE OpCode='$OpCode' AND Userid='$userid' AND Status='PENDING' AND Date='$date' AND PTime<='$PdatePtime'";
    $rePendingRech=mysqli_query($con,$quPendingRech);
    $countPendingRech=mysqli_num_rows($rePendingRech);
    
    if($countPendingRech>5)
    {
        $myresponse = array("status"=>"FAILED", "msg"=>"Operator Block For Pending Txn Above 5 !");
		echo $myresult = json_encode($myresponse);
		exit();
    }
    
	$quApiUsr   = "SELECT * FROM api_user WHERE Userid='$userid' AND Status='ACTIVE'";
	$reApiUsr   = mysqli_query($con,$quApiUsr);
	$dataApiUsr = mysqli_fetch_array($reApiUsr);
	$countUser  = mysqli_num_rows($reApiUsr);
	$ApiToken   = $dataApiUsr['Token'];
	$IpAddr     = $dataApiUsr['Ip'];
	
	$IP = $_SERVER['REMOTE_ADDR'];
	$api_errorPass=1;
	
	if($countUser!=1)
	{
	    $msg="User Not Active !";
	}
	elseif(!empty($IpAddr))
	{
	    if($IpAddr!==$IP)
	    {
	        $msg="Invalid IP Address !";
	    }
	}
	elseif($token!==$ApiToken)
	{    
	    $msg="Invalid API Token !";
	}
	elseif(empty($OpCode))
	{
	    $msg="Operator Id Not Found !";
	}
	elseif(empty($mnum))
	{
	    $msg="Mobile Number Not Found !";
	}
	elseif(empty($amount))
	{
	    $msg="Amount Not Found !";
	}			
	else
	{ 
        $api_errorPass=0;
		$usrBal=getBal($userid);
		
		include'../LinkFile/recharge_code.php';
	}
	
	if($api_errorPass==1)
	{
	    $Printmsg=$msg;
	    include'../LinkFile/respoApi.php';
	}
	$time_end = microtime(true);
    $execution_time = floor(($time_end - $time_start));
    $UpdateExcTime="UPDATE recharge SET R_time='$execution_time' WHERE Id='$newrechid'";
	mysqli_query($con,$UpdateExcTime);
	
	ob_end_flush();
?>	