<?php
    
    $apiLoop   = "STOP";
    $status    = "";   
    $ApiBal    = "";   
    $respoMsg  = "";   
    $txnId     = "";   
    $errCode   = "";   
    $OrderID   = "";   
	$partnerId = "";  
	$LapuApi   = "";
	
	
	$apiUsrId = "730";
    $apiToken = "3247fd46b02aeb152e1df75684c77ca0";
    $apiUrl   = "http://api.ambikaexchange.com//ExchangeTransactionAPI?UserID=$apiUsrId&Token=$apiToken&Account=$mnum&Amount=$amount&SPKey=$ApiOpCode&APIRequestID=$newrechid&Format=1";
    
    $ApiHitReq="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','REQUEST','$apiUrl')";
	mysqli_query($con,$ApiHitReq);
	
	$request_timeout = 60; // 60 seconds timeout
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $apiUrl);
	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);
	
	$ApiHitRes="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','RESULT','$output')";
	mysqli_query($con,$ApiHitRes);
	
	$result = json_decode($output);
		                                            
	$status    = $result->status;                                            
	$ApiBal    = $result->bal;
    $respoMsg  = $result->msg;   
    $txnId     = $result->opid; 
    $partnerId = $result->rpid;
    $errorcode = $result->errorcode;
    $commrate  = $result->commrate;
    $commamount = $result->commamount;
    $agentid   = $result->agentid;
    
    $OrderID   = $newrechid;
	$Printmsg  = $respoMsg;
	$LapuApi   = $api;
	
	
	$updateLapu="UPDATE recharge SET Api='$api', Lapu='$LapuApi' where Id='$newrechid'";
	mysqli_query($con,$updateLapu);
    
	    if($status=="2" || $status=="Success")
        {
            require'../LinkFile/rechSuccess.php';
        }
        else if($status=="3" || $status=="Failed" )
        {
            if($countSetApi>$x)
	    	{
		        $apiLoop="CONTINUE";
		    }
		    else
		    {
		        if(chk_msg($respoMsg,'Insufbalan')==10)
	            {
	                $respoMsg = "Transaction Failed !";
	                $OpDeactive = "UPDATE comm_slab SET Status='DEACTIVE' WHERE Userid='$userid' AND OpCode='$OpCode'";
	                mysqli_query($con,$OpDeactive);
	            }
	            $respoMsg ="Transaction Failed !";
		        require'../LinkFile/rechFailed.php';
		    }   
        }
        else if($status=="1" || $status=="Pending")
        {
            require'../LinkFile/rechPending.php';
        }
    
?>