blocks
1 month ago
acceptjs-echeck-handler.js
1 month ago
acceptjs-handler.js
1 month ago
blocks-authorizenet.js
1 month ago
easyauthnet-authorizenet-admin.js
1 month ago
easyauthnet-review-ajax.js
1 month ago
googlepay-express.js
1 month ago
googlepay-handler.js
1 month ago
acceptjs-echeck-handler.js
197 lines
| 1 | (function ($) { |
| 2 | const { __ } = wp.i18n; |
| 3 | |
| 4 | class EPAcceptJsEcheckHandler { |
| 5 | constructor(gatewayId, params) { |
| 6 | this.gatewayId = gatewayId; |
| 7 | this.params = params; |
| 8 | this.debug = !!params.debug; |
| 9 | this.tokenGenerated = false; |
| 10 | this.pendingResolve = null; |
| 11 | this.pendingReject = null; |
| 12 | |
| 13 | this.attachEventListeners(); |
| 14 | this.log('eCheck handler initialized'); |
| 15 | } |
| 16 | |
| 17 | log(message, data = null) { |
| 18 | if (this.debug) { |
| 19 | // eslint-disable-next-line no-console |
| 20 | console.log(`[AuthorizeNet][eCheck] ${message}`, data || ''); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | error(message, data = null) { |
| 25 | if (this.debug) { |
| 26 | // eslint-disable-next-line no-console |
| 27 | console.error(`[AuthorizeNet][eCheck] ${message}`, data || ''); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | isSelected() { |
| 32 | return $(`#payment_method_${this.gatewayId}`).is(':checked'); |
| 33 | } |
| 34 | |
| 35 | collectBankData() { |
| 36 | return { |
| 37 | nameOnAccount: $('#easyauthnet_echeck_name').val().trim(), |
| 38 | routingNumber: $('#easyauthnet_echeck_routing').val().replace(/\s+/g, ''), |
| 39 | accountNumber: $('#easyauthnet_echeck_account').val().replace(/\s+/g, ''), |
| 40 | accountType: $('#easyauthnet_echeck_type').val(), |
| 41 | echeckType: 'WEB' |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | validateBankFields(data) { |
| 46 | if (!data.nameOnAccount || !data.routingNumber || !data.accountNumber || !data.accountType) { |
| 47 | this.showError(__('Please fill in all bank details.', 'payment-gateway-for-authorize-net-for-woocommerce')); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | // Basic length validation (not exhaustive) |
| 52 | if (data.routingNumber.length < 6) { |
| 53 | this.showError(__('Please enter a valid routing number.', 'payment-gateway-for-authorize-net-for-woocommerce')); |
| 54 | return false; |
| 55 | } |
| 56 | if (data.accountNumber.length < 4) { |
| 57 | this.showError(__('Please enter a valid account number.', 'payment-gateway-for-authorize-net-for-woocommerce')); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | addHiddenField(form, name, value) { |
| 65 | form.find(`input[name="${name}"]`).remove(); |
| 66 | $('<input>').attr({ type: 'hidden', name, id: name, value }).appendTo(form); |
| 67 | } |
| 68 | |
| 69 | showError(msg) { |
| 70 | const $form = $('form.checkout, .woocommerce-notices-wrapper, .woocommerce, .wc-block-components-notices').first(); |
| 71 | if (!$form.length) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $form.find('.woocommerce-error').remove(); |
| 76 | $form.prepend(`<ul class="woocommerce-error"><li>${msg}</li></ul>`); |
| 77 | $('html, body').animate({ scrollTop: $form.offset().top - 100 }, 500); |
| 78 | } |
| 79 | |
| 80 | sendToAcceptJs(bankData, form, resolve, reject) { |
| 81 | this.pendingResolve = resolve; |
| 82 | this.pendingReject = reject; |
| 83 | |
| 84 | try { |
| 85 | if (typeof Accept === 'undefined') { |
| 86 | throw new Error('Accept.js library not loaded'); |
| 87 | } |
| 88 | |
| 89 | this.log('Dispatching bank data to Authorize.Net'); |
| 90 | form.addClass('easyauthnet-authorizenet-submitting'); |
| 91 | |
| 92 | Accept.dispatchData( |
| 93 | { |
| 94 | authData: { |
| 95 | clientKey: this.params.client_key, |
| 96 | apiLoginID: this.params.login_id |
| 97 | }, |
| 98 | bankData: { |
| 99 | nameOnAccount: bankData.nameOnAccount, |
| 100 | routingNumber: bankData.routingNumber, |
| 101 | accountNumber: bankData.accountNumber, |
| 102 | accountType: bankData.accountType, |
| 103 | echeckType: bankData.echeckType |
| 104 | } |
| 105 | }, |
| 106 | (response) => { |
| 107 | this.handleResponse(response, form); |
| 108 | } |
| 109 | ); |
| 110 | } catch (err) { |
| 111 | form.removeClass('easyauthnet-authorizenet-submitting'); |
| 112 | this.error('Accept.js error', err); |
| 113 | if (reject) { |
| 114 | reject(err); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | handleResponse(response, form) { |
| 120 | this.log('Received response from Authorize.Net', response); |
| 121 | |
| 122 | if (response?.messages?.resultCode === 'Error') { |
| 123 | const message = response?.messages?.message?.[0]?.text || __('Payment tokenization failed.', 'payment-gateway-for-authorize-net-for-woocommerce'); |
| 124 | this.showError(message); |
| 125 | form.removeClass('easyauthnet-authorizenet-submitting'); |
| 126 | if (this.pendingReject) { |
| 127 | this.pendingReject(new Error(message)); |
| 128 | } |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | const token = response?.opaqueData?.dataValue || ''; |
| 133 | const descriptor = response?.opaqueData?.dataDescriptor || ''; |
| 134 | if (!token) { |
| 135 | const message = __('Payment tokenization failed.', 'payment-gateway-for-authorize-net-for-woocommerce'); |
| 136 | this.showError(message); |
| 137 | form.removeClass('easyauthnet-authorizenet-submitting'); |
| 138 | if (this.pendingReject) { |
| 139 | this.pendingReject(new Error(message)); |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | this.addHiddenField(form, 'easyauthnet_authorizenet_echeck_token', token); |
| 145 | if (descriptor) { |
| 146 | this.addHiddenField(form, 'easyauthnet_authorizenet_echeck_descriptor', descriptor); |
| 147 | } |
| 148 | form.removeClass('easyauthnet-authorizenet-submitting'); |
| 149 | |
| 150 | if (this.pendingResolve) { |
| 151 | this.pendingResolve({ token, descriptor }); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | attachEventListeners() { |
| 156 | // Classic checkout |
| 157 | $('form.checkout, form#order_review').on(`checkout_place_order_${this.gatewayId}`, (e) => { |
| 158 | const form = $(e.currentTarget); |
| 159 | |
| 160 | if (!this.isSelected() || this.tokenGenerated) { |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | e.preventDefault(); |
| 165 | $('.woocommerce-error').remove(); |
| 166 | |
| 167 | const data = this.collectBankData(); |
| 168 | if (!this.validateBankFields(data)) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | this.sendToAcceptJs(data, form, () => { |
| 173 | this.tokenGenerated = true; |
| 174 | form.trigger('submit'); |
| 175 | }, (err) => { |
| 176 | this.error('Accept.js error (classic)', err); |
| 177 | }); |
| 178 | |
| 179 | return false; |
| 180 | }); |
| 181 | |
| 182 | // If customer changes gateway, reset token flag |
| 183 | $(document.body).on('change', 'input[name="payment_method"]', () => { |
| 184 | this.tokenGenerated = false; |
| 185 | $('form.checkout, form#order_review').find('input[name="easyauthnet_authorizenet_echeck_token"]').remove(); |
| 186 | }); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | $(function () { |
| 191 | if (typeof easyauthnet_authorizenet_echeck_params === 'undefined') { |
| 192 | return; |
| 193 | } |
| 194 | new EPAcceptJsEcheckHandler('easyauthnet_authorizenet_echeck', easyauthnet_authorizenet_echeck_params); |
| 195 | }); |
| 196 | })(jQuery); |
| 197 |