<?php
// Check if order_id is provided in the GET request
$order_id = isset($_GET['order_id']) ? $_GET['order_id'] : '1234561724311420'; // Default order_id if none provided

// API endpoint URL
$url = "https://tiraxon.in/upi/api/check-order-status";

// POST data
$postData = array(
    "user_token" => "76b75008df0aa23baeecce16ab96b5fb",
    "order_id" => $order_id // Use the provided or default order_id
);

// 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 json_encode([
        "status" => "error",
        "message" => "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, prepare the response data
    $responseArray = [
        "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 the data as a JSON response
    echo json_encode($responseArray);
} else {
    // API call failed, return the error message as JSON
    echo json_encode([
        "status" => "failed",
        "message" => $responseData["message"]
    ]);
}
?>