export class AppBridgeHelper { static startListener(callback: (msg: MessageEvent) => void) { window.addEventListener('message', callback, false); } static stopListener(callback: (msg: MessageEvent) => void) { window.removeEventListener('message', callback, false); } static closeLoader() { const event: AppEvent = { type: AppBridgeMessageType.CLOSE_LOADER, data: {}, }; AppBridgeHelper.sendEvent(event); } static startMerchantPayment(merchantPaymentId: string) { const event: AppEvent = { type: AppBridgeMessageType.START_MERCHANT_PAYMENT, data: { merchantPaymentId, }, }; AppBridgeHelper.sendEvent(event); } static reAuthorizeApp(data: ReAuthorizeAppData) { const event: AppEvent = { type: AppBridgeMessageType.RE_AUTHORIZE_APP, data, }; AppBridgeHelper.sendEvent(event); } static openProductPage(productId: string) { const event: AppEvent = { type: AppBridgeMessageType.OPEN_PRODUCT_PAGE, data: { productId, }, }; AppBridgeHelper.sendEvent(event); } static openOrderPage(orderId: string) { const event: AppEvent = { type: AppBridgeMessageType.OPEN_ORDER_PAGE, data: { orderId, }, }; AppBridgeHelper.sendEvent(event); } static openWalletPage() { const event: AppEvent = { type: AppBridgeMessageType.OPEN_WALLET_PAGE, data: {}, }; AppBridgeHelper.sendEvent(event); } static closeApp() { const event: AppEvent = { type: AppBridgeMessageType.CLOSE_APP, data: {}, }; AppBridgeHelper.sendEvent(event); } static getNewToken(): Promise { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject('Timed out getNewToken'), 30000); try { const callback = (msg: MessageEvent) => { if (msg.data?.type === AppBridgeCallbackMessageType.REQUEST_TOKEN_RESPONSE) { const token = msg.data?.data?.token; resolve(token); AppBridgeHelper.stopListener(callback); clearTimeout(timer); } }; AppBridgeHelper.startListener(callback); const event: AppEvent = { type: AppBridgeMessageType.REQUEST_TOKEN, data: {}, }; AppBridgeHelper.sendEvent(event); } catch (e) { clearTimeout(timer); reject(e); } }); } static getAuthorizedAppId(): Promise { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject('Timed out getAuthorizedAppId'), 5000); try { const callback = (msg: MessageEvent) => { console.log('msg', msg); if (msg.data?.type === AppBridgeCallbackMessageType.AUTHORIZED_APP_ID_RESPONSE) { const authorizedAppId = msg.data?.data?.authorizedAppId; resolve(authorizedAppId); AppBridgeHelper.stopListener(callback); clearTimeout(timer); } }; AppBridgeHelper.startListener(callback); const event: AppEvent = { type: AppBridgeMessageType.AUTHORIZED_APP_ID, data: {}, }; AppBridgeHelper.sendEvent(event); } catch (e) { clearTimeout(timer); reject(e); } }); } static getDashboardLanguage(): Promise { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject('Timed out getDashboardLanguage'), 30000); try { const callback = (msg: MessageEvent) => { if (msg.data?.type === AppBridgeCallbackMessageType.GET_DASHBOARD_LANGUAGE_RESPONSE) { const lang = msg.data?.data?.lang; resolve(lang); AppBridgeHelper.stopListener(callback); clearTimeout(timer); } }; AppBridgeHelper.startListener(callback); const event: AppEvent = { type: AppBridgeMessageType.GET_DASHBOARD_LANGUAGE, data: {}, }; AppBridgeHelper.sendEvent(event); } catch (e) { clearTimeout(timer); reject(e); } }); } static getMeData(): Promise { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject('Timed out getMeData'), 30000); try { const callback = (msg: MessageEvent) => { console.log('msg', msg); if (msg.data?.type === AppBridgeCallbackMessageType.ME_RESPONSE) { const me = msg.data?.data?.me resolve(me); AppBridgeHelper.stopListener(callback); clearTimeout(timer); } }; AppBridgeHelper.startListener(callback); const event: AppEvent = { type: AppBridgeMessageType.ME, data: {}, }; AppBridgeHelper.sendEvent(event); } catch (e) { clearTimeout(timer); reject(e); } }); } static sendEvent(event: AppEvent) { //@ts-ignore window.top.postMessage(event, '*'); } } export enum AppBridgeMessageType { CLOSE_LOADER = 'CLOSE_LOADER', START_MERCHANT_PAYMENT = 'START_MERCHANT_PAYMENT', RE_AUTHORIZE_APP = 'RE_AUTHORIZE_APP', OPEN_PRODUCT_PAGE = 'OPEN_PRODUCT_PAGE', OPEN_ORDER_PAGE = 'OPEN_ORDER_PAGE', OPEN_WALLET_PAGE = 'OPEN_WALLET_PAGE', REQUEST_TOKEN = 'REQUEST_TOKEN', CLOSE_APP = 'CLOSE_APP', AUTHORIZED_APP_ID = 'AUTHORIZED_APP_ID', GET_DASHBOARD_LANGUAGE = 'GET_DASHBOARD_LANGUAGE', ME = "ME" } export enum AppBridgeCallbackMessageType { REQUEST_TOKEN_RESPONSE = 'REQUEST_TOKEN_RESPONSE', AUTHORIZED_APP_ID_RESPONSE = 'AUTHORIZED_APP_ID_RESPONSE', GET_DASHBOARD_LANGUAGE_RESPONSE = 'GET_DASHBOARD_LANGUAGE_RESPONSE', ME_RESPONSE = "ME_RESPONSE" } export type AppEvent = { type: AppBridgeMessageType; data: any }; export type AppCallbackEvent = { type: AppBridgeCallbackMessageType; data: any }; export type ReAuthorizeAppData = { redirectUri: string; state: string; scope: string };