<?php
    include'connection.php';
        
        $apiUsrId=170;
        $apiToken="2bc8fc628274544b5ba8445989e865ec";
        $url="http://ozzyrecharge.com/API/Balance?UserID=$apiUsrId&Token=$apiToken";
        $request_timeout = 60; // 60 seconds timeout
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($ch);
		$curl_error = curl_errno($ch);
		$getserver= curl_getinfo($ch);
		curl_close($ch);
		$data2=json_decode($output);
		$errorcode = $data2->errorcode;
		$Ozzy_ApiBal = $data2->bal;
		
    date_default_timezone_set("asia/Kolkata");
	$datetime= Date("Y-m-d - h:i:s A");
	$time= Date("h:i:s A");
	$date= Date("Y-m-d");
    
    $id = $_GET['AGENTID'];
    $status = $_GET['STATUS'];
    $orderid = $_GET['TRANID'];
    $txid = $_GET['LIVEID'];
    $msg = $_GET['MSG'];
    
    $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='Ozzy_Api' and status='SUCCESS' order by Id desc";
    	$reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
	    $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
    	$LapuOldBal=$dataGetLapuOldBal['ApiBal'];
    	$remaiBal=$LapuOldBal-$amount;
		$LapuDiff=$Ozzy_ApiBal-$remaiBal;
		
		$sucessquery="update recharge set Status='SUCCESS', Transactionid='$orderid', Operatorid='$txid', API='Ozzy_Api', ApiBal='$Ozzy_ApiBal', AdminOpId='S', LapuNo='Ozzy_Api', Diff='$LapuDiff' where Id='$id'";
		mysqli_query($con,$sucessquery);
		
		$quUpdatebal="UPDATE myapi SET LapuBal='$Ozzy_ApiBal', LastUpdate='$date', OrderId='$id' WHERE LapuNumber='Ozzy_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='Ozzy_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 !";
    		
    		}
	    }
	}
?>