<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve form data
    $amount = $_POST['gtp_Amount'] ?? null;
    $currency = $_POST['gtp_Currency'] ?? null;
    $name = $_POST['gtp_PayerName'] ?? null;
    $orderId = $_POST['gtp_OrderId'] ?? null;
    $reason = $_POST['gtp_TransDetails'] ?? null;
    $customerCode = $_POST['gtp_CustomerCode'] ?? null;
    $gtPayURL = $_POST['GTPayURL'] ?? null;
    $message = $_POST['message'] ?? null; // Now checking 'message' field

    // Generate unique transaction ID
    $transactionId = uniqid('txn_');

    // Create a secure hash (example using SHA256)
    $hashKey = "BFF5EE8EB65E33C9CF02170FDC40A6EE"; // Replace with your actual secret key
    $secureHash = hash('sha256', $transactionId . $amount . $currency . $hashKey);

    // Generate success and failure links
    $successLink = "https://rainscriptservers.com/payments/payment_success.php?transaction_id=$transactionId&message=1&secure_hash=$secureHash";
    $failureLink = "https://rainscriptservers.com/payments/payment_failed.php?transaction_id=$transactionId&message=0&secure_hash=$secureHash";

    // Check for the 'message' response
    if ($message === '1') {
        // Redirect to success URL if message is 1
        header("Location: $successLink");
    } else {
        // Redirect to failure URL for any other case
        header("Location: $failureLink");
    }
    exit;
} else {
    // Redirect to failure URL if request method is not POST
    $failureLink = "https://rainscriptservers.com/payments/payment_failed.php?transaction_id=unknown&message=0";
    header("Location: $failureLink");
    exit;
}
