<?php
    
    $apiLoop   = "STOP";
    $status   = "";
    $errorMsg = "";   
    $respoMsg = "";   
    $txnId    = "";   
    $LapuNo   = "";   
    $ApiBal   = "";   
	$roffer   = "";  
	$is_roam  = "";   
	$LapuApi  = "";
	
    
    if($LapuGroup>0)
    {
        $LapuGroupIdCode="&group_id=".$LapuGroup;
    }
    else
    {
        $LapuGroupIdCode="";
    }
    
    if($ReqRoffer>0 &&  $R_offer=="false")
    {
        $R_offer        = "true";
        $quReqRoffer    = "SELECT SUM(Amount), SUM(R_offer) FROM recharge WHERE Date='$date' AND Api='$api' AND Status='SUCCESS' AND OpCode='$OpCode' AND Userid='$userid'";
        $reReqRoffer    = mysqli_query($con,$quReqRoffer);
        $dataReqRoffer  = mysqli_fetch_array($reReqRoffer);
        $totalAmt       = $dataReqRoffer[0];
        $totalRoffer    = $dataReqRoffer[1];
        $countRowRO     = $dataReqRoffer[2];
        if($totalAmt>0)
        {
            $rofferPercent  = $totalRoffer*100/$totalAmt;
            if($rofferPercent>=$ReqRoffer)
            {
                $R_offer = "false";
            }
        }
    }
    $apiHTTPurl = "https://mrobotics.in/api/recharge_statewise?";
    $apiUrl     = "api_token=37a1e7bd-3d24-4d73-8c17-ecd76d5a847e&mobile_no=$mnum&amount=$amount&company_id=$ApiOpCode&order_id=$newrechid&is_stv=$STV&no_roaming=$Circle&only_roffer=$R_offer$LapuGroupIdCode";
	$reqUrl     = $apiHTTPurl.$apiUrl;
	$quApiHit   = "INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','REQUEST','$reqUrl')";
	mysqli_query($con,$quApiHit);
	
	$ch = curl_init();
	if (!$ch)
    {
        die("Couldn't initialize a cURL handle");
    }
    curl_setopt($ch, CURLOPT_URL,$apiHTTPurl); // Target URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$apiUrl);
    $result = curl_exec ($ch);
    
    $quApiHit="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','RESULT','$result')";
	mysqli_query($con,$quApiHit);
	
    $result = (array)json_decode($result);
	
    $status   = $result['status'];
    $errorMsg = $result['errorMessage'];   
    $respoMsg = $result['response'];   
    $txnId    = $result['tnx_id'];
    $LapuNo   = $result['lapu_no'];   
    $ApiBal   = $result['balance'];   
	$roffer   = $result['roffer'];  
	$is_roam  = $result['is_roaming'];   
	$LapuApi  = $LapuNo;
	
	if($is_roam=="true")
	{
	    $roamingMsg="R";
	}
	else
	{
	    $roamingMsg="C";
	}
												
	$updateLapu="UPDATE recharge SET Api='$api', Lapu='$LapuApi', Roaming='$roamingMsg' where Id='$newrechid'";
	mysqli_query($con,$updateLapu);
    
    if($status=="SUCCESS" || $status=="success")
	{
	    require'../LinkFile/rechSuccess.php';
	}
	elseif($status=="FAILED" || $status=="failure")
	{
	       
        $flag=0;
	    require'../LinkFile/reasonFailed.php';
														      
		if($flag==0 && $countSetApi>$x)
		{
		    $apiLoop="CONTINUE";
		}
		else
		{
		    require'../LinkFile/rechFailed.php';
		}
	}
	elseif($status=="PENDING" || $status=="pending")
	{
	    require'../LinkFile/rechPending.php';
	} 

?>