export type PaymentType = 'JOLI_COIN' | 'JOLI_COIN_IAP' | 'JOLI_GEM_IAP'; // support both jolicoin and gem import { StandardResponse } from '@jolibox/types'; import { reportError } from '@/common/report/errors/report'; import { BaseError } from '@jolibox/common'; export type PaymentResult = StandardResponse; export interface PaymentHandlerMap { JOLI_COIN: (productId: string) => Promise>; // jolicoin JOLI_COIN_IAP: (params: { productId: string; appStoreProductId: string; }) => Promise>; // jolicoin iap JOLI_GEM_IAP: (params: { productId: string; appStoreProductId: string; }) => Promise>; // gem iap } export type PaymentHandler = PaymentHandlerMap[T]; export function createPaymentHelper() { const paymentHandlers = new Map>(); return { registerPaymentHandler(type: T, handler: PaymentHandler) { paymentHandlers.set(type, handler); }, async invokePayment(type: T, ...args: Parameters>) { const paymentHandler = paymentHandlers.get(type); if (!paymentHandler) { return { code: 'FAILED', message: `[joliboxSDK]: ${type} payment not supported in this platform` }; } try { const result = await paymentHandler(...args); return result; } catch (e) { reportError(e as BaseError); return { code: 'FAILED', message: `[joliboxSDK]: paymenterror ${e}` }; } } }; } export type PaymentHelper = ReturnType;