Google Pay: Fix the incorrect popup triggering (2645)

This commit is contained in:
Daniel Dudzic 2024-07-12 13:49:50 +02:00
parent 36a13f6500
commit 4b8843d93d
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
3 changed files with 96 additions and 56 deletions

View file

@ -1,5 +1,6 @@
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
import GooglepayButton from "./GooglepayButton";
import ContextHandlerFactory from "./Context/ContextHandlerFactory";
class GooglepayManager {
@ -8,34 +9,72 @@ class GooglepayManager {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.googlePayConfig = null;
this.transactionInfo = null;
this.contextHandler = null;
this.buttons = [];
buttonModuleWatcher.watchContextBootstrap((bootstrap) => {
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);
if (this.googlePayConfig) {
button.init(this.googlePayConfig);
// 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);
}
}
});
}
init() {
(async () => {
// Gets GooglePay configuration of the PayPal merchant.
this.googlePayConfig = await paypal.Googlepay().config();
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);
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() {