import { IJoliCoin, IGem } from '@/common/rewards/type'; import { rewardsCommands } from '../rewards-command'; import { IHttpClient } from '@/common/http'; import { unlockOptionsHandler } from '../../common'; import { rewardsEmitter } from '@/common/rewards/reward-emitter'; import { createEventPromiseHandler } from '../../event-listener'; import { IPaymentEvent, PaymentResultEventName, InvokePaymentEventName } from '@/common/rewards/reward-emitter'; import { CURRENCY_HANDLERS, CurrencyType } from './currency-handlers'; import { refreshGemBalanceCache } from '../joligem/cached-fetch-gem-balance'; import { refreshBalanceCache } from '../jolicoin/cached-fetch-balance'; export const registerUsePaymentCommand = ( prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', currency: 'JOLI_COIN' | 'JOLI_GEM', params: { httpClient: IHttpClient; initiateAndAwaitPayment: (params: { userCurrency: T; currencyQuantity: number; currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM'; }) => Promise<'SUCCESS' | 'FAILED' | 'CANCEL'>; } ) => { const { httpClient, initiateAndAwaitPayment } = params; rewardsCommands.registerCommand(`Rewards.${prefix}.usePayment`, async () => { const unlockOptions = await unlockOptionsHandler.getData(); // Use currency handler for balance and quantity calculation const handler = CURRENCY_HANDLERS[currency as CurrencyType]; const newBalance = await handler.fetchBalance(httpClient); const currencyQuantity = handler.getQuantity(unlockOptions); const currentUser = handler.getCurrentUser(unlockOptions); const newUser = handler.createUpdatedUser(newBalance?.balance ?? 0, currentUser.enableAutoDeduct); const updateKey = handler.getUpdateKey(); unlockOptionsHandler.updateData({ [updateKey]: newUser }); console.log('-----unlockOptions payment-----', unlockOptions, newUser, currencyQuantity, currency); if (!handler.isEnough(unlockOptions, newUser)) { const paymentResult = await initiateAndAwaitPayment({ userCurrency: newUser as T, currencyQuantity, currency: currency as T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM' }); if (currency === 'JOLI_GEM') { refreshGemBalanceCache(httpClient); } else { refreshBalanceCache(httpClient); } return { result: paymentResult === 'SUCCESS' ? 'SUCCESS' : 'FAILED' }; } return { result: 'CONTINUE' }; }); }; // events export const createInitiateAndAwaitPayment = ( type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', buttons: { confirmButtonText: string; cancelButtonText: string; } ) => { return async (params: { userCurrency: T; currencyQuantity: number; currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM'; }) => { const paymentHandler = createEventPromiseHandler( rewardsEmitter, PaymentResultEventName ); rewardsEmitter.emit(InvokePaymentEventName, type, { userCurrency: params.userCurrency as T, quantity: params.currencyQuantity, currency: params.currency, enableAutoDeduct: params.userCurrency.enableAutoDeduct, confirmButtonText: buttons.confirmButtonText, cancelButtonText: buttons.cancelButtonText }); // Wait for payment result const paymentResult = await paymentHandler.getFreshData(); return paymentResult.paymentResult; }; };