<?php
    
    require'../connection.php';
    require'../functions.php';
    
    $IP = $_SERVER['REMOTE_ADDR'];
    
	$QuGetUser = "SELECT Api_IP,Status,ApiName FROM api WHERE Api_IP='$IP'";
	$ReGetUser = mysqli_query($con,$QuGetUser);
	$GetUser   = mysqli_num_rows($ReGetUser);
	$dataUser  = mysqli_fetch_array($ReGetUser);
	$ApiStatus = $dataUser['Status'];
	$ApiName   = $dataUser['ApiName'];
	
	if($GetUser!==1)
	{    
		$myresponse = array("bal"=>"API Not Found For This IP Address !");
		echo $myresult = json_encode($myresponse);
	}
	elseif($ApiStatus!="ACTIVE")
	{    
		$myresponse = array("bal"=>"API Is DEACTIVE !");
		echo $myresult = json_encode($myresponse);
	}
	else
	{
	    $status    = $_GET['status'];                                            
	    $ApiBal    = $_GET['balance'];
        $respoMsg  = $_GET['remark'];   
        $txnId     = $_GET['field1'];   
        $errCode   = $_GET['ec'];   
        $OrderID   = $_GET['reqid'];
        $newrechid = $OrderID;
        
        $result = $status.", ".$ApiBal.", ".$respoMsg.", ".$txnId.", ".$errCode.", ".$OrderID;
        $quApiHit="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$ApiName','CALLBACK','$result')";
	    mysqli_query($con,$quApiHit);
	    
	    $quRech   = "SELECT * FROM recharge WHERE Id='$newrechid'";
	    $reRech   = mysqli_query($con,$quRech);
	    $dataRech = mysqli_fetch_array($reRech);
	    $userid   = $dataRech['Userid'];
	    
	    $dataUser = getUser($userid);
	    
	    if($status=="SUCCESS")
        {
            require'../LinkFile/rechSuccess.php';
        }
        else if($status=="FAILED" || $status=="REFUND" )
        {
            require'../LinkFile/rechFailed.php';
        }
        else if($status=="PENDING")
        {
            require'../LinkFile/rechPending.php';
        }
	
	}

?>