window.Mollie = { mollieWindow: null, callback: null, startMolliePayment: function (paymentUrl, callback) { this.closeMolliePayment(callback); // Set up the message listener for the payment window window.addEventListener('message', (event) => { if (event.data.type === 'mollie' && event.data.result) { callback(event.data.result); console.log("Mollie payment result: ", event.data.result); this.endMolliePayment(); } }) if (Telegram.WebApp.initData) { this.mollieWindow = Telegram.WebApp.openLink(paymentUrl); } else { this.mollieWindow = window.open(paymentUrl, 'newwin'); } }, closeMolliePayment: function (callback) { if (this.mollieWindow) { this.mollieWindow.close(); this.mollieWindow = null; callback("Payment.Cancelled"); } }, endMolliePayment: function () { if (this.mollieWindow) { this.mollieWindow.close(); this.mollieWindow = null; } }, getCallbackUrl: function () { return `${window.location.origin}/mollie/mollie-payment-result.html`; } };