<?php
    include'connection.php';
    include'functions.php';
    
   	$apiKey="d1db51a3a6a8ef6078fd8bf623dd0008";
	$circle="Gujarat";
	$operator="Airtel";
	
	
	$url="https://www.mplan.in/api/plans.php?";

	$ch = curl_init();

    if (!$ch)
    {
        die("Couldn't initialize a cURL handle");
    }
    curl_setopt($ch, CURLOPT_URL,$url); // Target URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    
	$apiUrl="apikey=$apiKey&cricle=$circle&operator=$operator";
	
    curl_setopt($ch, CURLOPT_POSTFIELDS,$apiUrl);
    $result = curl_exec ($ch); 
    
    $result = json_decode($result,true);
    $records=$result["records"];
    
    //echo '<pre>';
    //print_r($records);
    //die();
    
    
    $TOPUP=$records["TOPUP"];
    $data3g4g=$records["3G/4G"];
    $Romaing=$records["Romaing"];
    $COMBO=$records["COMBO"];
    
    
    
    foreach($TOPUP as $key => $TOPUPval)
    {
            echo 'TOPUP: '.$key;
            $rs=$TOPUPval["rs"];
            $desc=$TOPUPval["desc"];
            $validity=$TOPUPval["validity"];
            
            echo "RS: ".$rs." || DESC: ".$desc." || Validity: ".$validity."<hr>";
    }
    
    
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    exit();
    $records = (array)json_decode($result[0]);
    $TOPUP = (array)json_decode($records[0]);
    $data = (array)json_decode($TOPUP[0]);
    echo $data[0]['rs'];
    
    
    echo $result[1]."<br>";
    /*
    echo $result['time']."<br>";
    
    echo $result => records;
    
    /*echo $result['operator']."<br><br>";
    $recordes  = $result['records']."<br><br>";
    $recordes = (array)json_decode($recordes);
    echo $recordes['rs']."<br>";
    echo $recordes['desc']."<br>";
    8?
*/
    
    //echo $url.$apiUrl;
    /*while($result)
    {
        echo $result['rs']." ".$result['desc']."<br><br>";
    }
    */
    exit();
    
    $status1 = $result['status'];
    $errorMsg = $result['errorMessage'];   
    $msg = $result['response'];   
    $txnId = $result['tnx_id'];   
    $LapuNo = $result['lapu_no'];   
    $LapuBal = $result['balance'];   
	$roffer = $result['roffer'];  
	$romingStatus = $result['is_roaming'];   
	
	if($romingStatus=="true")
	{
	    $roamingMsg="Y";
	}
	else
	{
	    $roamingMsg="N";
	}
												
	$updateLapu="UPDATE recharge SET LapuNo='$LapuNo', Roaming='$roamingMsg' where Id='$newrechid'";
	mysqli_query($con,$updateLapu);
    
    if($status1=="SUCCESS" || $status1=="success")
	{
	    $GetLapuOldBal="SELECT * FROM recharge WHERE LapuNo='$LapuNo' 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', R_Offer='$roffer', Operatorid='$txnId', 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($status1=="FAILED" || $status1=="failure")
	{
	    $autoFailedStatus=getAFS();
	    
	    if(chk_msg($msg,'Noreco')==6  || chk_msg($msg,'BadMSISDN')==9 || chk_msg($msg,'SubsNot')==7)
        {
            $Printmsg ="Invalid Number Or Operator !";
            $flag=1;
        }
        elseif(chk_msg($msg,'NoPlans')==7  || chk_msg($msg,'TranFail')==8 || chk_msg($msg,'Cardgorup')==9)
		{
			$Printmsg ="Invalid Amount !";
            $flag=1;
		}
		elseif(chk_msg($msg,'PrimePlan')==9)
		{
		    $Printmsg ="You can only select other than prime plan for recharge.	";
            $flag=1;
		}
		elseif(chk_msg($msg,'sessExpi')==8)
		{
		    $Printmsg ="Lapu Session Expired !";
            $flag=0;
		}
		elseif(chk_msg($msg,'insufbalan')==10)
		{
		    $Printmsg ="Insufficiant Balance !";
            $flag=0;
		}
		else
		{
		    $Printmsg = $msg;
            $flag=0;
		}
														      
		if($flag==1 || $apiNewNo==2)
		{
		    include'rechFailed.php';											          
		}
		else
		{
		    if($UsrPersApi2!="OFF" && $UsrPersApi2!="My_API")
		    {
		            if($UsrPersApi2=="ON" || $UsrPersApi2=="By_Default")
		            {
		                if($UseApi1!="OFF" && $UseApi1!="My_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);
			
			        $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>";
		        }
		
		    }
		}
	}

?>