<div class="form-container">
    <div id="payment-form">
        <div class="form-header">
            <h1 class="Header-companyName">{$siteName}</h1>
            <h2 class="Header-purchaseDescription">{$title}</h2>
        </div>
        <div class="form-body">
            <div class="js_loader flat-spinner" style="display:none;"></div>
            <div id="paypal-button-container"></div>
            <a class="back_to_order" href="/dashboard/add-payment/{$add_payment.id}">Back to Additional Payment</a>
        </div>
    </div> 
</div> 

<script
    src="https://www.paypal.com/sdk/js?client-id={$paypal_client}&currency={$currency}">
</script>

<script type="text/javascript">
    paypal.Buttons({
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '{$price}'
                    },
                }],
                application_context: {
                    shipping_preference: 'NO_SHIPPING',
                } 
            });
        }, 
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(payment) {
                $('#paypal-button-container').hide();
                $('.js_loader').show();
                $.ajax({
                    method: "POST",
                    url: '{$callback_url}',
                    data: { 
                        handle: 'add-payment',
                        nodeId: '{$add_payment.id}',
                        currency: '{$currency}',
                        payment_method: '{$payment_method}',
                        paymentId: payment.id,
                        payerId: payment.payer.payer_id
                    }
                })
                .done(function(response) {
                    if(response.success) {
                        window.location = '{$success_url}';
                    } else {
                        window.location = '{$decline_url}';
                    }
                });
            });
        }
    }).render('#paypal-button-container');
</script>