<?php
    
    include'../connection.php';
    include'../functions.php';
    
    // Get the protocol (http or https)
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';

    // Get the host name
    $host = $_SERVER['HTTP_HOST'];

    // Get the request URI, which includes the path and query string
    $request_uri = $_SERVER['REQUEST_URI'];

    // Construct the complete URL
    $complete_url = $protocol . '://' . $host . $request_uri;
    
    
    $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'];
	$api       = $dataUser['ApiName'];
	$LapuApi   = $api;
	
	$quApiHit="INSERT INTO api_hit (Date,Time,TxnId,ApiName,Type,Disc) VALUES ('$date','$time','$newrechid','$api','CALLBACK','$complete_url')";
	mysqli_query($con,$quApiHit);
    
	if($GetUser!=1)
	{    
		$myresponse = array("bal"=>"Unknown 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;
        $Printmsg  = $respoMsg;
        
	    $quRech   = "SELECT * FROM recharge WHERE Id='$newrechid'";
	    $reRech   = mysqli_query($con,$quRech);
	    $dataRech = mysqli_fetch_array($reRech);
	    $userid   = $dataRech['Userid'];
	    
	    $dataUser = getUser($userid);
	    
	    $quWallet    = "SELECT * FROM wallet WHERE TxnId='$newrechid'";
	    $reWallet    = mysqli_query($con,$quWallet);
	    $dataWallet  = mysqli_fetch_array($reWallet);
	    $DebitAmount = abs($dataWallet['Txn']);
	    
	    if($status=="SUCCESS")
        {
            include'../LinkFile/rechSuccess.php';
        }
        else if($status=="FAILED" || $status=="REFUND" )
        {
            include'../LinkFile/rechFailed.php';
        }
        else if($status=="PENDING")
        {
            include'../LinkFile/rechPending.php';
        }
	
	}

?>