<?php
    //include'connection.php';
    //include'functions.php';
    
    $apiOldNo=getApiNo($newrechid);
    $apiNewNo=$apiOldNo+1;
    
    $UpdateRechApi="UPDATE recharge SET API='Dizi_Kwik', LapuNo='Dizi_Kwik', API_NO='$apiNewNo' where Id='$newrechid'";
	mysqli_query($con,$UpdateRechApi);
											    		
	
    $url="https://www.kwikpayapi.com/Kwpapi/RechargeAPI.aspx?MobileNo=9897095971&APIKey=agfbEchMMunPC8xXeJ3gTbcwq9XtHJoUgKH&REQTYPE=RECH&REFNO=$newrechid&SERCODE=$Dizi_KwikOpCode&CUSTNO=$mnum&AMT=$amount&STV=$stv&RESPTYPE=JSON";
    $curl = curl_init();
	curl_setopt_array($curl, array(
	CURLOPT_URL => $url,
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 0,
	CURLOPT_FOLLOWLOCATION => false,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "GET",
	));
	
	$err = curl_error($curl);							

    $result = curl_exec ($curl); 
    $result = (array)json_decode($result);
    curl_close($curl);
    
    $StatusCode = $result['STATUSCODE'];   
    $ReqSttaus = $result['STATUSMSG'];   
    $ApiBal = $result['BALANCE'];
    $MeOrderId = $result['REFNO'];
    $ApiTxnId = $result['TRNID'];
    $RespoStatus = $result['TRNSTATUS'];
    $OpRefId = $result['OPRID'];
    
    $LapuBal=$ApiBal;
    
    
        if($RespoStatus==1)
        {
            $GetLapuOldBal="SELECT * FROM recharge WHERE LapuNo='Dizi_Kwik' AND status='SUCCESS' ORDER BY Id DESC";
		    $reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
		    $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
		    $LapuOldBal=$dataGetLapuOldBal['ApiBal'];
		    $LapuOldBalRow=mysqli_num_rows($reGetLapuOldBal);
													
		    if($LapuOldBalRow==0 || $LapuOldBal==0 || empty($LapuOldBal))
		    {
		        $LapuOldBal=0;
			    $remaiBal=$LapuOldBal-$amount;
			    $LapuDiff = $LapuBal-$remaiBal;
		    }
		    else
		    {
		        $remaiBal=$LapuOldBal-$amount;
			    $LapuDiff = $LapuBal-$remaiBal;	
		    }
		    
		    $UpadateStatus="UPDATE recharge SET Status='SUCCESS', Operatorid='$OpRefId', LapuOldBal='$LapuOldBal', Diff='$LapuDiff', ApiBal='$LapuBal' WHERE Id='$newrechid'";
		    mysqli_query($con,$UpadateStatus);
																
		    $quUpdatebal="UPDATE myapi SET LapuBal='$LapuBal', LastUpdate='$date', OrderId='$newrechid' WHERE LapuNumber='$LapuNo'";
		    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($RespoStatus==2 || $RespoStatus==3 || $RespoStatus==5 || $ReqSttaus!="Success")
        {
            $autoFailedStatus=getAFS();
            if($RespoStatus==2)
            {
               $Printmsg="Invalid Number Or Operator !"; 
            }
            elseif($RespoStatus==3)
            {
               $Printmsg="System Failed !"; 
            }
            elseif($RespoStatus==5)
            {
               $Printmsg="Refunded !"; 
            }
            else
            {
                $Printmsg="Failed Test!";
            }
            
            if($apiNewNo==2)
		    {
		        include'rechFailed.php';											          
		    }
		    else
		    {
		        $flagFailed=0;
		        if($UsrPersApi2!="OFF" && $UsrPersApi2!="Dizi_Kwik")
		        {
		            if($UsrPersApi2=="ON" || $UsrPersApi2=="By_Default")
		            {
		                if($UseApi1!="OFF" && $UseApi1!="Dizi_Kwik")
		                {
		                    $apiFile2="RechFile_".$UseApi1.".php";
		                }
		                elseif($UseApi2!="OFF" && $UseApi2!="Dizi_Kwik")
		                {
		                    $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);
			
		    	        $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
        {
                        $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>";
        }
?>