<?php
session_start();
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';

$status = $_POST['status'];
    $txnid = $_POST['order_id'];
    $pid = $_POST['remark1'];



$date=date();
$orid= $_POST['order_id'];
  
if ($status=='SUCCESS') {
    
    $url = $protocol . $_SERVER['HTTP_HOST'] . "/api/check-order-status";

// POST data
$postData = array(
    "user_token" => "64f06fc9c26552c69d7ed4eba3459e1e",
    "order_id" => $txnid,
);

// Initialize cURL session
$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") {
    // API call was successful
    // Access the response data as needed
    $txnStatus = $responseData["result"]["txnStatus"];
    $orderId = $responseData["result"]["orderId"];
    $status = $responseData["result"]["status"];
    $amount = $responseData["result"]["amount"];
    $date = $responseData["result"]["date"];
    $utr = $responseData["result"]["utr"];

    echo "Transaction Status: $txnStatus<br>";
    echo "Order ID: $orderId<br>";
    echo "Status: $status<br>";
    echo "Amount: $amount<br>";
    echo "Date: $date<br>";
    echo "UTR: $utr<br>";

    
    
    
    
    
    
   echo "Amirul";
$servername = "localhost";
$username = "nuahkbzh_upi";
$password = "nuahkbzh_upi";
$dbname = "nuahkbzh_upi";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `transactions` ( `userid`, `order_id`, `txn_id`, `amount`, `status`, `trans_date`) VALUES ( '$pid', '$orid', '$txnid', '$amount', '$status', '$date')";
$sql2 = "SELECT * FROM `userdatas` WHERE `playerid`='$pid'";
$result = $conn->query($sql2);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
   $totalcoin=$row['totalcoin'];
   $playcoin=$row['playcoin'];
  }
  $newcoin=$totalcoin+$amount;
 

if ($conn->query($sql) === TRUE) {
    
  $sql3 = "UPDATE `userdatas` SET `totalcoin` = '$newcoin', `playcoin` = '$newcoin' WHERE `playerid` = '$pid'";
$result1 = $conn->query($sql3);
if($result1){
    echo 87943875;
   // header("Location:" $protocol . $_SERVER['HTTP_HOST'] . "/success.php");
die();
}
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}  }}else{
    echo 8789765;
//header("Location:" $protocol . $_SERVER['HTTP_HOST'] . "/success.php");
die();
}

?>