<?php
session_start();
include('../includes/config.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $url = "https://shivajigateway.in/api/create-order"; // Replace with the actual API endpoint

    $data = array(
        "customer_mobile" => $_POST["phone"],
        "user_token" => "0f61e67c7465cc40d26726b6143d6731",
        "amount" => $_POST["amount"],
        "order_id" => $_POST["order_id"],
        "redirect_url" => "https://".$_SERVER['SERVER_NAME']."/HBconnect4in1/regresponse.php",
    );
// Store order_id in a session variable
        $_SESSION['order_id'] = $_POST['order_id'];
         $_SESSION['customer_mobile'] = $_POST['phone'];

    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => http_build_query($data),
        ),
    );

    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    if ($response === FALSE) {
        die('Error occurred while making the API request');
    }

    $result = json_decode($response, true);

    if ($result['status'] === true) {
        // Success Response

        
        // Delay the redirect by 2 seconds
        sleep(2);

        // Redirect to the payment URL
        header("Location: " . $result['result']['payment_url']);
        exit();
    } else {
        // Failed Response
        echo "Error from API: " . $result['message'] . "\n";
    }
}
?>

<html>
<head>
    <title>Gateway Check Out Page</title>
</head>
<body>
    <center><h1>Please do not refresh this page...</h1></center>
</body>
</html>
