<?php
    
    $apiLoop   = "STOP";
    $status    = "";   
    $ApiBal    = "";   
    $respoMsg  = "";   
    $txnId     = "";   
    $errCode   = "";   
    $OrderID   = "";   
	$partnerId = "";  
	$LapuApi   = "";
	
    $apiToken = "ec30f64528da410e9c478205d3b57cd2";
    if($OpCode==5 || $OpCode==4)
    {
        $field1 = $mnum;
    }
    else
    {
        $field1 = "";
    }
    
    $apiUrl="https://www.digitalbiz.co.in/apiservice.asmx/Recharge?apiToken=$apiToken&mn=$mnum&amt=$amount&op=$ApiOpCode&reqid=$newrechid&field1=$field1&field2=''"; 
	
	$quApiHit="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','REQUEST','$apiUrl')";
	mysqli_query($con,$quApiHit);
	
	
	$curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    ));

    $response = curl_exec($curl);
    $result   = simplexml_load_string($response);
    curl_close($curl);
    
    $quApiHit="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','RESULT','$response')";
	mysqli_query($con,$quApiHit);
	
    $status    = $result->status;                                            
	$ApiBal    = $result->balance;
    $respoMsg  = $result->remark;   
    $txnId     = $result->field1;   
    $errCode   = $result->ec;
    $partnerId = $result->apirefid;
    
    $OrderID   = $newrechid;
	$Printmsg  = $respoMsg;
	$LapuApi   = $api;
	
	
	$updateLapu="UPDATE recharge SET Api='$api', Lapu='$LapuApi' where Id='$newrechid'";
	mysqli_query($con,$updateLapu);
    
	    if($status=="SUCCESS")
        {
            if($ApiBal<20000 && $ApiBal>19300 || $ApiBal<15000 && $ApiBal>14200 || $ApiBal<10000 && $ApiBal>9000 || $ApiBal<5000 && $ApiBal>4000 || $ApiBal<3000)
            {
                $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=="FAILED" || $status=="REFUND" )
        {
            if($countSetApi>$x)
	    	{
		        $apiLoop="CONTINUE";
		    }
		    else
		    {
		        if(chk_msg($respoMsg,'nsuf')==4)
	            {
	                $respoMsg   ="Failed !";
	                $OpDeactive = "UPDATE comm_slab SET Status='DEACTIVE' WHERE Userid='$userid' AND OpCode='$OpCode'";
	                mysqli_query($con,$OpDeactive);
                    $toMobile = "8410930786";
                    $wmsg    = "$LapuApi wallet Insufficiant Balance and Op $OpName is Deactive !";
                    require'../wmsg.php';
	            }
	
		        require'../LinkFile/rechFailed.php';
		    }   
        }
        else if($status=="PENDING")
        {
            require'../LinkFile/rechPending.php';
        }
    
?>