<?php
    
    $apiLoop   = "STOP";
    $status    = "";   
    $ApiBal    = "";   
    $respoMsg  = "";   
    $txnId     = "";   
    $errCode   = "";   
    $OrderID   = "";   
	$partnerId = "";  
	$LapuApi   = "";
	
	
	$apiUsrId = "159";
	$apiToken = "GnQB5VE8GoNCHfUnR";
	$apiUrl   = "https://rcrecharge.com/Rechargeapi.asmx/Recharge?UserId=$apiUsrId&APIKey=$apiToken&MobileNo=$mnum&OperatorCode=$ApiOpCode&Amount=$amount&APIReferanceID=$newrechid";
    
    $ApiHitReq="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','REQUEST','$apiUrl')";
	mysqli_query($con,$ApiHitReq);
	
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
	$ApiHitRes="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','RESULT','$response')";
	mysqli_query($con,$ApiHitRes);
	
    $result   = json_decode($response);
		                                            
	$status    = $result->Status;                                            
	$ApiBal    = $result->Balance;
    $respoMsg  = $result->Message;   
    $txnId     = $result->TransactionID; 
    $partnerId = $result->ReferenceID;
    
    $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")
        {
            if($ApiBal<10000 && $ApiBal>9000 || $ApiBal<5000 && $ApiBal>4000 || $ApiBal<3000 && $ApiBal>2000 || $ApiBal<1000)
            {
                $RoundApiBal = floor($ApiBal);
                $toMobile = "8410930786";
                $wmsg    = "$LapuApi wallet balance is Rs $RoundApiBal Only, Please Update Wallet Balance Quickly !";
                require'../wmsg.php';
            }
            
            require'../LinkFile/rechSuccess.php';
        }
        else if($status=="3" || $status=="FAIL" )
        {
            if($countSetApi>$x)
	    	{
		        $apiLoop="CONTINUE";
		    }
		    else
		    {
		        if(chk_msg($respoMsg,'Insufbalan')==10)
	            {
	                $respoMsg   ="Failed !";
	                $OpDeactive = "UPDATE comm_slab SET Status='DEACTIVE' WHERE Userid='$userid' AND OpCode='$OpCode'";
	                mysqli_query($con,$OpDeactive);
	            }
	
		        require'../LinkFile/rechFailed.php';
		    }   
        }
        else if($status=="1" || $status=="PENDING")
        {
            require'../LinkFile/rechPending.php';
        }
    
?>