mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
88 lines
2.7 KiB
JavaScript
88 lines
2.7 KiB
JavaScript
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
|
|
import GooglepayButton from "./GooglepayButton";
|
|
import ContextHandlerFactory from "./Context/ContextHandlerFactory";
|
|
|
|
class GooglepayManager {
|
|
|
|
constructor(buttonConfig, ppcpConfig) {
|
|
|
|
this.buttonConfig = buttonConfig;
|
|
this.ppcpConfig = ppcpConfig;
|
|
this.googlePayConfig = null;
|
|
this.transactionInfo = null;
|
|
this.contextHandler = null;
|
|
|
|
this.buttons = [];
|
|
|
|
buttonModuleWatcher.watchContextBootstrap(async (bootstrap) => {
|
|
if (!this.contextHandler) {
|
|
this.contextHandler = ContextHandlerFactory.create(
|
|
bootstrap.context,
|
|
buttonConfig,
|
|
ppcpConfig,
|
|
bootstrap.handler
|
|
);
|
|
}
|
|
|
|
const button = new GooglepayButton(
|
|
bootstrap.context,
|
|
bootstrap.handler,
|
|
buttonConfig,
|
|
ppcpConfig,
|
|
this.contextHandler
|
|
);
|
|
|
|
this.buttons.push(button);
|
|
|
|
// Initialize button only if googlePayConfig and transactionInfo are already fetched.
|
|
if (this.googlePayConfig && this.transactionInfo) {
|
|
button.init(this.googlePayConfig, this.transactionInfo);
|
|
} else {
|
|
await this.init();
|
|
if (this.googlePayConfig && this.transactionInfo) {
|
|
button.init(this.googlePayConfig, this.transactionInfo);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
async init() {
|
|
try {
|
|
if (!this.googlePayConfig) {
|
|
// Gets GooglePay configuration of the PayPal merchant.
|
|
this.googlePayConfig = await paypal.Googlepay().config();
|
|
}
|
|
|
|
if (!this.transactionInfo) {
|
|
this.transactionInfo = await this.fetchTransactionInfo();
|
|
}
|
|
|
|
for (const button of this.buttons) {
|
|
button.init(this.googlePayConfig, this.transactionInfo);
|
|
}
|
|
} catch(error) {
|
|
console.error('Error during initialization:', error);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
reinit() {
|
|
for (const button of this.buttons) {
|
|
button.reinit();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
export default GooglepayManager;
|