|
1 | 1 | "use strict";
|
2 |
| -window.onload = function () { |
3 |
| - // reset validation when back button press |
4 |
| - $(window).on('popstate', function () { $('.tab-pane.active').attr('validation', 'false'); }); |
5 |
| - // Listen to changes on billing and shipping forms and update the payment methods accordingly |
6 |
| - $('#dwfrm_billing > fieldset.billing-address-block > fieldset.billing-address, #dwfrm_shipping > div.shipping-address > fieldset.shipping-address-block').on('mouseleave', function () { |
7 |
| - var data = $(this).serializeArray(); |
8 |
| - // @ts-ignore |
9 |
| - $.ajax({ |
10 |
| - method: "POST", |
11 |
| - url: $("[name=WALLEE_url]").first().val(), |
12 |
| - data: data, |
13 |
| - success: function (result) { |
14 |
| - if (result && result.wallee && result.wallee.possiblePaymentMethodsArray) { |
15 |
| - result.wallee.possiblePaymentMethodsArray.forEach(function (value) { |
16 |
| - $('.WALLEE-nav-item-' + value + ', .wallee-content-' + value).show(); |
17 |
| - }); |
| 2 | +jQuery(function ($) { |
| 3 | + var WalleeCheckout = { |
| 4 | + active_payment_tab: '.tab-pane.active', |
| 5 | + customer_address: '#dwfrm_billing > fieldset.billing-address-block > fieldset.billing-address, #dwfrm_shipping > div.shipping-address > fieldset.shipping-address-block', |
| 6 | + submit_payment: '#checkout-main button.btn.btn-primary.btn-block.submit-payment', |
| 7 | + place_order: '#checkout-main button.btn.btn-primary.btn-block.place-order', |
| 8 | + available_payment_methods_tabs: 'a.wallee-tab', |
| 9 | + first_available: '#dwfrm_billing div.form-nav.billing-nav.payment-information > ul > li > a', |
| 10 | + payment_handler: '[name=WALLEE_handler]', |
| 11 | + /** |
| 12 | + * Initialize plugin |
| 13 | + */ |
| 14 | + init: function () { |
| 15 | + $(window).on('popstate', this.disableValidation); |
| 16 | + $(this.customer_address).on('mouseleave', this.addressUpdatePaymentMethods); |
| 17 | + $(this.submit_payment).on('click', this.submitPayment); |
| 18 | + $(this.place_order).on('click', this.placeOrder); |
| 19 | + $(this.available_payment_methods_tabs).on('click', this.getIframe); |
| 20 | + $('.submit-shipping').on('click', function () { |
| 21 | + $('#dwfrm_billing div.form-nav.billing-nav.payment-information > ul > li > a').first().trigger('click'); |
| 22 | + $(".tab-pane.wallee-content").removeAttr("style"); |
| 23 | + }); |
| 24 | + window.WalleeCheckout = this; |
| 25 | + }, |
| 26 | + /** |
| 27 | + * Disable validation when user clicks back button |
| 28 | + */ |
| 29 | + disableValidation: function () { |
| 30 | + $(this.active_payment_tab).attr('validation', false); |
| 31 | + }, |
| 32 | + /** |
| 33 | + * Update payment methods on address change |
| 34 | + */ |
| 35 | + addressUpdatePaymentMethods: function () { |
| 36 | + var data = $(this).serializeArray(); |
| 37 | + // @ts-ignore |
| 38 | + $.ajax({ |
| 39 | + method: "POST", |
| 40 | + url: $("[name=WALLEE_url]").first().val(), |
| 41 | + data: data, |
| 42 | + success: function (result) { |
| 43 | + if (result && result.wallee && result.wallee.possiblePaymentMethodsArray) { |
| 44 | + result.wallee.possiblePaymentMethodsArray.forEach(function (value) { |
| 45 | + $('.WALLEE-nav-item-' + value + ', .wallee-content-' + value).show(); |
| 46 | + }); |
| 47 | + } |
18 | 48 | }
|
19 |
| - } |
20 |
| - }); |
21 |
| - }); |
22 |
| - // Validate selected WALLEE paymentmethod |
23 |
| - $('#checkout-main button.btn.btn-primary.btn-block.submit-payment').on('click', function (e) { |
24 |
| - var activeTabId = $('.tab-pane.active').data('id'); |
25 |
| - if (activeTabId) { |
26 |
| - var activeTabValid = $('.tab-pane.active').attr('validation'); |
27 |
| - if (activeTabValid && (activeTabValid === 'true')) { |
28 |
| - if ($('[name="WALLEE_handler"]').val() !== '') { |
29 |
| - return true; |
| 49 | + }); |
| 50 | + }, |
| 51 | + /** |
| 52 | + * Submit payment |
| 53 | + * @param event |
| 54 | + */ |
| 55 | + submitPayment: function (event) { |
| 56 | + var activeTabId = $(WalleeCheckout.active_payment_tab).data('id'); |
| 57 | + if (activeTabId) { |
| 58 | + var activeTabValid = $(WalleeCheckout.active_payment_tab).attr('validation'); |
| 59 | + if (activeTabValid) { |
| 60 | + if ($(WalleeCheckout.payment_handler).val() !== '') { |
| 61 | + return true; |
| 62 | + } |
30 | 63 | }
|
| 64 | + event.preventDefault(); |
| 65 | + window['WALLEE_handler' + activeTabId].validate(); |
| 66 | + return false; |
31 | 67 | }
|
32 |
| - e.preventDefault(); |
33 |
| - window['WALLEE_handler' + activeTabId].validate(); |
34 |
| - return false; |
35 |
| - } |
36 |
| - }); |
37 |
| - // Submit selected WALLEE payment method and place order |
38 |
| - $('#checkout-main button.btn.btn-primary.btn-block.place-order').on('click', function (e) { |
39 |
| - var activeTabId = $('.tab-pane.active').data('id'); |
40 |
| - if (activeTabId !== '') { |
41 |
| - var activeTabValid = $('.tab-pane.active').attr('validation'); |
42 |
| - if (activeTabValid === 'true') { |
43 |
| - var handlerName_1 = $('[name="WALLEE_handler"]').val(); |
44 |
| - if ((handlerName_1) && (window[handlerName_1] !== undefined)) { |
45 |
| - // disable the placeOrder button here |
46 |
| - $('body').trigger('checkout:disableButton', '.next-step-button button'); |
47 |
| - $.ajax({ |
48 |
| - url: $('.place-order').data('action'), |
49 |
| - method: 'POST', |
50 |
| - success: function (data) { |
51 |
| - // enable the placeOrder button here |
52 |
| - $('body').trigger('checkout:enableButton', '.next-step-button button'); |
53 |
| - if (data.error) { |
54 |
| - if (data.cartError) { |
55 |
| - window.location.href = data.redirectUrl; |
| 68 | + }, |
| 69 | + /** |
| 70 | + * Place order |
| 71 | + */ |
| 72 | + placeOrder: function (event) { |
| 73 | + var activeTabId = $(WalleeCheckout.active_payment_tab).data('id'); |
| 74 | + if (activeTabId !== '') { |
| 75 | + var activeTabValid = $(WalleeCheckout.active_payment_tab).attr('validation'); |
| 76 | + if (activeTabValid) { |
| 77 | + var handlerName_1 = $(WalleeCheckout.payment_handler).val(); |
| 78 | + if ((handlerName_1) && (window[handlerName_1] !== undefined)) { |
| 79 | + // disable the placeOrder button here |
| 80 | + $('body').trigger('checkout:disableButton', '.next-step-button button'); |
| 81 | + $.ajax({ |
| 82 | + url: $('.place-order').data('action'), |
| 83 | + method: 'POST', |
| 84 | + success: function (data) { |
| 85 | + if (data.error) { |
| 86 | + // enable the placeOrder button here |
| 87 | + $('body').trigger('checkout:enableButton', '.next-step-button button'); |
| 88 | + if (data.cartError) { |
| 89 | + window.location.href = data.redirectUrl; |
| 90 | + } |
56 | 91 | }
|
| 92 | + else { |
| 93 | + window[handlerName_1].submit(); |
| 94 | + } |
| 95 | + }, |
| 96 | + error: function () { |
| 97 | + // enable the placeOrder button here |
| 98 | + $('body').trigger('checkout:enableButton', $('.next-step-button button')); |
57 | 99 | }
|
58 |
| - else { |
59 |
| - var continueUrl = data.continueUrl; |
60 |
| - var urlParams_1 = { |
61 |
| - ID: data.orderID, |
62 |
| - token: data.orderToken |
63 |
| - }; |
64 |
| - continueUrl += (continueUrl.indexOf('?') !== -1 ? '&' : '?') + |
65 |
| - Object.keys(urlParams_1).map(function (key) { |
66 |
| - // @ts-ignore |
67 |
| - return key + '=' + encodeURIComponent(urlParams_1[key]); |
68 |
| - }).join('&'); |
69 |
| - window[handlerName_1].submit(); |
70 |
| - //window.location.href = continueUrl; |
71 |
| - } |
72 |
| - }, |
73 |
| - error: function () { |
74 |
| - // enable the placeOrder button here |
75 |
| - $('body').trigger('checkout:enableButton', $('.next-step-button button')); |
76 |
| - } |
77 |
| - }); |
| 100 | + }); |
| 101 | + } |
78 | 102 | }
|
| 103 | + return false; |
| 104 | + } |
| 105 | + return true; |
| 106 | + }, |
| 107 | + /** |
| 108 | + * Get iframe |
| 109 | + * @param event |
| 110 | + */ |
| 111 | + getIframe: function (event) { |
| 112 | + var value = $(this).data('id'); |
| 113 | + var payment_handler_name = 'WALLEE_handler' + value; |
| 114 | + if ($('#wallee-form-' + value).children().length === 0) { // iframe has not been loaded yet |
| 115 | + window[payment_handler_name] = window.IframeCheckoutHandler(value); |
| 116 | + window[payment_handler_name].setValidationCallback(function (validationResult) { |
| 117 | + WalleeCheckout.validationCallBack(payment_handler_name, validationResult); |
| 118 | + }); |
| 119 | + window[payment_handler_name].create('wallee-form-' + value); |
| 120 | + } |
| 121 | + return true; |
| 122 | + }, |
| 123 | + /** |
| 124 | + * validation callback |
| 125 | + * @param payment_handler_name |
| 126 | + * @param validationResult |
| 127 | + */ |
| 128 | + validationCallBack: function (payment_handler_name, validationResult) { |
| 129 | + if (validationResult.success) { |
| 130 | + $(WalleeCheckout.active_payment_tab).attr('validation', true); |
| 131 | + $(WalleeCheckout.payment_handler).val(payment_handler_name); |
| 132 | + $('#checkout-main button.btn.btn-primary.btn-block.submit-payment').trigger('click'); |
| 133 | + } |
| 134 | + else { |
| 135 | + $(WalleeCheckout.active_payment_tab).attr('validation', false); |
| 136 | + $(WalleeCheckout.payment_handler).val(''); |
79 | 137 | }
|
80 |
| - return false; |
81 |
| - } |
82 |
| - return true; |
83 |
| - }); |
84 |
| - // Bind available payment methods and fetch iframe |
85 |
| - $('a.wallee-tab').on('click', function (e) { |
86 |
| - var value = $(this).data('id'); |
87 |
| - if ($('#wallee-form-' + value).children().length === 0) { // iframe has not been loaded yet |
88 |
| - window['WALLEE_handler' + value] = window.IframeCheckoutHandler(value); |
89 |
| - window['WALLEE_handler' + value].create('wallee-form-' + value); |
90 |
| - window['WALLEE_handler' + value].setValidationCallback(function (validationResult) { |
91 |
| - if (validationResult.success) { |
92 |
| - $('.tab-pane.active').attr('validation', 'true'); |
93 |
| - $('#checkout-main button.btn.btn-primary.btn-block.submit-payment').click(); |
94 |
| - $('[name="WALLEE_handler"]').val('WALLEE_handler' + value); |
95 |
| - } |
96 |
| - else { |
97 |
| - $('.tab-pane.active').attr('validation', 'false'); |
98 |
| - $('[name="WALLEE_handler"]').val(''); |
99 |
| - } |
100 |
| - }); |
101 | 138 | }
|
102 |
| - return true; |
103 |
| - }); |
104 |
| - // Click on the first payment method |
105 |
| - $('#dwfrm_billing div.form-nav.billing-nav.payment-information > ul > li > a').first().click(); |
106 |
| -}; |
| 139 | + }; |
| 140 | + WalleeCheckout.init(); |
| 141 | +}); |
0 commit comments