<?php
if (isset($_GET['amount'])) {
    // URL of the PHP page
    $url = 'https://tiraxon.in/upi/api/create-order';

    // Get the amount from the URL query parameter
    $amount = $_GET['amount'];

    // Check if order_id is provided in the GET request
    $order_id = isset($_GET['order_id']) ? $_GET['order_id'] : '123456' . time();

    // Data to be sent in the POST request
    $data = array(
        'customer_mobile' => '1234567890',
        'user_token' => '76b75008df0aa23baeecce16ab96b5fb',
        'amount' => $amount,
        'order_id' => $order_id, // Use the provided or generated order_id
        'redirect_url' => 'https://tiraxon.in/upi/php/success.php',  // Redirect here after payment
        'remark1' => 'test1',
        'remark2' => 'test2',
    );

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

    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute cURL session and store 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
    $jsonResponse = json_decode($response, true);

    // Check if decoding was successful
    if ($jsonResponse !== null) {
        // Redirect the user to the payment URL
        $paymentUrl = $jsonResponse['result']['payment_url'];

        // Redirect to the payment URL
        echo "<script>
                window.location.href = '$paymentUrl';
              </script>";
        exit;
    } else {
        echo 'Failed to decode JSON response.';
    }
} else {
    echo 'Amount not provided. Please include the "amount" parameter in the URL.';
}
?>