<?php $checkoutId = $_GET['checkout_id']; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Confirming Payment</title>
    <style>
        body { font-family: Arial; text-align: center; padding: 50px; }
        .loader { font-size: 20px; }
    </style>
</head>
<body>

<h2>📲 Confirm Payment</h2>
<p class="loader">Waiting for M-Pesa confirmation...</p>

<script>
let attempts = 0;
const maxAttempts = 20; // ~60 seconds

const interval = setInterval(() => {
    attempts++;

    fetch("status.php?checkout_id=<?= $checkoutId ?>")
        .then(res => res.json())
        .then(data => {
            if (data.status === "paid" || data.status === "failed") {
                window.location.href = "result.php?status=" + data.status;
                clearInterval(interval);
            }
        });

    if (attempts >= maxAttempts) {
        window.location.href = "result.php?status=pending";
        clearInterval(interval);
    }
}, 2000);
</script>

</body>
</html>
