<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>Secure Payment | EMI CREDITS</title>

    <script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>

    <style>
        body {
            margin: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f5f7fb;
            font-family: Arial, sans-serif;
        }

        .payment-box {
            text-align: center;
            background: #ffffff;
            padding: 30px;
            border-radius: 14px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
        }

        .loader {
            width: 40px;
            height: 40px;
            margin: 0 auto 18px;
            border: 4px solid #e5e7eb;
            border-top-color: #2563eb;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        .message {
            color: #374151;
            font-size: 15px;
        }

        .error {
            color: #dc2626;
        }
    </style>
</head>

<body>

    <div class="payment-box">

        <div class="loader"></div>

        <div
            id="payment-message"
            class="message"
        >
            Redirecting to secure payment...
        </div>

    </div>


    <script>

        document.addEventListener('DOMContentLoaded', async function () {

            const paymentSessionId =
                @json($paymentSessionId ?? null);

            const message =
                document.getElementById('payment-message');


            /*
            |--------------------------------------------------------------------------
            | Payment Session Check
            |--------------------------------------------------------------------------
            */

            if (!paymentSessionId) {

                message.classList.add('error');

                message.innerText =
                    'Payment session is missing. Please try again.';

                return;
            }


            /*
            |--------------------------------------------------------------------------
            | Cashfree SDK
            |--------------------------------------------------------------------------
            */

            try {

                const cashfree = Cashfree({
                    mode: "sandbox"
                });


                /*
                |--------------------------------------------------------------------------
                | Open Cashfree Checkout
                |--------------------------------------------------------------------------
                */

                await cashfree.checkout({

                    paymentSessionId: paymentSessionId,

                    redirectTarget: "_self"

                });

            } catch (error) {

                console.error(
                    'Cashfree Checkout Error:',
                    error
                );

                message.classList.add('error');

                message.innerText =
                    'Unable to open payment gateway. Please try again.';

            }

        });

    </script>

</body>

</html>