authorizenet-card.js
1 month ago
authorizenet-echeck.js
1 month ago
authorizenet-googlepay.js
1 month ago
blocks-common.js
1 month ago
blocks-common.js
122 lines
| 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const { createElement, isValidElement } = wp.element; |
| 5 | const { getSetting } = wc.wcSettings; |
| 6 | const { registerPaymentMethod } = wc.wcBlocksRegistry; |
| 7 | const { decodeEntities } = wp.htmlEntities; |
| 8 | |
| 9 | function base64EncodeUnicode(str) { |
| 10 | try { |
| 11 | return btoa(unescape(encodeURIComponent(str))); |
| 12 | } catch (e) { |
| 13 | try { |
| 14 | return btoa(str); |
| 15 | } catch (err) { |
| 16 | return ''; |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | function getCheckoutTotalMajor() { |
| 22 | // Returns the checkout total in major currency units (e.g., 10.99). |
| 23 | let totals = null; |
| 24 | |
| 25 | try { |
| 26 | if ( |
| 27 | window.wc && |
| 28 | wc.wcBlocksData && |
| 29 | wc.wcBlocksData.checkout && |
| 30 | typeof wc.wcBlocksData.checkout.getCheckoutTotals === 'function' |
| 31 | ) { |
| 32 | totals = wc.wcBlocksData.checkout.getCheckoutTotals(); |
| 33 | } |
| 34 | } catch (e) {} |
| 35 | |
| 36 | // Fallback: Woo Blocks data store. |
| 37 | if (!totals) { |
| 38 | try { |
| 39 | if (window.wp && wp.data && typeof wp.data.select === 'function') { |
| 40 | const cartStore = wp.data.select('wc/store/cart'); |
| 41 | if (cartStore) { |
| 42 | if (typeof cartStore.getCartTotals === 'function') { |
| 43 | totals = cartStore.getCartTotals(); |
| 44 | } else if (typeof cartStore.getCartData === 'function') { |
| 45 | const cartData = cartStore.getCartData(); |
| 46 | totals = cartData && cartData.totals ? cartData.totals : null; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } catch (e) {} |
| 51 | } |
| 52 | |
| 53 | const minor = totals && (totals.total_price ?? totals.totalPrice ?? totals.total); |
| 54 | const minorNum = Number(minor); |
| 55 | return Number.isFinite(minorNum) ? minorNum / 100 : 0; |
| 56 | } |
| 57 | |
| 58 | function descriptionOrFallback(desc, fallback) { |
| 59 | const d = (desc || '').toString().trim(); |
| 60 | return d ? decodeEntities(d) : fallback; |
| 61 | } |
| 62 | |
| 63 | function registerIfConfigured(gatewayId, settingKey, config) { |
| 64 | const settings = getSetting(settingKey, {}); |
| 65 | if (!settings || !settings.title) return; |
| 66 | |
| 67 | const title = decodeEntities(settings.title || ''); |
| 68 | const description = decodeEntities(settings.description || ''); |
| 69 | const icons = settings.icons || []; |
| 70 | |
| 71 | const label = createElement( |
| 72 | 'span', |
| 73 | { style: { display: 'inline-flex', alignItems: 'center' } }, |
| 74 | title, |
| 75 | icons.length |
| 76 | ? createElement( |
| 77 | 'span', |
| 78 | { style: { display: 'flex', gap: '4px', marginLeft: '8px' } }, |
| 79 | icons.map((icon, i) => |
| 80 | createElement('img', { |
| 81 | key: `icon-${gatewayId}-${i}`, |
| 82 | src: icon.src, |
| 83 | alt: icon.alt || '', |
| 84 | style: { display: 'inline-block', verticalAlign: 'middle', height: '24px' }, |
| 85 | }) |
| 86 | ) |
| 87 | ) |
| 88 | : null |
| 89 | ); |
| 90 | |
| 91 | registerPaymentMethod({ |
| 92 | name: gatewayId, |
| 93 | label, |
| 94 | ariaLabel: settings.ariaLabel || title, |
| 95 | canMakePayment: () => true, |
| 96 | content: (function(){ |
| 97 | const Content = config.content(settings, description); |
| 98 | if (Content === null || typeof Content === 'undefined') return null; |
| 99 | if (isValidElement(Content)) return Content; |
| 100 | if (typeof Content === 'function') return createElement(Content); |
| 101 | return null; |
| 102 | })(), |
| 103 | edit: (function(){ |
| 104 | const Content = config.content(settings, description); |
| 105 | if (Content === null || typeof Content === 'undefined') return null; |
| 106 | if (isValidElement(Content)) return Content; |
| 107 | if (typeof Content === 'function') return createElement(Content); |
| 108 | return null; |
| 109 | })(), |
| 110 | supports: config.supports(settings), |
| 111 | paymentMethodId: gatewayId, |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | window.EasyAuthNetAuthorizeNetBlocksCommon = { |
| 116 | base64EncodeUnicode, |
| 117 | getCheckoutTotalMajor, |
| 118 | descriptionOrFallback, |
| 119 | registerIfConfigured, |
| 120 | }; |
| 121 | })(); |
| 122 |