1
- /* global wc_stripe_upe_params */
1
+ /* global wc_stripe_upe_params, wc */
2
2
import { dispatch } from '@wordpress/data' ;
3
3
import { __ } from '@wordpress/i18n' ;
4
4
import { getAppearance } from '../styles/upe' ;
@@ -21,13 +21,24 @@ import {
21
21
* @return {StripeServerData } Stripe server data.
22
22
*/
23
23
const getStripeServerData = ( ) => {
24
- // Classic checkout.
24
+ let data = null ;
25
+
25
26
// eslint-disable-next-line camelcase
26
- if ( ! wc_stripe_upe_params ) {
27
+ if ( typeof wc_stripe_upe_params !== 'undefined' ) {
28
+ data = wc_stripe_upe_params ; // eslint-disable-line camelcase
29
+ } else if (
30
+ typeof wc === 'object' &&
31
+ typeof wc . wcSettings !== 'undefined'
32
+ ) {
33
+ // 'getSetting' has this data value on block checkout only.
34
+ data = wc . wcSettings ?. getSetting ( 'getSetting' ) || null ;
35
+ }
36
+
37
+ if ( ! data ) {
27
38
throw new Error ( 'Stripe initialization data is not available' ) ;
28
39
}
29
- // eslint-disable-next-line camelcase
30
- return wc_stripe_upe_params ;
40
+
41
+ return data ;
31
42
} ;
32
43
33
44
const isNonFriendlyError = ( type ) =>
@@ -210,8 +221,8 @@ export { getStripeServerData, getErrorMessageForTypeAndCode };
210
221
*/
211
222
export const isLinkEnabled = ( paymentMethodsConfig ) => {
212
223
return (
213
- paymentMethodsConfig . link !== undefined &&
214
- paymentMethodsConfig . card !== undefined
224
+ paymentMethodsConfig ? .link !== undefined &&
225
+ paymentMethodsConfig ? .card !== undefined
215
226
) ;
216
227
} ;
217
228
@@ -505,16 +516,18 @@ export const getPaymentMethodName = ( paymentMethodType ) => {
505
516
* @return {boolean } Whether the payment method is restricted to selected billing country.
506
517
**/
507
518
export const isPaymentMethodRestrictedToLocation = ( upeElement ) => {
508
- const paymentMethodsConfig = getStripeServerData ( ) ?. paymentMethodsConfig ;
519
+ const paymentMethodsConfig =
520
+ getStripeServerData ( ) ?. paymentMethodsConfig || { } ;
509
521
const paymentMethodType = upeElement . dataset . paymentMethodType ;
510
- return ! ! paymentMethodsConfig [ paymentMethodType ] . countries . length ;
522
+ return ! ! paymentMethodsConfig [ paymentMethodType ] ? .countries . length ;
511
523
} ;
512
524
513
525
/**
514
526
* @param {Object } upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
515
527
**/
516
528
export const togglePaymentMethodForCountry = ( upeElement ) => {
517
- const paymentMethodsConfig = getStripeServerData ( ) ?. paymentMethodsConfig ;
529
+ const paymentMethodsConfig =
530
+ getStripeServerData ( ) ?. paymentMethodsConfig || { } ;
518
531
const paymentMethodType = upeElement . dataset . paymentMethodType ;
519
532
const supportedCountries =
520
533
paymentMethodsConfig [ paymentMethodType ] . countries ;
0 commit comments