2024-07-23 17:36:10 +02:00
|
|
|
import ApplePayButton from './ApplepayButton';
|
2024-10-08 14:25:43 +02:00
|
|
|
import ContextHandlerFactory from './Context/ContextHandlerFactory';
|
2024-07-23 17:36:10 +02:00
|
|
|
|
|
|
|
class ApplePayManagerBlockEditor {
|
2024-10-08 14:25:43 +02:00
|
|
|
constructor( namespace, buttonConfig, ppcpConfig ) {
|
|
|
|
this.namespace = namespace;
|
2024-07-12 12:58:34 +02:00
|
|
|
this.buttonConfig = buttonConfig;
|
|
|
|
this.ppcpConfig = ppcpConfig;
|
|
|
|
|
2024-10-08 14:25:43 +02:00
|
|
|
/*
|
|
|
|
* 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() );
|
2024-07-12 12:58:34 +02:00
|
|
|
}
|
|
|
|
|
2024-10-08 14:25:43 +02:00
|
|
|
async init() {
|
2024-07-12 12:58:34 +02:00
|
|
|
try {
|
2024-10-08 14:25:43 +02:00
|
|
|
this.applePayConfig = await window[ this.namespace ]
|
|
|
|
.Applepay()
|
|
|
|
.config();
|
|
|
|
|
|
|
|
this.contextHandler = ContextHandlerFactory.create(
|
|
|
|
this.ppcpConfig.context,
|
|
|
|
this.buttonConfig,
|
|
|
|
this.ppcpConfig,
|
|
|
|
null
|
|
|
|
);
|
2024-07-12 12:58:34 +02:00
|
|
|
|
2024-10-08 14:25:43 +02:00
|
|
|
const button = ApplePayButton.createButton(
|
2024-07-12 12:58:34 +02:00
|
|
|
this.ppcpConfig.context,
|
|
|
|
null,
|
|
|
|
this.buttonConfig,
|
2024-10-08 14:25:43 +02:00
|
|
|
this.ppcpConfig,
|
|
|
|
this.contextHandler
|
2024-07-12 12:58:34 +02:00
|
|
|
);
|
|
|
|
|
2024-10-08 14:25:43 +02:00
|
|
|
button.configure( this.applePayConfig );
|
|
|
|
button.init();
|
2024-07-12 12:58:34 +02:00
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Failed to initialize Apple Pay:', error );
|
|
|
|
}
|
|
|
|
}
|
2024-04-23 15:00:05 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:36:10 +02:00
|
|
|
export default ApplePayManagerBlockEditor;
|