<?php
require_once('../database/header.php');
?>


<div class="main-panel">
<div class="content-wrapper">

<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3 bg-primary">
<h6 class="m-0 font-weight-bold text-white">交易状态</h6>
</div>
<div class="card-body">


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Include your database configuration file
require_once('../database/config.php'); // Make sure this path is correct

// Check if $_POST contains 'order_id'
if (isset($_POST['order_id'])) {
// Get the order_id from the POST request
$order_id = $_POST['order_id'];

$api_key = $userdata['api_key']; // Replace with your actual API key
$domain = $_SERVER['SERVER_NAME'];
// Step 2: Set the API URL using the domain name
$apiUrl = 'https://'.$domain.'/api/order-status.php'; // Dynamic domain URL

// Prepare the data to send in the POST request
$data = [
'api_key' => $api_key,
'orderid' => $order_id, // Use the received orderid directly here
];

// Initialize cURL session
$ch = curl_init();

// Set the URL for the API call
curl_setopt($ch, CURLOPT_URL, $apiUrl);

// Set cURL options for the POST request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Return the response as a string
curl_setopt($ch, CURLOPT_POST, true);  // Indicate that it's a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  // Attach data as a query string

// Set the Content-Type to application/x-www-form-urlencoded
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);

// Execute the cURL request and capture the response
$response = curl_exec($ch);
//echo $response;
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Decode the API response
$apiResponse = json_decode($response, true);

// Check if txn_status is Success and api_txn_id is available
if (isset($apiResponse['status']) && $apiResponse['status'] === 'SUCCESS') {
$api_txn_id = $apiResponse['data']['txn_id'];
$txn_status = $apiResponse['data']['txn_status'];
$utr_number = $apiResponse['data']['utr_number']; // UTR number is available here
$amount = $apiResponse['data']['amount']; // Transaction amount
} else {
$txn_status = "Failed";
$api_txn_id = "N/A";
$utr_number = "N/A";
$amount = "N/A";
}
}

// Close cURL session
curl_close($ch);
} else {
echo "未收到订单 ID！";
exit;
}
?>






<?php if ($txn_status === "SUCCESS"): ?>
<div class="status">
<p class="info"><span>交易状态:</span> <?php echo $txn_status; ?></p>
<p class="info"><span>交易编号 (Txn ID):</span> <?php echo $api_txn_id; ?></p>
<p class="info"><span>订单编号 (Order ID):</span> <?php echo $order_id; ?></p>
<p class="info"><span>UTR 编号:</span> <?php echo $utr_number; ?></p>
<p class="info"><span>金额:</span> <?php echo $amount; ?></p>
</div>
<?php else: ?>
<div class="status failed">
<p class="info"><span>交易状态:</span> <?php echo $txn_status; ?></p>
<p class="info"><span>交易编号 (Txn ID):</span> <?php echo $api_txn_id; ?></p>

<p class="info"><span>订单编号 (Order ID):</span> <?php echo $order_id; ?></p>
<p class="info"><span>UTR 编号:</span> <?php echo $utr_number; ?></p>
<p class="info"><span>金额:</span> <?php echo $amount; ?></p>
</div>
<?php endif; ?>

<a href="index.php" class="button">返回</a>
</div>

</div>
</div>

<?php
require_once('../database/footer.php');
?>
