2024-07-12 12:58:34 +02:00
|
|
|
import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher';
|
2024-07-23 17:36:10 +02:00
|
|
|
import ApplePayButton from './ApplepayButton';
|
2024-10-07 12:13:46 +02:00
|
|
|
import ContextHandlerFactory from './Context/ContextHandlerFactory';
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2024-07-23 17:36:10 +02:00
|
|
|
class ApplePayManager {
|
2024-10-08 14:26:46 +02:00
|
|
|
#namespace = '';
|
|
|
|
#buttonConfig = null;
|
|
|
|
#ppcpConfig = null;
|
|
|
|
#applePayConfig = null;
|
|
|
|
#contextHandler = null;
|
2024-10-08 14:54:00 +02:00
|
|
|
#transactionInfo = null;
|
2024-10-08 14:26:46 +02:00
|
|
|
#buttons = [];
|
|
|
|
|
2024-10-08 14:25:43 +02:00
|
|
|
constructor( namespace, buttonConfig, ppcpConfig ) {
|
2024-10-08 14:26:46 +02:00
|
|
|
this.#namespace = namespace;
|
|
|
|
this.#buttonConfig = buttonConfig;
|
|
|
|
this.#ppcpConfig = ppcpConfig;
|
2024-07-12 12:58:34 +02:00
|
|
|
|
2024-10-07 12:34:05 +02:00
|
|
|
this.onContextBootstrap = this.onContextBootstrap.bind( this );
|
|
|
|
buttonModuleWatcher.watchContextBootstrap( this.onContextBootstrap );
|
|
|
|
}
|
|
|
|
|
|
|
|
async onContextBootstrap( bootstrap ) {
|
2024-10-08 14:26:46 +02:00
|
|
|
this.#contextHandler = ContextHandlerFactory.create(
|
2024-10-07 12:34:05 +02:00
|
|
|
bootstrap.context,
|
2024-10-08 14:26:46 +02:00
|
|
|
this.#buttonConfig,
|
|
|
|
this.#ppcpConfig,
|
2024-10-07 12:34:05 +02:00
|
|
|
bootstrap.handler
|
|
|
|
);
|
|
|
|
|
|
|
|
const button = ApplePayButton.createButton(
|
|
|
|
bootstrap.context,
|
|
|
|
bootstrap.handler,
|
2024-10-08 14:26:46 +02:00
|
|
|
this.#buttonConfig,
|
|
|
|
this.#ppcpConfig,
|
|
|
|
this.#contextHandler
|
2024-10-07 12:34:05 +02:00
|
|
|
);
|
|
|
|
|
2024-10-08 14:26:46 +02:00
|
|
|
this.#buttons.push( button );
|
2024-10-07 12:34:05 +02:00
|
|
|
|
|
|
|
// Ensure ApplePayConfig is loaded before proceeding.
|
|
|
|
await this.init();
|
|
|
|
|
2024-10-08 14:54:00 +02:00
|
|
|
button.configure( this.#applePayConfig, this.#transactionInfo );
|
2024-10-07 12:34:05 +02:00
|
|
|
button.init();
|
2024-07-12 12:58:34 +02:00
|
|
|
}
|
|
|
|
|
2024-10-07 12:27:20 +02:00
|
|
|
async init() {
|
|
|
|
try {
|
2024-10-08 14:26:46 +02:00
|
|
|
if ( ! this.#applePayConfig ) {
|
|
|
|
this.#applePayConfig = await window[ this.#namespace ]
|
2024-10-08 14:25:43 +02:00
|
|
|
.Applepay()
|
|
|
|
.config();
|
2024-10-07 12:27:20 +02:00
|
|
|
|
2024-10-08 14:26:46 +02:00
|
|
|
if ( ! this.#applePayConfig ) {
|
2024-10-07 12:27:20 +02:00
|
|
|
console.error( 'No ApplePayConfig received during init' );
|
|
|
|
}
|
2024-07-12 12:58:34 +02:00
|
|
|
}
|
2024-10-08 14:54:00 +02:00
|
|
|
|
|
|
|
if ( ! this.#transactionInfo ) {
|
|
|
|
this.#transactionInfo = await this.fetchTransactionInfo();
|
|
|
|
|
|
|
|
if ( ! this.#applePayConfig ) {
|
|
|
|
console.error( 'No transactionInfo found during init' );
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 12:27:20 +02:00
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Error during initialization:', error );
|
|
|
|
}
|
2024-07-12 12:58:34 +02:00
|
|
|
}
|
|
|
|
|
2024-10-08 14:54:00 +02:00
|
|
|
async fetchTransactionInfo() {
|
|
|
|
try {
|
|
|
|
if ( ! this.#contextHandler ) {
|
|
|
|
throw new Error( 'ContextHandler is not initialized' );
|
|
|
|
}
|
|
|
|
return await this.#contextHandler.transactionInfo();
|
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Error fetching transaction info:', error );
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 12:58:34 +02:00
|
|
|
reinit() {
|
2024-10-08 14:26:46 +02:00
|
|
|
for ( const button of this.#buttons ) {
|
2024-07-12 12:58:34 +02:00
|
|
|
button.reinit();
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:36:10 +02:00
|
|
|
export default ApplePayManager;
|