payment-gateway-for-authorize-net-for-woocommerce
/
assets
/
js
/
blocks
/
authorizenet-googlepay.js
authorizenet-card.js
1 month ago
authorizenet-echeck.js
1 month ago
authorizenet-googlepay.js
1 month ago
blocks-common.js
1 month ago
authorizenet-googlepay.js
169 lines
| 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const common = window.EasyAuthNetAuthorizeNetBlocksCommon; |
| 5 | if (!common) return; |
| 6 | |
| 7 | const { createElement, useEffect, useRef, useState } = wp.element; |
| 8 | const { __ } = wp.i18n; |
| 9 | |
| 10 | function googlePayContent(settings) { |
| 11 | return ({ eventRegistration, emitResponse }) => { |
| 12 | const { onPaymentProcessing } = eventRegistration; |
| 13 | const [isProcessing, setIsProcessing] = useState(false); |
| 14 | |
| 15 | const unsubRef = useRef(null); |
| 16 | const mountedRef = useRef(false); |
| 17 | |
| 18 | useEffect(() => { |
| 19 | mountedRef.current = true; |
| 20 | return () => { |
| 21 | mountedRef.current = false; |
| 22 | }; |
| 23 | }, []); |
| 24 | |
| 25 | useEffect(() => { |
| 26 | if (unsubRef.current) return; |
| 27 | |
| 28 | unsubRef.current = onPaymentProcessing(async () => { |
| 29 | if (mountedRef.current) setIsProcessing(true); |
| 30 | try { |
| 31 | if (!settings.gatewayMerchantId) { |
| 32 | throw new Error( |
| 33 | __('Google Pay is not configured. Please contact the store owner.', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 34 | ); |
| 35 | } |
| 36 | if (!window.google || !google.payments || !google.payments.api) { |
| 37 | throw new Error( |
| 38 | (settings.i18n && settings.i18n.failed) || |
| 39 | __('Google Pay unavailable.', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | const client = new google.payments.api.PaymentsClient({ |
| 44 | environment: settings.environment === 'live' ? 'PRODUCTION' : 'TEST', |
| 45 | }); |
| 46 | |
| 47 | const readyResp = await client.isReadyToPay({ |
| 48 | apiVersion: 2, |
| 49 | apiVersionMinor: 0, |
| 50 | allowedPaymentMethods: [ |
| 51 | { |
| 52 | type: 'CARD', |
| 53 | parameters: { |
| 54 | allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'], |
| 55 | allowedCardNetworks: ['AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'], |
| 56 | }, |
| 57 | }, |
| 58 | ], |
| 59 | }); |
| 60 | |
| 61 | if (!readyResp || !readyResp.result) { |
| 62 | throw new Error( |
| 63 | (settings.i18n && settings.i18n.not_ready) || |
| 64 | __('Google Pay not available.', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | const totalRaw = common.getCheckoutTotalMajor(); |
| 69 | |
| 70 | const paymentData = await client.loadPaymentData({ |
| 71 | apiVersion: 2, |
| 72 | apiVersionMinor: 0, |
| 73 | allowedPaymentMethods: [ |
| 74 | { |
| 75 | type: 'CARD', |
| 76 | parameters: { |
| 77 | allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'], |
| 78 | allowedCardNetworks: ['AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'], |
| 79 | }, |
| 80 | tokenizationSpecification: { |
| 81 | type: 'PAYMENT_GATEWAY', |
| 82 | parameters: { |
| 83 | gateway: 'authorizenet', |
| 84 | gatewayMerchantId: settings.gatewayMerchantId, |
| 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | ], |
| 89 | merchantInfo: (function () { |
| 90 | var mi = { merchantName: settings.merchantName || '' }; |
| 91 | if (settings.googleMerchantId) { |
| 92 | mi.merchantId = String(settings.googleMerchantId); |
| 93 | } |
| 94 | return mi; |
| 95 | })(), |
| 96 | transactionInfo: { |
| 97 | totalPriceStatus: 'FINAL', |
| 98 | totalPrice: Number(totalRaw || 0).toFixed(2), |
| 99 | currencyCode: settings.currency || 'USD', |
| 100 | countryCode: settings.countryCode || 'US', |
| 101 | }, |
| 102 | }); |
| 103 | |
| 104 | const token = paymentData?.paymentMethodData?.tokenizationData?.token || ''; |
| 105 | if (!token) { |
| 106 | throw new Error( |
| 107 | (settings.i18n && settings.i18n.failed) || |
| 108 | __('Google Pay token missing.', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | return { |
| 113 | type: emitResponse.responseTypes.SUCCESS, |
| 114 | meta: { |
| 115 | paymentMethodData: { |
| 116 | easyauthnet_authorizenet_googlepay_token: common.base64EncodeUnicode(token), |
| 117 | easyauthnet_authorizenet_googlepay_nonce: settings.nonce || '', |
| 118 | }, |
| 119 | }, |
| 120 | }; |
| 121 | } catch (error) { |
| 122 | return { |
| 123 | type: emitResponse.responseTypes.ERROR, |
| 124 | message: |
| 125 | error.message || |
| 126 | __('Payment processing failed. Please try again.', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 127 | }; |
| 128 | } finally { |
| 129 | if (mountedRef.current) setIsProcessing(false); |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | return () => { |
| 134 | if (unsubRef.current) { |
| 135 | try { |
| 136 | unsubRef.current(); |
| 137 | } catch (e) { |
| 138 | // ignore |
| 139 | } |
| 140 | unsubRef.current = null; |
| 141 | } |
| 142 | }; |
| 143 | }, []); |
| 144 | |
| 145 | return createElement( |
| 146 | 'div', |
| 147 | { className: `wc-payment-form ${isProcessing ? 'processing' : ''}` }, |
| 148 | createElement( |
| 149 | 'p', |
| 150 | null, |
| 151 | common.descriptionOrFallback( |
| 152 | settings.description, |
| 153 | __('You will be prompted to approve the payment in Google Pay.', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 154 | ) |
| 155 | ) |
| 156 | ); |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | function supports(settings) { |
| 161 | return settings.supports || {}; |
| 162 | } |
| 163 | |
| 164 | common.registerIfConfigured('easyauthnet_authorizenet_googlepay', 'easyauthnet_authorizenet_googlepay_data', { |
| 165 | content: (settings) => googlePayContent(settings), |
| 166 | supports, |
| 167 | }); |
| 168 | })(); |
| 169 |