<?php
    require'../connection.php';
    require'../functions.php';
    
    $status   = "";
    $errorMsg = "";   
    $respoMsg = "";   
    $txnId    = "";   
    $LapuNo   = "";   
    $ApiBal   = "";   
	$roffer   = "";  
	$is_roam  = "";   
	$LapuApi  = "";
	
	//$order_id = "3579326";
	$order_id = $_GET['order_id'];
	
	$dataRech = getRech($order_id);
	$Status   = $dataRech['Status'];
	if($Status=="PENDING")
	{
	    $ApiName    = $dataRech['Api'];
	    $ClinteId   = $dataRech['Userid'];
	    $DebitAmt   = $dataRech['Debit'];
	    
	    $dataApi    = getSellerApi($ApiName);
	    $ApiUsrId   = $dataApi['ApiUsrId'];
	    $ApiToken   = $dataApi['ApiToken'];
	    $chkRechUrl = $dataApi['chkRechUrl'];
	    $ReqFormat  = $dataApi['ReqFormat'];
	    $USERIDkey  = $dataApi['USERID'];
	    $TOKENkey   = $dataApi['TOKEN'];
	    $ORDERIDkey = $dataApi['ORDERID'];
	    
	    $STATSkey   = $dataApi['STATS'];
	    $MSGkey     = $dataApi['MSG'];
	    $ERRMSGkey  = $dataApi['ERRMSG'];
	    $ERRCODEkey = $dataApi['ERRCODE'];
	    $TXNIDkey   = $dataApi['TXNID'];
	    $PARTNRIDkey= $dataApi['PARTNRID'];
	    $BALkey     = $dataApi['BAL'];
	    $LAPUkey    = $dataApi['LAPU'];
	    $ROFRkey    = $dataApi['ROFR'];
	    $ROAMkey    = $dataApi['ROAM'];
	    
	    if(!empty($chkRechUrl))
	    {
	        if(!empty($USERIDkey))
	        {
	            $USERIDkeyUrl = "&".$USERIDkey."=".$ApiUsrId;
	        }
	        if(!empty($TOKENkey))
	        {
	            $TOKENkeyUrl = "&".$TOKENkey."=".$ApiToken;
	        }
	        
	        $apiHTTPurl = $chkRechUrl."?";
	        $apiData    = $ORDERIDkey."=".$order_id.$TOKENkeyUrl.$USERIDkeyUrl;
	        echo $apiUrl     = $apiHTTPurl.$apiData;
	        $curlfile   = "curl".$ReqFormat.".php";
	        require $curlfile;
	        
	        $status   = $result->$STATSkey;
	        $errorMsg = $result->$ERRMSGkey;
	        $respoMsg = $result->$MSGkey;
	        $txnId    = $result->$TXNIDkey;
	        $ApiBal   = $result->$BALkey;
	        $PARTNRID = $result->$PARTNRIDkey;
	        $LapuApi  = $ApiName;
	        if($ApiName=="M_ROBO" || $ApiName=="M_ROBO2")
	        {   
	            $LapuNo   = $result->$LAPUkey;
	            $roffer   = $result->$ROFRkey;
	            $is_roam  = $result->$ROAMkey;
	            $LapuApi  = $LapuNo; 
	        }
	        $STATUS4Digit = strtoupper(substr($status,0,4));
	        
	        if($STATUS4Digit=="SUCC")
	        {
	            $UpadateStatus="UPDATE recharge SET Status='SUCCESS', OpId='$txnId', ApiBal='$ApiBal', Lapu='$LapuApi' WHERE Id='$order_id'";
		        if(mysqli_query($con,$UpadateStatus))
		        {
		            header("location:https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php");
		        }
		
	        }
	        elseif($STATUS4Digit=="FAIL" || $STATUS4Digit=="REFU")
	        {
	            $opBal = getBal($ClinteId);
                $clBal = $opBal+$DebitAmt;
	
	            $txnType="Recharge_Refund";
	            if(0==chk_dup_wallet_txn($ClinteId,$order_id,$txnType))
	            {
	                $quRefund="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,Remarks)
	                VALUES ('$date','$time','$ClinteId','$opBal','$DebitAmt','$clBal','$order_id','Recharge_Refund','$IP','Recharge_Refund')";
	                if(mysqli_query($con,$quRefund))
	                {
	                    $quFailed="UPDATE recharge SET Status='FAILED', ApiBal='$ApiBal', OpId='Txn Failed !' WHERE Id='$order_id'";
	                    if(mysqli_query($con,$quFailed))
	                    {
	                        header("location:https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php");
	                    }
	            
	                }
	            }
	            else
	            {
	                echo "<pre>";
	                echo $result."<br>";
	                echo "Duplicate Refund Not Allowed";
	                echo "<a href='https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php'>Back</a>";
	            }
	        }
	        elseif($STATUS4Digit=="PEND")
	        {
	            header("location:https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php");
	        }
	        else
	        {
	            echo "<pre>";
	            print_r($response);
	            echo "<a href='https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php'>Back</a>";
	        }
	    }
	}
	else
	{
	    header("location:https://www.llprecharge.in/LLPRECHARGE/Admin/Pending_Recharge.php");
	}
	
?>