<?php

require_once 'includes/provider_auth.php';
require_once '../config/constants.php';

$plan_id = $_GET['plan_id'] ?? 0;

?>

<?php include '../admin/partials/layouts/layoutTop.php' ?>

<div class="dashboard-main-body">

    <div class="card">

        <div class="card-body text-center p-48">

            <h4 class="mb-16">Subscription Checkout</h4>

            <button id="payBtn" class="btn btn-primary px-48 py-12 radius-8">
                Proceed to Payment
            </button>

        </div>

    </div>

</div>

<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script>

    $("#payBtn").click(function () {

        $.post(
            "<?php echo BASE_URL ?>api/payments/create-order.php",
            { plan_id: "<?php echo $plan_id ?>" },
            function (res) {

                if (res.status !== "success") {
                    alert(res.message);
                    return;
                }

                let options = {

                    key: res.data.key,
                    amount: res.data.amount * 100,
                    currency: "INR",
                    name: "QwikFix",
                    description: "Subscription Payment",
                    order_id: res.data.order_id,

                    handler: function (response) {

                        $.post(
                            "<?php echo BASE_URL ?>api/payments/verify-payment.php",
                            response,
                            function (r) {

                                if (r.status === "success") {
                                    window.location = "payment-success.php";
                                } else {
                                    alert(r.message);
                                }

                            },
                            'json'
                        );

                    }

                };

                let rzp = new Razorpay(options);
                rzp.open();

            },
            'json'
        );

    });

</script>

<?php include '../admin/partials/layouts/layoutBottom.php' ?>