<!doctype html>
<html lang="en">
    <head>
        {block name='head'}
            {include file='_partials/head.tpl'}
        {/block}
        <link rel="canonical" href="/dashboard/authorization/">
    </head>
<body>

    <div class="form-container">
        <form id="payment-form">
            <div class="form-header">
                <h1 class="Header-companyName">{$site_title}</h1>
                <h2 class="Header-purchaseDescription">{$title}</h2>
            </div>
            <div class="form-body">
                <script src="https://api.ravepay.co/flwv3-pug/getpaidx/api/flwpbf-inline.js"></script>
                <button class="pay_btn" type="button" onClick="payWithRave()">Pay {$price_formatted}</button>
                <a class="back_to_order" href="/dashboard/order/{$order.id}">Back to Order</a>
            </div>
        </form>
    </div>

    <script>window.jQuery || document.write('<script src="/static/common/libs/jquery/3.2.1/jquery.min.js"><\/script>')</script>
    {block name='javascript_bottom'}
        {include file="_partials/javascript.tpl" javascript=$javascript.bottom}
    {/block}

    <script type="text/javascript">
        const API_publicKey = '{$publicKey}';

        document.addEventListener("DOMContentLoaded", payWithRave());

        function payWithRave() {
            var x = getpaidSetup({
                PBFPubKey: API_publicKey,
                customer_email: '{$payee_email}',
                amount: '{$price}',
                currency: '{$currency}',
                txref: '{$order.id}',
                onclose: function() {
                },
                callback: function(response) {
                    if (
                        response.respcode == "00" ||
                        response.respcode == "0"
                    ) {
                        $.ajax({
                            method: "POST",
                            url: '{$callback_url}',
                            data: { 
                                handle: 'order',
                                nodeId: '{$order.id}'
                            }
                        })
                        .done(function(resp) {
                            if(resp.success) {
                                window.location = '{$success_url}';
                            } else {
                                window.location = '{$decline_url}';
                            }
                        });
                    } else {
                        window.location = '{$decline_url}';
                    }

                    x.close(); // use this to close the modal immediately after payment.
                }
            });
        }
    </script>
</body>
</html>