<?php
	require'../connection.php';
	require'../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'];
    $IP         = $_SERVER['REMOTE_ADDR'];
	
    $ReciData   = $IP.", ".$userid.", ".$token.", ".$partnerId.", ".$OpCode.", ".$mnum.", ".$amount;
    
    $dataUser   = getUser($userid);
    $usrType    = $dataUser['Type'];
    
    $Ptime      = strtotime($time);
    $Ptime      = $Ptime - (1 * 60);
    $Ptime      = date("h:i:s A", $Ptime);
    
    $quPendingRech="SELECT * FROM recharge WHERE OpCode='$OpCode' AND Userid='$userid' AND Status='PENDING' AND Date='$date' AND Time<='$Ptime'";
    $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'";
	$reApiUsr   = mysqli_query($con,$quApiUsr);
	$dataApiUsr = mysqli_fetch_array($reApiUsr);
	$countUser  = mysqli_num_rows($reApiUsr);
	$Status     = $dataApiUsr['Status'];
	$ApiToken   = $dataApiUsr['Token'];
	$IpAddr     = $dataApiUsr['Ip'];
	
	$quApiPartnerHit="INSERT INTO api_hit (Date,Time,PartnerId,ApiName,Type,Disc) VALUES ('$date','$time','$partnerId','$userid','HIT','$ReciData')";
	if(mysqli_query($con,$quApiPartnerHit))
	{
	    $apiHitId = mysqli_insert_id($con);
	}
	$api_errorPass=1;
	if($countUser!=1)
	{
	    $msg="Invalid Userid !";
	}
	elseif($Status!="ACTIVE")
	{
	    $msg="User Is Not Active !";
	}
	elseif($IP!=$IpAddr && !empty($IpAddr))
	{
	    $msg="Api Token Is Invalid !";
	}
	elseif($ApiToken!=$token)
	{
	    $msg="Api Token Is Invalid !";
	}
	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);
		
		require'../LinkFile/recharge_code.php';
	}
	
	if($api_errorPass==1)
	{
	    $Printmsg=$msg;
	    $rechStatus="FAILED";
	    require'../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();
?>	