Apply new namespaced script loading

This commit is contained in:
Philipp Stracker 2024-10-08 14:25:43 +02:00
parent ac98924f52
commit 0d72641ded
No known key found for this signature in database
4 changed files with 79 additions and 51 deletions

View file

@ -1,11 +1,10 @@
/* global paypal */
import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher'; import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher';
import ApplePayButton from './ApplepayButton'; import ApplePayButton from './ApplepayButton';
import ContextHandlerFactory from './Context/ContextHandlerFactory'; import ContextHandlerFactory from './Context/ContextHandlerFactory';
class ApplePayManager { class ApplePayManager {
constructor( buttonConfig, ppcpConfig ) { constructor( namespace, buttonConfig, ppcpConfig ) {
this.namespace = namespace;
this.buttonConfig = buttonConfig; this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig; this.ppcpConfig = ppcpConfig;
this.ApplePayConfig = null; this.ApplePayConfig = null;
@ -36,16 +35,18 @@ class ApplePayManager {
// Ensure ApplePayConfig is loaded before proceeding. // Ensure ApplePayConfig is loaded before proceeding.
await this.init(); await this.init();
button.configure( this.ApplePayConfig ); button.configure( this.applePayConfig );
button.init(); button.init();
} }
async init() { async init() {
try { try {
if ( ! this.ApplePayConfig ) { if ( ! this.applePayConfig ) {
this.ApplePayConfig = await paypal.Applepay().config(); this.applePayConfig = await window[ this.namespace ]
.Applepay()
.config();
if ( ! this.ApplePayConfig ) { if ( ! this.applePayConfig ) {
console.error( 'No ApplePayConfig received during init' ); console.error( 'No ApplePayConfig received during init' );
} }
} }

View file

@ -1,32 +1,43 @@
/* global paypal */
import ApplePayButton from './ApplepayButton'; import ApplePayButton from './ApplepayButton';
import ContextHandlerFactory from './Context/ContextHandlerFactory';
class ApplePayManagerBlockEditor { class ApplePayManagerBlockEditor {
constructor( buttonConfig, ppcpConfig ) { constructor( namespace, buttonConfig, ppcpConfig ) {
this.namespace = namespace;
this.buttonConfig = buttonConfig; this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig; 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 init() {
( async () => {
await this.config();
} )();
}
async config() {
try { try {
this.applePayConfig = await ppcpBlocksEditorPaypalApplepay.Applepay().config(); this.applePayConfig = await window[ this.namespace ]
.Applepay()
.config();
const button = new ApplePayButton( this.contextHandler = ContextHandlerFactory.create(
this.ppcpConfig.context,
this.buttonConfig,
this.ppcpConfig,
null
);
const button = ApplePayButton.createButton(
this.ppcpConfig.context, this.ppcpConfig.context,
null, null,
this.buttonConfig, this.buttonConfig,
this.ppcpConfig this.ppcpConfig,
this.contextHandler
); );
button.init( this.applePayConfig ); button.configure( this.applePayConfig );
button.init();
} catch ( error ) { } catch ( error ) {
console.error( 'Failed to initialize Apple Pay:', error ); console.error( 'Failed to initialize Apple Pay:', error );
} }

View file

@ -1,6 +1,7 @@
import { useEffect, useState } from '@wordpress/element'; import { useEffect, useRef, useState } from '@wordpress/element';
import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry'; import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry';
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'; import { __ } from '@wordpress/i18n';
import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading';
import { cartHasSubscriptionProducts } from '../../../ppcp-blocks/resources/js/Helper/Subscription'; import { cartHasSubscriptionProducts } from '../../../ppcp-blocks/resources/js/Helper/Subscription';
import { loadCustomScript } from '@paypal/paypal-js'; import { loadCustomScript } from '@paypal/paypal-js';
import CheckoutHandler from './Context/CheckoutHandler'; import CheckoutHandler from './Context/CheckoutHandler';
@ -12,24 +13,16 @@ const ppcpConfig = ppcpData.scriptData;
const buttonData = wc.wcSettings.getSetting( 'ppcp-applepay_data' ); const buttonData = wc.wcSettings.getSetting( 'ppcp-applepay_data' );
const buttonConfig = buttonData.scriptData; const buttonConfig = buttonData.scriptData;
const dataNamespace = 'ppcpBlocksEditorPaypalApplepay'; const namespace = 'ppcpBlocksPaypalAppglepay';
if ( typeof window.PayPalCommerceGateway === 'undefined' ) { if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
window.PayPalCommerceGateway = ppcpConfig; window.PayPalCommerceGateway = ppcpConfig;
} }
const ApplePayComponent = ( props ) => { const ApplePayComponent = ( props ) => {
const [ bootstrapped, setBootstrapped ] = useState( false );
const [ paypalLoaded, setPaypalLoaded ] = useState( false ); const [ paypalLoaded, setPaypalLoaded ] = useState( false );
const [ applePayLoaded, setApplePayLoaded ] = useState( false ); const [ applePayLoaded, setApplePayLoaded ] = useState( false );
const wrapperRef = useRef( null );
const bootstrap = function () {
const ManagerClass = props.isEditing
? ApplePayManagerBlockEditor
: ApplePayManager;
const manager = new ManagerClass( buttonConfig, ppcpConfig );
manager.init();
};
useEffect( () => { useEffect( () => {
// Load ApplePay SDK // Load ApplePay SDK
@ -39,25 +32,33 @@ const ApplePayComponent = ( props ) => {
ppcpConfig.url_params.components += ',applepay'; ppcpConfig.url_params.components += ',applepay';
if ( props.isEditing ) {
ppcpConfig.data_namespace = dataNamespace;
}
// Load PayPal // Load PayPal
loadPaypalScript( ppcpConfig, () => { loadPayPalScript( namespace, ppcpConfig )
setPaypalLoaded( true ); .then( () => {
} ); setPaypalLoaded( true );
} )
.catch( ( error ) => {
console.error( 'Failed to load PayPal script: ', error );
} );
}, [] ); }, [] );
useEffect( () => { useEffect( () => {
if ( ! bootstrapped && paypalLoaded && applePayLoaded ) { if ( ! paypalLoaded || ! applePayLoaded ) {
setBootstrapped( true ); return;
bootstrap();
} }
}, [ paypalLoaded, applePayLoaded ] );
const ManagerClass = props.isEditing
? ApplePayManagerBlockEditor
: ApplePayManager;
buttonConfig.reactWrapper = wrapperRef.current;
new ManagerClass( namespace, buttonConfig, ppcpConfig );
}, [ paypalLoaded, applePayLoaded, props.isEditing ] );
return ( return (
<div <div
ref={ wrapperRef }
id={ buttonConfig.button.wrapper.replace( '#', '' ) } id={ buttonConfig.button.wrapper.replace( '#', '' ) }
className="ppcp-button-apm ppcp-button-applepay" className="ppcp-button-apm ppcp-button-applepay"
></div> ></div>
@ -75,6 +76,11 @@ if (
registerExpressPaymentMethod( { registerExpressPaymentMethod( {
name: buttonData.id, name: buttonData.id,
title: `PayPal - ${ buttonData.title }`,
description: __(
'Eligible users will see the PayPal button.',
'woocommerce-paypal-payments'
),
label: <div dangerouslySetInnerHTML={ { __html: buttonData.title } } />, label: <div dangerouslySetInnerHTML={ { __html: buttonData.title } } />,
content: <ApplePayComponent isEditing={ false } />, content: <ApplePayComponent isEditing={ false } />,
edit: <ApplePayComponent isEditing={ true } />, edit: <ApplePayComponent isEditing={ true } />,

View file

@ -1,15 +1,21 @@
import { loadCustomScript } from '@paypal/paypal-js'; 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 ApplePayManager from './ApplepayManager';
import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper'; import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper';
( function ( { buttonConfig, ppcpConfig } ) { ( function ( { buttonConfig, ppcpConfig } ) {
const namespace = 'ppcpPaypalApplepay';
function bootstrapPayButton() { function bootstrapPayButton() {
if ( ! buttonConfig || ! ppcpConfig ) { if ( ! buttonConfig || ! ppcpConfig ) {
return; return;
} }
const manager = new ApplePayManager( buttonConfig, ppcpConfig ); const manager = new ApplePayManager(
namespace,
buttonConfig,
ppcpConfig
);
setupButtonEvents( function () { setupButtonEvents( function () {
manager.reinit(); manager.reinit();
@ -61,10 +67,14 @@ import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Hel
} ); } );
// Load PayPal // Load PayPal
loadPaypalScript( ppcpConfig, () => { loadPayPalScript( namespace, ppcpConfig )
paypalLoaded = true; .then( () => {
tryToBoot(); paypalLoaded = true;
} ); tryToBoot();
} )
.catch( ( error ) => {
console.error( 'Failed to load PayPal script: ', error );
} );
} ); } );
} )( { } )( {
buttonConfig: window.wc_ppcp_applepay, buttonConfig: window.wc_ppcp_applepay,