-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
189 lines (173 loc) · 5.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* External dependencies
*/
// Handled as an external dependency: see '/webpack.config.js:83'
import {
registerPaymentMethod,
registerExpressPaymentMethod,
// eslint-disable-next-line import/no-unresolved
} from '@woocommerce/blocks-registry';
/**
* Internal dependencies
*/
import { getUPEConfig, getConfig } from 'utils/checkout';
import { isLinkEnabled } from '../utils/upe';
import WCPayAPI from '../api';
import { SavedTokenHandler } from './saved-token-handler';
import PaymentMethodLabel from './payment-method-label';
import request from '../utils/request';
import enqueueFraudScripts from 'fraud-scripts';
import {
tokenizedExpressCheckoutElementApplePay,
tokenizedExpressCheckoutElementGooglePay,
} from 'wcpay/tokenized-express-checkout/blocks';
import {
PAYMENT_METHOD_NAME_CARD,
PAYMENT_METHOD_NAME_BANCONTACT,
PAYMENT_METHOD_NAME_BECS,
PAYMENT_METHOD_NAME_EPS,
PAYMENT_METHOD_NAME_GIROPAY,
PAYMENT_METHOD_NAME_IDEAL,
PAYMENT_METHOD_NAME_P24,
PAYMENT_METHOD_NAME_SEPA,
PAYMENT_METHOD_NAME_SOFORT,
PAYMENT_METHOD_NAME_AFFIRM,
PAYMENT_METHOD_NAME_AFTERPAY,
PAYMENT_METHOD_NAME_KLARNA,
} from '../constants.js';
import { getDeferredIntentCreationUPEFields } from './payment-elements';
import { handleWooPayEmailInput } from '../woopay/email-input-iframe';
import { recordUserEvent } from 'tracks';
import wooPayExpressCheckoutPaymentMethod from '../woopay/express-button/woopay-express-checkout-payment-method';
import { isPreviewing } from '../preview';
import '../utils/copy-test-number';
const upeMethods = {
card: PAYMENT_METHOD_NAME_CARD,
bancontact: PAYMENT_METHOD_NAME_BANCONTACT,
au_becs_debit: PAYMENT_METHOD_NAME_BECS,
eps: PAYMENT_METHOD_NAME_EPS,
giropay: PAYMENT_METHOD_NAME_GIROPAY,
ideal: PAYMENT_METHOD_NAME_IDEAL,
p24: PAYMENT_METHOD_NAME_P24,
sepa_debit: PAYMENT_METHOD_NAME_SEPA,
sofort: PAYMENT_METHOD_NAME_SOFORT,
affirm: PAYMENT_METHOD_NAME_AFFIRM,
afterpay_clearpay: PAYMENT_METHOD_NAME_AFTERPAY,
klarna: PAYMENT_METHOD_NAME_KLARNA,
};
const enabledPaymentMethodsConfig = getUPEConfig( 'paymentMethodsConfig' );
const isStripeLinkEnabled = isLinkEnabled( enabledPaymentMethodsConfig );
// Create an API object, which will be used throughout the checkout.
const api = new WCPayAPI(
{
publishableKey: getUPEConfig( 'publishableKey' ),
accountId: getUPEConfig( 'accountId' ),
forceNetworkSavedCards: getUPEConfig( 'forceNetworkSavedCards' ),
locale: getUPEConfig( 'locale' ),
isStripeLinkEnabled,
},
request
);
Object.entries( enabledPaymentMethodsConfig )
.filter( ( [ upeName ] ) => upeName !== 'link' )
.forEach( ( [ upeName, upeConfig ] ) => {
registerPaymentMethod( {
name: upeMethods[ upeName ],
content: getDeferredIntentCreationUPEFields(
upeName,
upeMethods,
api,
upeConfig.testingInstructions
),
edit: getDeferredIntentCreationUPEFields(
upeName,
upeMethods,
api,
upeConfig.testingInstructions
),
savedTokenComponent: <SavedTokenHandler api={ api } />,
canMakePayment: ( cartData ) => {
const billingCountry = cartData.billingAddress.country;
const needsPayment = cartData.cart.cartNeedsPayment;
const isRestrictedInAnyCountry = !! upeConfig.countries.length;
const isAvailableInTheCountry =
! isRestrictedInAnyCountry ||
upeConfig.countries.includes( billingCountry );
// We used to check if stripe was loaded with `getStripeForUPE`, but we can't guarantee it will be loaded synchronously.
return needsPayment && isAvailableInTheCountry;
},
paymentMethodId: upeMethods[ upeName ],
// see .wc-block-checkout__payment-method styles in blocks/style.scss
label: (
<PaymentMethodLabel
api={ api }
title={ upeConfig.title }
countries={ upeConfig.countries }
iconLight={ upeConfig.icon }
iconDark={ upeConfig.darkIcon }
upeName={ upeName }
/>
),
ariaLabel: 'WooPayments',
supports: {
showSavedCards: getUPEConfig( 'isSavedCardsEnabled' ) ?? false,
showSaveOption: upeConfig.showSaveOption ?? false,
features: getUPEConfig( 'features' ),
},
} );
} );
const addCheckoutTracking = () => {
const placeOrderButton = document.getElementsByClassName(
'wc-block-components-checkout-place-order-button'
);
if ( placeOrderButton.length ) {
placeOrderButton[ 0 ].addEventListener( 'click', () => {
const blocksCheckbox = document.getElementById(
'radio-control-wc-payment-method-options-woocommerce_payments'
);
if ( ! blocksCheckbox?.checked ) {
return;
}
recordUserEvent( 'checkout_place_order_button_click' );
} );
}
};
// Call handleWooPayEmailInput if woopay is enabled and this is the checkout page.
if ( getUPEConfig( 'isWooPayEnabled' ) ) {
if (
document.querySelector( '[data-block-name="woocommerce/checkout"]' ) &&
getUPEConfig( 'isWooPayEmailInputEnabled' ) &&
! isPreviewing()
) {
handleWooPayEmailInput( '#email', api, true );
}
if ( getUPEConfig( 'shouldShowWooPayButton' ) ) {
registerExpressPaymentMethod( wooPayExpressCheckoutPaymentMethod() );
}
}
if ( getUPEConfig( 'isPaymentRequestEnabled' ) ) {
registerExpressPaymentMethod(
tokenizedExpressCheckoutElementApplePay( api )
);
registerExpressPaymentMethod(
tokenizedExpressCheckoutElementGooglePay( api )
);
}
window.addEventListener( 'load', () => {
enqueueFraudScripts( getUPEConfig( 'fraudServices' ) );
addCheckoutTracking();
} );
// If multi-currency is enabled, add currency code to total amount in cart and checkout blocks.
if ( getConfig( 'isMultiCurrencyEnabled' ) ) {
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyTotalsPrice = ( defaultValue, extensions, args ) => {
const { cart } = args;
if ( cart?.cartTotals?.currency_code ) {
return `<price/> ${ cart.cartTotals.currency_code }`;
}
return defaultValue;
};
registerCheckoutFilters( 'woocommerce-payments', {
totalValue: modifyTotalsPrice,
} );
}