<?php
    //include'connection.php';
    //include'functions.php';
    
    $apiOldNo=getApiNo($newrechid);
    $apiNewNo=$apiOldNo+1;
    
    $Api_Name="PP_API";									    	
    												
    $UpdateRechApi="UPDATE recharge SET API='$Api_Name', LapuNo='$Api_Name', API_NO='$apiNewNo' where Id='$newrechid'";
	mysqli_query($con,$UpdateRechApi);
	
	$token="b968b63cabae4d9aa26b4c252e13be11";
	$url="https://www.perfectpaisa.in/apiservice.asmx/Recharge?apiToken=b968b63cabae4d9aa26b4c252e13be11&mn=$mnum&op=$PP_ApiOpCode&amt=$amount&reqid=$newrechid&field1=''&field2=''";

	$curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://www.perfectpaisa.in/apiservice.asmx/Recharge?apiToken=b968b63cabae4d9aa26b4c252e13be11&mn=9897095971&op=4&amt=10&reqid=123123123&field1=\'%20\'&field2=\'%20\'',
    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);

    curl_close($curl);
    $response;
    
    $LLP_Order_Id=$response->reqid;
    $Api_Txn_Id=$response->apirefid;
    $Error_Code=$response->ec;
    $Status_Msg=$response->status;
	$Op_Ref_Id=$xml->field1;
    $Error_Msg=$response->remark;
    $Api_Bal=$response->balance;
    
    if($Error_Code=="1010")
	{
		$STATUS=="PENDING";
	}
	elseif($Error_Code=="1000")
	{
		$STATUS=="SUCCESS";
	}
	elseif($Error_Code=="1010" || $Error_Code=="1005" || $Error_Code=="1015")
	{
		$STATUS=="FAILED";
	}												
    
    if($STATUS=="SUCCESS")
    {
        $GetLapuOldBal="SELECT * FROM recharge WHERE LapuNo='$Api_Name' and status='SUCCESS' order by Id desc";
        $reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
        $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
        $LapuOldBal=$dataGetLapuOldBal['ApiBal'];
        
        $Remain_Bal=$LapuOldBal-$amount;
        $Diff_Bal=$Remain_Bal-$Api_Bal;
                                                        	  
        $quUpdateRechData="UPDATE recharge SET Transactionid='$Api_Txn_Id', Status='SUCCESS', Operatorid='$Op_Ref_Id', LapuOldBal='$LapuOldBal', Diff='$Diff_Bal', ApiBal='$Api_Bal' where Id='$newrechid'";
		mysqli_query($con,$quUpdateRechData);
																
		$quUpdatebal="UPDATE myapi SET LapuBal='$Api_Bal', LastUpdate='$date', OrderId='$newrechid' WHERE LapuNumber='$Api_Name'";
		mysqli_query($con,$quUpdatebal);
		    
													                                                    
			$success_msg="Recharge Successful !";
	        echo "<div class='msg-div'>
			    <form method='post'>
			        <h3>".$success_msg."</h3><br>
					<i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
				    <button type='submit' class='btnPopup'>OK</button><br><br>
                </form>
           </div>";
    }
    elseif($STATUS=="FAILED")
    {
        $autoFailedStatus=getAFS();
            
        $Printmsg="Failed";
            
        if($apiNewNo==2)
		{
	        include'rechFailed.php';											          
	    }
		else
		{
	        if($UsrPersApi2!="OFF" && $UsrPersApi2!="Ozzy_Api")
	        {
	            if($UsrPersApi2=="ON" || $UsrPersApi2=="By_Default")
		        {
		            if($UseApi1!="OFF" && $UseApi1!="PP_API")
		            {
		                $apiFile2="RechFile_".$UseApi1.".php";
		            }
		            else
		            {
		                $apiFile2="RechFile_".$UseApi2.".php";
		            }
	            }
		        else
		        {
		            $apiFile2="RechFile_".$UsrPersApi2.".php";
		        }
		        
				include $apiFile2;
		    }
		    else
		    {
	            if($autoFailedStatus!="Deactive")
		        {   
		            include'rechFailed.php';
	            }
		        else
		        {
		                $UpdateRechApibal="UPDATE recharge SET ReHit='1', AdminOpId='$Printmsg' WHERE Id='$newrechid'";
	    		        mysqli_query($con,$UpdateRechApibal);
			            
			            //$myresponse = array("status"=>"PENDING", "partnertid"=>"$partnerId", "orderid"=>"$newrechid", "opid"=>"$txnId");
					    //echo $myresult = json_encode($myresponse);
					
			            
		    	        $success_msg="Request Submitted Successfully !";
	                    echo "<div class='msg-div'>
				            <form method='post'>
				                <h3>".$success_msg."</h3><br>
    					        <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
		    			        <button type='submit' class='btnPopup'>OK</button><br><br>
    			    	    </form>
	        		     </div>";
	        		     
		            }
		        }
		    }
        }
        else
        {
            //$myresponse = array("status"=>"PENDING", "partnertid"=>"$partnerId", "orderid"=>"$newrechid", "opid"=>"$txnId");
			//echo $myresult = json_encode($myresponse);
			
					
            $success_msg="Request Submitted Successfully !";
	        echo "<div class='msg-div'>
			    <form method='post'>
			        <h3>".$success_msg."</h3><br>
    		        <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
		    		<button type='submit' class='btnPopup'>OK</button><br><br>
    		    </form>
	        </div>";
	        
        }
?>