Skip to content

Commit 632b6d0

Browse files
Mayishadiegocurbelo
authored andcommitted
Fix undefined check of global variable (#3387)
* get data from wcSettings on block checkout if 'wc_stripe_upe_params' is not loaded yet
1 parent 4758d8c commit 632b6d0

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** Changelog ***
22

3+
= 8.8.0 - xxxx-xx-xx =
4+
* Fix - Resolve an error for checkout block where 'wc_stripe_upe_params' is undefined due to the script registering the variable not being loaded yet.
5+
36
= 8.7.0 - xxxx-xx-xx =
47
* Fix - Prevent duplicate failed-order emails from being sent.
58
* Fix - Support custom name and description for Afterpay.

client/stripe-utils/utils.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global wc_stripe_upe_params */
1+
/* global wc_stripe_upe_params, wc */
22
import { dispatch } from '@wordpress/data';
33
import { __ } from '@wordpress/i18n';
44
import { getAppearance } from '../styles/upe';
@@ -21,13 +21,24 @@ import {
2121
* @return {StripeServerData} Stripe server data.
2222
*/
2323
const getStripeServerData = () => {
24-
// Classic checkout.
24+
let data = null;
25+
2526
// 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 ) {
2738
throw new Error( 'Stripe initialization data is not available' );
2839
}
29-
// eslint-disable-next-line camelcase
30-
return wc_stripe_upe_params;
40+
41+
return data;
3142
};
3243

3344
const isNonFriendlyError = ( type ) =>
@@ -210,8 +221,8 @@ export { getStripeServerData, getErrorMessageForTypeAndCode };
210221
*/
211222
export const isLinkEnabled = ( paymentMethodsConfig ) => {
212223
return (
213-
paymentMethodsConfig.link !== undefined &&
214-
paymentMethodsConfig.card !== undefined
224+
paymentMethodsConfig?.link !== undefined &&
225+
paymentMethodsConfig?.card !== undefined
215226
);
216227
};
217228

@@ -505,16 +516,18 @@ export const getPaymentMethodName = ( paymentMethodType ) => {
505516
* @return {boolean} Whether the payment method is restricted to selected billing country.
506517
**/
507518
export const isPaymentMethodRestrictedToLocation = ( upeElement ) => {
508-
const paymentMethodsConfig = getStripeServerData()?.paymentMethodsConfig;
519+
const paymentMethodsConfig =
520+
getStripeServerData()?.paymentMethodsConfig || {};
509521
const paymentMethodType = upeElement.dataset.paymentMethodType;
510-
return !! paymentMethodsConfig[ paymentMethodType ].countries.length;
522+
return !! paymentMethodsConfig[ paymentMethodType ]?.countries.length;
511523
};
512524

513525
/**
514526
* @param {Object} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
515527
**/
516528
export const togglePaymentMethodForCountry = ( upeElement ) => {
517-
const paymentMethodsConfig = getStripeServerData()?.paymentMethodsConfig;
529+
const paymentMethodsConfig =
530+
getStripeServerData()?.paymentMethodsConfig || {};
518531
const paymentMethodType = upeElement.dataset.paymentMethodType;
519532
const supportedCountries =
520533
paymentMethodsConfig[ paymentMethodType ].countries;

readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ If you get stuck, you can ask for help in the Plugin Forum.
128128

129129
== Changelog ==
130130

131+
= 8.8.0 - xxxx-xx-xx =
132+
* Fix - Resolve an error for checkout block where 'wc_stripe_upe_params' is undefined due to the script registering the variable not being loaded yet.
133+
131134
= 8.7.0 - xxxx-xx-xx =
132135
* Fix - Prevent duplicate failed-order emails from being sent.
133136
* Fix - Support custom name and description for Afterpay.

0 commit comments

Comments
 (0)