Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined check of global variable #3387

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*** Changelog ***

= 8.8.0 - xxxx-xx-xx =
* 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.

= 8.7.0 - xxxx-xx-xx =
* Fix - Prevent duplicate failed-order emails from being sent.
* Fix - Support custom name and description for Afterpay.
Expand Down
33 changes: 23 additions & 10 deletions client/stripe-utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global wc_stripe_upe_params */
/* global wc_stripe_upe_params, wc */
import { dispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { getAppearance } from '../styles/upe';
Expand All @@ -21,13 +21,24 @@ import {
* @return {StripeServerData} Stripe server data.
*/
const getStripeServerData = () => {
// Classic checkout.
let data = null;

// eslint-disable-next-line camelcase
if ( ! wc_stripe_upe_params ) {
if ( typeof wc_stripe_upe_params !== 'undefined' ) {
data = wc_stripe_upe_params; // eslint-disable-line camelcase
} else if (
typeof wc === 'object' &&
typeof wc.wcSettings !== 'undefined'
) {
// 'getSetting' has this data value on block checkout only.
data = wc.wcSettings?.getSetting( 'getSetting' ) || null;
}

if ( ! data ) {
throw new Error( 'Stripe initialization data is not available' );
}
// eslint-disable-next-line camelcase
return wc_stripe_upe_params;

return data;
};

const isNonFriendlyError = ( type ) =>
Expand Down Expand Up @@ -210,8 +221,8 @@ export { getStripeServerData, getErrorMessageForTypeAndCode };
*/
export const isLinkEnabled = ( paymentMethodsConfig ) => {
return (
paymentMethodsConfig.link !== undefined &&
paymentMethodsConfig.card !== undefined
paymentMethodsConfig?.link !== undefined &&
paymentMethodsConfig?.card !== undefined
);
};

Expand Down Expand Up @@ -505,16 +516,18 @@ export const getPaymentMethodName = ( paymentMethodType ) => {
* @return {boolean} Whether the payment method is restricted to selected billing country.
**/
export const isPaymentMethodRestrictedToLocation = ( upeElement ) => {
const paymentMethodsConfig = getStripeServerData()?.paymentMethodsConfig;
const paymentMethodsConfig =
getStripeServerData()?.paymentMethodsConfig || {};
const paymentMethodType = upeElement.dataset.paymentMethodType;
return !! paymentMethodsConfig[ paymentMethodType ].countries.length;
return !! paymentMethodsConfig[ paymentMethodType ]?.countries.length;
};

/**
* @param {Object} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
**/
export const togglePaymentMethodForCountry = ( upeElement ) => {
const paymentMethodsConfig = getStripeServerData()?.paymentMethodsConfig;
const paymentMethodsConfig =
getStripeServerData()?.paymentMethodsConfig || {};
const paymentMethodType = upeElement.dataset.paymentMethodType;
const supportedCountries =
paymentMethodsConfig[ paymentMethodType ].countries;
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ If you get stuck, you can ask for help in the Plugin Forum.

== Changelog ==

= 8.8.0 - xxxx-xx-xx =
* 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.

= 8.7.0 - xxxx-xx-xx =
* Fix - Prevent duplicate failed-order emails from being sent.
* Fix - Support custom name and description for Afterpay.
Expand Down
Loading