<?php
    //include'connection.php';
    //include'functions.php';
    
    $apiOldNo=getApiNo($newrechid);
    $apiNewNo=$apiOldNo+1;
    										    	
    
    $UpdateRechApi="UPDATE recharge SET API='Rechbyesell_Api', LapuNo='Rechbyesell_Api', API_NO='$apiNewNo' where Id='$newrechid'";
	mysqli_query($con,$UpdateRechApi);
	
	$apiUsrId=3334;
    $apiToken="e696918427bc39de4a0761dbddd35ad9";
    //$url="https://api.rechargebuysale.com/ExchangeTransactionAPI?UserID=$apiUsrId&Token=$apiToken&Account=$mnum&Amount=$amount&SPKey=$AmbikaOpCode&APIRequestID=$newrechid"; 
    $url="https://api.rechargebuysale.com/ExchangeTransactionAPI?UserID=$apiUsrId&Token=$apiToken&Account=$mnum&Amount=$amount&SPKey=$RechbesellOpCode&APIRequestID=$newrechid&Format=1"; 
	$request_timeout = 60; // 60 seconds timeout
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	$curl_error = curl_errno($ch);
	$getserver= curl_getinfo($ch);
    
	curl_close($ch);
	$data2 = (array)json_decode($output);
    
    $status1 = $data2['status'];                                            
	$RechbyesellBal=$data2['bal'];
    $msg = $data2['msg'];   
    $txnId = $data2['opid'];   
    $OrderID = $newrechid;
    $partnerId= $data2['rpid'];; 
	
	    if($status1=="SUCCESS" || $status1=="2")
        {
            $GetLapuOldBal="select * from recharge where LapuNo='Rechbyesell_Api' and status='SUCCESS' order by Id desc";
            $reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
            $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
            $LapuOldBal=$dataGetLapuOldBal['ApiBal'];
            $commAmount=$amount*$Ozzy_Comm/100;
            $debitAmount=$amount-$commAmount;
            $remaiBal=$LapuOldBal-$debitAmount;
            if(empty($RechbyesellBal) || $RechbyesellBal==0)
            {
                $RechbyesellBal=$remaiBal;
            }
            $LapuDiff=$RechbyesellBal-$remaiBal;                                  	  
                                                        	  
		    $queryUpadateOrderId="UPDATE recharge SET Transactionid='$orderId', Status='SUCCESS', Operatorid='$txnId', LapuOldBal='$LapuOldBal', Diff='$LapuDiff', API='Rechbyesell_Api', LapuNo='Rechbyesell_Api', ApiBal='$RechbyesellBal' where Id='$newrechid'";
			mysqli_query($con,$queryUpadateOrderId);
																
		    $quUpdatebal="UPDATE myapi SET LapuBal='$RechbyesellBal', LastUpdate='$date', OrderId='$newrechid' WHERE LapuNumber='Rechbyesell_Api'";
		    mysqli_query($con,$quUpdatebal);
		    
			//$myresponse = array("status"=>"SUCCESS", "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 if($status1=="FAILED" || $status1=="3")
        {
        
            $autoFailedStatus=getAFS();
            
            $Printmsg="Failed";
            
            if($apiNewNo==2)
		    {
		        include'rechFailed.php';
		       
		    }
		    else
		    {
		        $flagFailed=0;
		        if($UsrPersApi2!="OFF" && $UsrPersApi2!="Rechbyesell_Api")
		        {
		            if($UsrPersApi2=="ON" || $UsrPersApi2=="By_Default")
		            {
		                if($UseApi1!="OFF" && $UseApi1!="Rechbyesell_Api")
		                {
		                    $apiFile2="RechFile_".$UseApi1.".php";
		                }
		                elseif($UseApi2!="OFF" && $UseApi2!="Rechbyesell_Api")
		                {
		                    $apiFile2="RechFile_".$UseApi2.".php";
		                }
		                else
		                {
		                    $flagFailed=1;
		                }
		            }
		            else
		            {
		                $apiFile2="RechFile_".$UsrPersApi2.".php";
		            }
				    if($flagFailed==0)
				    {
			    	    include $apiFile2;
				    }
		        }
		        else
		        {
		            $flagFailed=1;
		        }
		    
		        if($flagFailed==1)
		        {
		           
		            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);
			$TEST = "Test5";
				
            $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>";
	        
        }
    
?>