<?php
    include'connection.php';
    include'time.php';
    //include'Retailers/functions.php';
        
    $id = $_GET['AGENTID'];
    $status = $_GET['STATUS'];
    $orderid = $_GET['TRANID'];
    $txid = $_GET['LIVEID'];
    $msg = $_GET['MSG'];
    $BAL = $_GET['BAL'];
    
    $newrechid=$id;
    
	$quGetRechData="SELECT * FROM recharge WHERE Id='$id'";
	$reGetRechData=mysqli_query($con,$quGetRechData);
	$dataGetRech=mysqli_fetch_array($reGetRechData);
	$userid=$dataGetRech['Userid'];
	$mobile=$dataGetRech['Mobile'];
	$amount=$dataGetRech['Amount'];
	$DebitAmount=$dataGetRech['Debitamount'];
	$operator=$dataGetRech['Companyname'];
	$CheckStatus=$dataGetRech['Status'];
	$Partnerid=$dataGetRech['Partnerid'];
	
	$UpdateCBS="update recharge set CallBack='1' where Id='$id'";
	mysqli_query($con,$UpdateCBS);
		
		$query2="select * from retailer where Userid='$userid'";
		$result2=mysqli_query($con,$query2);
		$databal=mysqli_fetch_array($result2);
		$username=$databal['Name'];
		$parentid=$databal['Parentid'];
		$grandparentid=$databal['Grandparentid'];
		$CallBackUrl=$databal['CallBackUrl'];
		$ApiActive=$databal['ApiActive'];
	    
	    if($ApiActive==1)
	    {
	        $ch = curl_init();
	        
    		curl_setopt($ch, CURLOPT_URL, "$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$id&opid=$txid&errmsg=$txid");
	    	curl_setopt($ch, CURLOPT_POST, false );
		    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    		curl_setopt($ch, CURLOPT_TIMEOUT, 0);
	    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
		
		    curl_close($ch);
	    }
	
	        
	if($status=="PENDING" || $status=="1")
	{
    	$pendingquery="update recharge set Status='PENDING', Transactionid='$orderid' where Id='$id'";
	    mysqli_query($con,$pendingquery);
	}
	elseif($status=="SUCCESS" || $status=="2")
	{
	    $GetLapuOldBal="select * from recharge where LapuNo='Ambika_Api' and status='SUCCESS' order by Id desc";
    	$reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
	    $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
    	$LapuOldBal=$dataGetLapuOldBal['ApiBal'];
    	$remaiBal=$LapuOldBal-$amount;
		if(empty($BAL) || $BAL==0)
		{
		    $clBal=$remaiBal;
		    $LapuDiff=0;
		}
		else
		{
		    $clBal=$BAL;
		    $LapuDiff=$remaiBal-$BAL;
		}
		$sucessquery="update recharge set Status='SUCCESS', Transactionid='$orderid', Operatorid='$txid', API='Ambika_Api', ApiBal='$clBal', AdminOpId='S', LapuNo='Ambika_Api', Diff='$LapuDiff' where Id='$id'";
		mysqli_query($con,$sucessquery);
		
		$quUpdatebal="UPDATE myapi SET LapuBal='$clBal', LastUpdate='$date', OrderId='$id' WHERE LapuNumber='Ambika_Api'";
		mysqli_query($con,$quUpdatebal);
	}
	elseif($status=="FAILED" || $status=="3" || $status=="4")
	{   
	    $quGetAutoFailedStatus="select * from Auto_Failed where Id='1'";
		$reGetAutoFailedStatus=mysqli_query($con,$quGetAutoFailedStatus);
		$dataGetAutoFailedStatus=mysqli_fetch_array($reGetAutoFailedStatus);
		$AutoFailedStatus=$dataGetAutoFailedStatus['Status'];
		
	    if($CheckStatus=="FAILURE")
	    {
	        
	    }
	    elseif($AutoFailedStatus=="Deactive" || $txid=="Insufficient Balance.")
	    {
	        $UpdateRechApibal="update recharge set ReHit='1', AdminOpId='$txid' where Id='$id'";
	    	mysqli_query($con,$UpdateRechApibal);
	    }
	    else
	    {
    		$opBal=getBal($userid);
		    $clBal=$opBal+$DebitAmount;
		    
		    if(10==strlen($userid))
		    {
		        $wallet="MLM_Wallet";
		        $usrTable="MLM_Users";
		    }
		    else
		    {
		        $wallet="wallet";
		        $usrTable="retailer";
		    }
			$refundaddbalquery="INSERT INTO $wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time)
    		values('$userid','$opBal','$DebitAmount','$clBal','Recharge_Refund','SELF','$newrechid','Recharge_Refund','$date','$time')";
    		if(mysqli_query($con,$refundaddbalquery))
    		{
    		    $refundewalletid=mysqli_insert_id($con);
    		    
    			$failedquery="update recharge set Creditewalletid='$refundewalletid', Status='FAILED', Transactionid='$orderid', Operatorid='$txid', Responsetxt='$resText', API='Ambika_Api' where Id='$id'";
    			
    			mysqli_query($con,$failedquery);
    			
    			$deletecomm="delete from commwallet where Rechargeid='$id'";
    			
		    	    $query9="UPDATE $usrTable SET Balance='$refundnewbal' WHERE Userid='$userid'";
			    	mysqli_query($con,$query9);
    			
    			echo "Your Recharge Request Has Been Failed Now !";
    		
    		}
	    }
	}
?>