chargeafter-rnd_chargeafter-plugin-for-nopcommerce/Nop.Plugin.Payments.ChargeAfter/Views/Checkout/CheckoutScript.cshtml
@model CheckoutModel

<!script type="text/javascript">
    var startCheckout = function () {
        let caData = {
            consumerDetails: {
                firstName: "@(Model.Customer.FirstName)",
                lastName: "@(Model.Customer.LastName)",
                email: "@(Model.Customer.Email)",
                mobilePhoneNumber: "@(Model.Customer.Phone)",
                shippingAddress: {
                    line1: "@(Model.Customer.ShippingAddressLine1)",
                    line2: "@(Model.Customer.ShippingAddressLine2)",
                    city: "@(Model.Customer.ShippingAddressCity)",
                    zipCode: "@(Model.Customer.ShippingAddressZipCode)",
                    state: "@(Model.Customer.ShippingAddressState)"
                },
                billingAddress: {
                    line1: "@(Model.Customer.BillingAddressLine1)",
                    line2: "@(Model.Customer.BillingAddressLine2)",
                    city: "@(Model.Customer.BillingAddressCity)",
                    zipCode: "@(Model.Customer.BillingAddressZipCode)",
                    state: "@(Model.Customer.BillingAddressState)"
                }
            },
            cartDetails: {
                items: [@foreach(var item in Model.Items) {
                    @Html.Raw("{ name: \"" + item.Name + "\", price: " + item.UnitPrice + ", sku: \"" + item.Sku + "\", quantity: " + item.Quantity + ", leasable: " + ( item.Leasable ? "true" : "false" ) + ( item.Warranty != null ? ", warranty: { name: \"" + item.Warranty.Name + "\", price: " + item.Warranty.Price + ", sku: \"" + item.Warranty.Sku + "\" }" : "" ) + " },")
                }],
                @if(Model.DiscountItems.Count > 0)
                {
                    @Html.Raw("discounts: [")
                    @foreach(var item in Model.DiscountItems) {
                        @Html.Raw("{ name: \"" + item.Name + "\", amount: " + item.Amount + "},")
                    }
                    @Html.Raw("],")
                }
                taxAmount: @(Model.TotalTaxAmount),
                shippingAmount: @(Model.TotalShippingAmount),
                totalAmount: @(Model.TotalAmount)
            },
            callback: function (token, data, error) {
                if (error) {
                    if (error.code != "BACK_TO_STORE" && error.code != "CONSUMER_CANCELLED") {
                        returnError(error.message);
                    }
                } else if (token) {
                    returnSuccess(token, data);
                }
            },
        };

        ChargeAfter.checkout.present(caData);
    }

    function returnSuccess(token, data) {
        console.log('Set confirmation token request: ' + token);
        createForm('ca_form_success', '@Url.RouteUrl(Defaults.CheckoutPlaceRouteName)', token, data);
    }

    function returnError(msg) {
        var errorEl = document.getElementById('chargeafter-payment-error');
        if (errorEl) {
            errorEl.innerHTML = msg;
        }
    }

    function createForm(id, action, ca_token, data) {
        if (window.caloader) {
            window.caloader.activate();
        }

        $.post(action, { ca_token, data })
            .done(function (response) {
                console.log(response);
                if (response.redirect) {
                    window.location.href = response.redirect;
                }

                if (response.error) {
                    returnError(response.message);
                }
            })
            .fail(function () {
                returnError("Fatal error");
            })
            .always(function () {
                if (window.caloader) {
                    window.caloader.deactivate();
                }
            });
    }
    
    // Terms
    function submitCheckoutCA() {
        // Prevent submit if is not ChargeAfter
        if ($('#chargeafter-payment').length === 0) {
            if (typeof scriptChargeAfterPayment !== 'undefined' && scriptChargeAfterPayment !== 0) {
                eval(scriptChargeAfterPayment);
            }

            return true;
        }

        returnError("");

        var termOfServiceOk = true,
            termsOfService = $('#termsofservice');
        
        if (termsOfService.length === 0) {
            termsOfService = $('#terms-of-service');
        }
        
        if (termsOfService.length > 0 && !termsOfService.is(":hidden")) {
            if (!termsOfService.is(':checked')) {
                var readTerms = $("#terms-of-service-warning-box");
                
                if (readTerms.length === 0) {
                    readTerms = $('#read-terms');
                }
                
                readTerms.dialog();
                termOfServiceOk = false;
            } else {
                termOfServiceOk = true;
            }
        }
        
        if (termOfServiceOk) {
            startCheckout();
        }
        
        return false;
    }

    if (typeof scriptChargeAfterPayment === 'undefined') {
        var scriptChargeAfterPayment;
    }

    // Init
    function initCheckoutCA() {
        if (typeof scriptChargeAfterPayment === 'undefined') {
            scriptChargeAfterPayment = $('.payment-info-next-step-button').attr('onclick');
        }

        // Dom Ready
        $(document).ready(function () {
            $('.payment-info-next-step-button, .confirm-order-button')
                .show().prop("onclick", null).unbind().click(submitCheckoutCA);   
            
            $('#checkoutForm').find('button[data-complete]')
                .show().prop("onclick", null).unbind().click(submitCheckoutCA);
        });
    }

    // Defer JQuery
    function deferJqueryLoading(method) {
        if (window.jQuery && typeof $ == 'function' && window.ChargeAfter) {
            method();
        } else {
            setTimeout(function () { deferJqueryLoading(method) }, 50);
        }
    }
    deferJqueryLoading(initCheckoutCA);
</!script>