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
googlepay-handler.js
205 lines
| 1 | (function ($) { |
| 2 | "use strict"; |
| 3 | |
| 4 | var params = null; |
| 5 | var IN_FLIGHT = false; |
| 6 | |
| 7 | function dbg() { |
| 8 | if (!params || !params.debug) return; |
| 9 | var args = Array.prototype.slice.call(arguments); |
| 10 | args.unshift("[EasyAuthNet][GPay]"); |
| 11 | console.log.apply(console, args); |
| 12 | } |
| 13 | |
| 14 | // UTF-8 safe base64 encode |
| 15 | function base64EncodeUtf8(str) { |
| 16 | str = String(str || ""); |
| 17 | try { |
| 18 | return btoa(unescape(encodeURIComponent(str))); |
| 19 | } catch (e) { |
| 20 | // If btoa fails, we want to surface it rather than silently sending a bad token |
| 21 | throw new Error("Base64 encoding failed"); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | function getSelectedMethod() { |
| 26 | return $('input[name="payment_method"]:checked').val() || ""; |
| 27 | } |
| 28 | |
| 29 | function isGooglePaySelected() { |
| 30 | return params && getSelectedMethod() === params.gateway_id; |
| 31 | } |
| 32 | |
| 33 | function getTokenField() { |
| 34 | return $("#" + params.token_field); |
| 35 | } |
| 36 | |
| 37 | function blockCheckout() { |
| 38 | $("#place_order").prop("disabled", true); |
| 39 | } |
| 40 | |
| 41 | function unblockCheckout() { |
| 42 | $("#place_order").prop("disabled", false); |
| 43 | } |
| 44 | |
| 45 | function buildPaymentsClient() { |
| 46 | return new google.payments.api.PaymentsClient({ |
| 47 | environment: params.environment === "live" ? "PRODUCTION" : "TEST" |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | function buildIsReadyToPayRequest() { |
| 52 | return { |
| 53 | apiVersion: 2, |
| 54 | apiVersionMinor: 0, |
| 55 | allowedPaymentMethods: [{ |
| 56 | type: "CARD", |
| 57 | parameters: { |
| 58 | allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"], |
| 59 | allowedCardNetworks: ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"] |
| 60 | } |
| 61 | }] |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | function normalizeTotal(total) { |
| 66 | var n = Number(total); |
| 67 | if (isNaN(n) || n < 0) n = 0; |
| 68 | return n.toFixed(2); |
| 69 | } |
| 70 | |
| 71 | function buildPaymentDataRequest(total) { |
| 72 | var req = { |
| 73 | apiVersion: 2, |
| 74 | apiVersionMinor: 0, |
| 75 | allowedPaymentMethods: [{ |
| 76 | type: "CARD", |
| 77 | parameters: { |
| 78 | allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"], |
| 79 | allowedCardNetworks: ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"] |
| 80 | }, |
| 81 | tokenizationSpecification: { |
| 82 | type: "PAYMENT_GATEWAY", |
| 83 | parameters: { |
| 84 | gateway: "authorizenet", |
| 85 | gatewayMerchantId: params.gatewayMerchantId |
| 86 | } |
| 87 | } |
| 88 | }], |
| 89 | merchantInfo: { |
| 90 | merchantName: params.merchantName || "" |
| 91 | }, |
| 92 | transactionInfo: { |
| 93 | totalPriceStatus: "FINAL", |
| 94 | // � |
| 95 | IMPORTANT: use the passed total (not params.total) |
| 96 | totalPrice: normalizeTotal(total), |
| 97 | currencyCode: params.currency || "USD", |
| 98 | countryCode: params.countryCode || "US" |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | if (params.googleMerchantId) { |
| 103 | req.merchantInfo.merchantId = params.googleMerchantId; |
| 104 | } |
| 105 | |
| 106 | return req; |
| 107 | } |
| 108 | |
| 109 | function startGooglePay() { |
| 110 | var $token = getTokenField(); |
| 111 | |
| 112 | // token already present |
| 113 | if ($token.val()) return Promise.resolve(true); |
| 114 | |
| 115 | var client = buildPaymentsClient(); |
| 116 | |
| 117 | return client.isReadyToPay(buildIsReadyToPayRequest()) |
| 118 | .then(function (resp) { |
| 119 | if (!resp || !resp.result) throw new Error("Google Pay not ready"); |
| 120 | |
| 121 | // Use PHP-provided total (params.total) here. |
| 122 | // If you later add AJAX total refresh, replace params.total with returned value. |
| 123 | var total = Number(params.total || 0); |
| 124 | |
| 125 | dbg("Opening GPay with total=", total); |
| 126 | |
| 127 | return client.loadPaymentData(buildPaymentDataRequest(total)); |
| 128 | }) |
| 129 | .then(function (paymentData) { |
| 130 | // Avoid optional chaining for compatibility |
| 131 | var token = |
| 132 | paymentData && |
| 133 | paymentData.paymentMethodData && |
| 134 | paymentData.paymentMethodData.tokenizationData && |
| 135 | paymentData.paymentMethodData.tokenizationData.token; |
| 136 | |
| 137 | if (!token) throw new Error("Google Pay token missing"); |
| 138 | |
| 139 | // � |
| 140 | Authorize.Net expects opaqueData.dataValue to be base64-encoded |
| 141 | var tokenB64 = base64EncodeUtf8(token); |
| 142 | |
| 143 | // Store base64 token for PHP |
| 144 | $token.val(tokenB64); |
| 145 | |
| 146 | dbg("Token stored (raw len / b64 len) =", String(token).length, String(tokenB64).length); |
| 147 | dbg("Token b64 prefix =", String(tokenB64).substring(0, 12)); |
| 148 | |
| 149 | return true; |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | function showError(err) { |
| 154 | unblockCheckout(); |
| 155 | IN_FLIGHT = false; |
| 156 | alert((err && err.message) ? err.message : "Google Pay failed"); |
| 157 | } |
| 158 | |
| 159 | $(function () { |
| 160 | if (!window.easyauthnet_googlepay_params) return; |
| 161 | params = window.easyauthnet_googlepay_params; |
| 162 | |
| 163 | dbg("Init", params); |
| 164 | |
| 165 | // Clear token on checkout refresh (but not while paying) |
| 166 | $(document.body).on("updated_checkout", function () { |
| 167 | if (IN_FLIGHT) return; |
| 168 | getTokenField().val(""); |
| 169 | }); |
| 170 | |
| 171 | // Intercept Place Order click |
| 172 | $(document).on("click", "#place_order", function (e) { |
| 173 | if (!isGooglePaySelected()) return true; |
| 174 | |
| 175 | var $token = getTokenField(); |
| 176 | if ($token.val()) return true; |
| 177 | |
| 178 | e.preventDefault(); |
| 179 | e.stopImmediatePropagation(); |
| 180 | |
| 181 | if (IN_FLIGHT) return false; |
| 182 | IN_FLIGHT = true; |
| 183 | |
| 184 | blockCheckout(); |
| 185 | |
| 186 | startGooglePay() |
| 187 | .then(function () { |
| 188 | IN_FLIGHT = false; |
| 189 | unblockCheckout(); |
| 190 | $("form.checkout").trigger("submit"); |
| 191 | }) |
| 192 | .catch(showError); |
| 193 | |
| 194 | return false; |
| 195 | }); |
| 196 | |
| 197 | // Safety net for Woo submit flow |
| 198 | $("form.checkout").on("checkout_place_order_" + params.gateway_id, function () { |
| 199 | var $token = getTokenField(); |
| 200 | return !!$token.val(); |
| 201 | }); |
| 202 | }); |
| 203 | |
| 204 | })(jQuery); |
| 205 |