<?php
require('connection.inc.php');
require('security.php');

$order_id = $_GET['order_id'] ?? '';
$payment_id = $_GET['payment_id'] ?? '';
$status = $_GET['payment_status'] ?? '';

if ($order_id && $payment_id && $status === 'Credit') {
    $payment_datetime = date('Y-m-d H:i:s'); // Indian time
    $stmt = $conn->prepare("UPDATE orders SET payment_id = ?, payment_status = 'Paid', payment_datetime = ? WHERE order_id = ?");
    $stmt->bind_param("sss", $payment_id, $payment_datetime, $order_id);
    $stmt->execute();
    $stmt->close();

    header("Location: orderconfirm.php?order_id=$order_id");
    exit;
} else {
    echo "
    <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
    <script>
       document.addEventListener('DOMContentLoaded', function() {
        Swal.fire({
            icon: 'error',
            title: 'Payment Failed',
            text: 'Your payment was cancelled or failed. You will be redirected to the homepage.',
            confirmButtonText: 'OK'
        }).then(() => {
            window.location.href = 'index.php'; // Redirect to index page
        });
        });
    </script>
    ";
}


?>
