<?php
session_start();



 $hostname = "localhost";
 $username = "u459483528_tiranga1";
 $password ="x]dNR:;se|K1";
 $database = "u459483528_tiranga1";

$conn = mysqli_connect($hostname, $username, $password, $database);

 date_default_timezone_set("Asia/Kolkata");
$date_time = date('d/m/Y g:i:s');
$date = date('d/m/Y');




if(isset($_GET['order_id'] )){
    $client_txn_id = $_GET['order_id'];
    $res = mysqli_query($conn,"SELECT * FROM upitxn WHERE order_id='$client_txn_id'");
    $dbdata = mysqli_fetch_assoc($res);
    $dbtxn_id = $dbdata['order_id'];
    $date = $dbdata['txn_date'];
    $key = "0f61e67c7465cc40d26726b6143d6731";     // you can get your key from https://merchant.upigateway.com/user/api_credentials
	
     $postData = array(
	 	"user_token"=> $key,
	 	"order_id"=> "$dbtxn_id",
        
	 );
	 $url = "https://shivajigateway.com/api/check-order-status";
	 $ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

// Execute cURL session and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Decode the JSON response
$responseData = json_decode($response, true);

// Check if the API call was successful
if ($responseData["status"] === "COMPLETED") {
         $txnStatus = $responseData["result"]["txnStatus"];
    $orderId = $responseData["result"]["orderId"];
    $status = $responseData["result"]["status"];
    $amount = $responseData["result"]["amount"];
    $date = $responseData["result"]["date"];
    $utr = $responseData["result"]["utr"];
	    
	            
	            
	            
    	        $sl= "SELECT * FROM upitxn WHERE order_id='$dbtxn_id'";
                $pres = mysqli_query($conn,$sl);
                $pdata = mysqli_fetch_assoc($pres);
                $puser = $pdata['userid'];
                // Select User 
                $ud = mysqli_query($conn,"SELECT * FROM usertable WHERE phone='$puser' ");
                $udata = mysqli_fetch_assoc($ud);
                
                $name = $udata['name'];
               
                // Update Balance if pending
                if($dbdata['status'] =="pending"){
                        $sql = "UPDATE `upitxn` SET `status`='success', `utr` = '$utr' WHERE order_id='$dbtxn_id'";
                		$q = mysqli_query($conn,$sql);
                		
                		$nbalance = $udata['walletamount'] + $amount;
                		$updatewallet = mysqli_query($conn,"UPDATE `usertable` SET `walletamount`='$nbalance' WHERE `phone`='$puser'");
                		if($updatewallet){
                		    
                		    
                		  echo "<script>
				  alert('Recharge Successfull');
				  window.location.replace('https://digitalsevaucl.online/admin/index.php');  
				</script>";
                		}
                		
                
                }else{
                    echo "<script>
				  alert('Already Added Balance');
				  window.location.replace('https://digitalsevaucl.online/admin/login');  
				</script>";
                }
               
        		
        		if(!isset($_SESSION)){
        			session_start();
        		 $_SESSION['username'] = $udata['username'];
                 $_SESSION['email'] = $udata['email'];
                 $_SESSION['mobile'] = $udata['mobile'];
                 $_SESSION['status'] = $udata['status'];
                 $_SESSION['usertype'] = $udata['usertype'];
		}
	    }else{
	       	  echo "<script>
				  alert('index Failed');
				  window.location.replace('https://digitalsevaucl.online/admin/index.php');  
				</script>";
	    }
	 }else{
	 	echo $response['msg'];
	 }

    



?>
