woocommerce-paypal-payments/modules/ppcp-applepay/resources/js/ApplepayManager.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-07-12 12:58:34 +02:00
import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher';
import ApplePayButton from './ApplepayButton';
import ContextHandlerFactory from './Context/ContextHandlerFactory';
2023-08-31 12:48:01 +02:00
class ApplePayManager {
#namespace = '';
#buttonConfig = null;
#ppcpConfig = null;
#applePayConfig = null;
#contextHandler = null;
#buttons = [];
constructor( namespace, buttonConfig, ppcpConfig ) {
this.#namespace = namespace;
this.#buttonConfig = buttonConfig;
this.#ppcpConfig = ppcpConfig;
2024-07-12 12:58:34 +02:00
this.onContextBootstrap = this.onContextBootstrap.bind( this );
buttonModuleWatcher.watchContextBootstrap( this.onContextBootstrap );
}
async onContextBootstrap( bootstrap ) {
this.#contextHandler = ContextHandlerFactory.create(
bootstrap.context,
this.#buttonConfig,
this.#ppcpConfig,
bootstrap.handler
);
const button = ApplePayButton.createButton(
bootstrap.context,
bootstrap.handler,
this.#buttonConfig,
this.#ppcpConfig,
this.#contextHandler
);
this.#buttons.push( button );
// Ensure ApplePayConfig is loaded before proceeding.
await this.init();
button.configure( this.#applePayConfig );
button.init();
2024-07-12 12:58:34 +02:00
}
async init() {
try {
if ( ! this.#applePayConfig ) {
this.#applePayConfig = await window[ this.#namespace ]
.Applepay()
.config();
if ( ! this.#applePayConfig ) {
console.error( 'No ApplePayConfig received during init' );
}
2024-07-12 12:58:34 +02:00
}
} catch ( error ) {
console.error( 'Error during initialization:', error );
}
2024-07-12 12:58:34 +02:00
}
reinit() {
for ( const button of this.#buttons ) {
2024-07-12 12:58:34 +02:00
button.reinit();
}
}
2023-08-31 12:48:01 +02:00
}
export default ApplePayManager;