From 1a40e2962dddae37fb5888a9e15981429f962884 Mon Sep 17 00:00:00 2001 From: Mayisha Date: Wed, 28 Aug 2024 11:19:15 +0600 Subject: [PATCH 1/5] fix undefined check and return empty object --- client/stripe-utils/utils.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/client/stripe-utils/utils.js b/client/stripe-utils/utils.js index 736778ab0c..95ad544065 100644 --- a/client/stripe-utils/utils.js +++ b/client/stripe-utils/utils.js @@ -23,8 +23,8 @@ import { const getStripeServerData = () => { // Classic checkout. // eslint-disable-next-line camelcase - if ( ! wc_stripe_upe_params ) { - throw new Error( 'Stripe initialization data is not available' ); + if ( typeof wc_stripe_upe_params === 'undefined' ) { + return {}; } // eslint-disable-next-line camelcase return wc_stripe_upe_params; @@ -210,8 +210,8 @@ export { getStripeServerData, getErrorMessageForTypeAndCode }; */ export const isLinkEnabled = ( paymentMethodsConfig ) => { return ( - paymentMethodsConfig.link !== undefined && - paymentMethodsConfig.card !== undefined + paymentMethodsConfig?.link !== undefined && + paymentMethodsConfig?.card !== undefined ); }; @@ -505,16 +505,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; From 9c168644e1741bd39c79f8a1546dd17e569c629c Mon Sep 17 00:00:00 2001 From: Mayisha Date: Sat, 7 Sep 2024 00:02:57 +0600 Subject: [PATCH 2/5] get data from wcSettings on block checkout if 'wc_stripe_upe_params' is not loaded yet --- client/stripe-utils/utils.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/client/stripe-utils/utils.js b/client/stripe-utils/utils.js index 95ad544065..6eea1b78b5 100644 --- a/client/stripe-utils/utils.js +++ b/client/stripe-utils/utils.js @@ -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'; @@ -21,13 +21,24 @@ import { * @return {StripeServerData} Stripe server data. */ const getStripeServerData = () => { - // Classic checkout. + let data = null; + // eslint-disable-next-line camelcase - if ( typeof wc_stripe_upe_params === 'undefined' ) { - return {}; + 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; } - // eslint-disable-next-line camelcase - return wc_stripe_upe_params; + + if ( ! data ) { + throw new Error( 'Stripe initialization data is not available' ); + } + + return data; }; const isNonFriendlyError = ( type ) => From 3224a5b9f3f987ac41151de71b844e7274815a1f Mon Sep 17 00:00:00 2001 From: Mayisha Date: Sat, 7 Sep 2024 00:28:12 +0600 Subject: [PATCH 3/5] add changelog --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index beba4b5d08..5fef54d70b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -29,6 +29,7 @@ * Fix - Address Klarna availability based on correct presentment currency rules. * Fix - Use correct ISO country code of United Kingdom in supported country and currency list of AliPay and WeChat. * Fix - Prevent duplicate order notes and emails being sent when purchasing subscription products with no initial payment. +* 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.6.1 - 2024-08-09 = * Tweak - Improves the wording of the invalid Stripe keys errors, instructing merchants to click the "Configure connection" button instead of manually setting the keys. diff --git a/readme.txt b/readme.txt index a17f5b53ce..e01f21ad3c 100644 --- a/readme.txt +++ b/readme.txt @@ -157,5 +157,6 @@ If you get stuck, you can ask for help in the Plugin Forum. * Fix - Address Klarna availability based on correct presentment currency rules. * Fix - Use correct ISO country code of United Kingdom in supported country and currency list of AliPay and WeChat. * Fix - Prevent duplicate order notes and emails being sent when purchasing subscription products with no initial payment. +* 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. [See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt). From 964109e711bd767fcdb755ddaf05e05c626afae7 Mon Sep 17 00:00:00 2001 From: Mayisha Date: Mon, 9 Sep 2024 23:37:10 +0600 Subject: [PATCH 4/5] update changelog --- changelog.txt | 4 +++- readme.txt | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 5fef54d70b..6d8adf5d90 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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. @@ -29,7 +32,6 @@ * Fix - Address Klarna availability based on correct presentment currency rules. * Fix - Use correct ISO country code of United Kingdom in supported country and currency list of AliPay and WeChat. * Fix - Prevent duplicate order notes and emails being sent when purchasing subscription products with no initial payment. -* 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.6.1 - 2024-08-09 = * Tweak - Improves the wording of the invalid Stripe keys errors, instructing merchants to click the "Configure connection" button instead of manually setting the keys. diff --git a/readme.txt b/readme.txt index e01f21ad3c..4f3fe3522b 100644 --- a/readme.txt +++ b/readme.txt @@ -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. From 070d9db607fcc7942a4867ce41018e95170732da Mon Sep 17 00:00:00 2001 From: Mayisha Date: Mon, 9 Sep 2024 23:38:58 +0600 Subject: [PATCH 5/5] remove duplicate entry from readme.txt --- readme.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/readme.txt b/readme.txt index 4f3fe3522b..942565733a 100644 --- a/readme.txt +++ b/readme.txt @@ -160,6 +160,5 @@ If you get stuck, you can ask for help in the Plugin Forum. * Fix - Address Klarna availability based on correct presentment currency rules. * Fix - Use correct ISO country code of United Kingdom in supported country and currency list of AliPay and WeChat. * Fix - Prevent duplicate order notes and emails being sent when purchasing subscription products with no initial payment. -* 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. [See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).