From a467402ae8cc473b906c946cab217af994f5cf5b Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Tue, 8 Oct 2024 14:25:43 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Apply=20new=20namespaced=20scri?= =?UTF-8?q?pt=20loading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Conflicts: # modules/ppcp-applepay/resources/js/ApplepayManager.js # modules/ppcp-applepay/resources/js/boot.js --- .../resources/js/ApplepayManager.js | 10 +++++--- .../js/ApplepayManagerBlockEditor.js | 25 ++++++++++--------- .../ppcp-applepay/resources/js/boot-block.js | 20 +++++++-------- modules/ppcp-applepay/resources/js/boot.js | 17 ++++++++----- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/modules/ppcp-applepay/resources/js/ApplepayManager.js b/modules/ppcp-applepay/resources/js/ApplepayManager.js index 264610f28..40bd340f8 100644 --- a/modules/ppcp-applepay/resources/js/ApplepayManager.js +++ b/modules/ppcp-applepay/resources/js/ApplepayManager.js @@ -1,10 +1,9 @@ -/* global paypal */ - import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher'; import ApplePayButton from './ApplepayButton'; class ApplePayManager { - constructor( buttonConfig, ppcpConfig ) { + constructor( namespace, buttonConfig, ppcpConfig ) { + this.namespace = namespace; this.buttonConfig = buttonConfig; this.ppcpConfig = ppcpConfig; this.ApplePayConfig = null; @@ -45,7 +44,10 @@ class ApplePayManager { * Gets Apple Pay configuration of the PayPal merchant. */ async config() { - this.ApplePayConfig = await paypal.Applepay().config(); + this.ApplePayConfig = await window[ this.namespace ] + .Applepay() + .config(); + return this.ApplePayConfig; } } diff --git a/modules/ppcp-applepay/resources/js/ApplepayManagerBlockEditor.js b/modules/ppcp-applepay/resources/js/ApplepayManagerBlockEditor.js index 7276abbb7..cb8a4ccd7 100644 --- a/modules/ppcp-applepay/resources/js/ApplepayManagerBlockEditor.js +++ b/modules/ppcp-applepay/resources/js/ApplepayManagerBlockEditor.js @@ -1,23 +1,24 @@ -/* global paypal */ - import ApplePayButton from './ApplepayButton'; class ApplePayManagerBlockEditor { - constructor( buttonConfig, ppcpConfig ) { + constructor( namespace, buttonConfig, ppcpConfig ) { + this.namespace = namespace; this.buttonConfig = buttonConfig; this.ppcpConfig = ppcpConfig; - this.applePayConfig = null; + + /* + * On the front-end, the init method is called when a new button context was detected + * via `buttonModuleWatcher`. In the block editor, we do not need to wait for the + * context, but can initialize the button in the next event loop. + */ + setTimeout( () => this.init() ); } - init() { - ( async () => { - await this.config(); - } )(); - } - - async config() { + async init() { try { - this.applePayConfig = await ppcpBlocksEditorPaypalApplepay.Applepay().config(); + this.applePayConfig = await window[ this.namespace ] + .Applepay() + .config(); const button = new ApplePayButton( this.ppcpConfig.context, diff --git a/modules/ppcp-applepay/resources/js/boot-block.js b/modules/ppcp-applepay/resources/js/boot-block.js index ea15477b0..008027e26 100644 --- a/modules/ppcp-applepay/resources/js/boot-block.js +++ b/modules/ppcp-applepay/resources/js/boot-block.js @@ -1,6 +1,6 @@ import { useEffect, useState } from '@wordpress/element'; import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry'; -import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'; +import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading'; import { cartHasSubscriptionProducts } from '../../../ppcp-blocks/resources/js/Helper/Subscription'; import { loadCustomScript } from '@paypal/paypal-js'; import CheckoutHandler from './Context/CheckoutHandler'; @@ -12,7 +12,7 @@ const ppcpConfig = ppcpData.scriptData; const buttonData = wc.wcSettings.getSetting( 'ppcp-applepay_data' ); const buttonConfig = buttonData.scriptData; -const dataNamespace = 'ppcpBlocksEditorPaypalApplepay'; +const namespace = 'ppcpBlocksPaypalApplepay'; if ( typeof window.PayPalCommerceGateway === 'undefined' ) { window.PayPalCommerceGateway = ppcpConfig; @@ -27,7 +27,7 @@ const ApplePayComponent = ( props ) => { const ManagerClass = props.isEditing ? ApplePayManagerBlockEditor : ApplePayManager; - const manager = new ManagerClass( buttonConfig, ppcpConfig ); + const manager = new ManagerClass( namespace, buttonConfig, ppcpConfig ); manager.init(); }; @@ -39,14 +39,14 @@ const ApplePayComponent = ( props ) => { ppcpConfig.url_params.components += ',applepay'; - if ( props.isEditing ) { - ppcpConfig.data_namespace = dataNamespace; - } - // Load PayPal - loadPaypalScript( ppcpConfig, () => { - setPaypalLoaded( true ); - } ); + loadPayPalScript( namespace, ppcpConfig ) + .then( () => { + setPaypalLoaded( true ); + } ) + .catch( ( error ) => { + console.error( 'Failed to load PayPal script: ', error ); + } ); }, [] ); useEffect( () => { diff --git a/modules/ppcp-applepay/resources/js/boot.js b/modules/ppcp-applepay/resources/js/boot.js index 8eddafbcb..509b9e44e 100644 --- a/modules/ppcp-applepay/resources/js/boot.js +++ b/modules/ppcp-applepay/resources/js/boot.js @@ -1,13 +1,14 @@ import { loadCustomScript } from '@paypal/paypal-js'; -import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'; +import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading'; import ApplePayManager from './ApplepayManager'; import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper'; ( function ( { buttonConfig, ppcpConfig, jQuery } ) { + const namespace = 'ppcpPaypalApplepay'; let manager; const bootstrap = function () { - manager = new ApplePayManager( buttonConfig, ppcpConfig ); + manager = new ApplePayManager( namespace, buttonConfig, ppcpConfig ); manager.init(); }; @@ -50,10 +51,14 @@ import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Hel } ); // Load PayPal - loadPaypalScript( ppcpConfig, () => { - paypalLoaded = true; - tryToBoot(); - } ); + loadPayPalScript( namespace, ppcpConfig ) + .then( () => { + paypalLoaded = true; + tryToBoot(); + } ) + .catch( ( error ) => { + console.error( 'Failed to load PayPal script: ', error ); + } ); } ); } )( { buttonConfig: window.wc_ppcp_applepay, From d55314a315ce822d492fefe0ab2b4be4999817ab Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Thu, 10 Oct 2024 14:08:50 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20cart=20response=20when?= =?UTF-8?q?=20shipping=20is=20disabled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New condition prevents a PHP notice which breaks the JSON response --- .../ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php index 70309d907..bfa8ba2fd 100644 --- a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php @@ -129,6 +129,14 @@ class CartScriptParamsEndpoint implements EndpointInterface { WC()->cart->get_shipping_packages() ); + if ( ! count( $calculated_packages ) ) { + // Shipping disabled, or no shipping methods available. + $response['chosen_shipping_methods'] = array(); + $response['shipping_packages'] = array(); + + return $response; + } + $shipping_packages = array(); foreach ( $calculated_packages[0]['rates'] as $rate ) {