import { innerFetch as fetch } from '@/native/network'; import { BasePaymentRegister, createPaymentError, createPaymentInternalError } from './base'; import { PaymentErrorCodeMap, IPlaceOrderResponse } from './type'; import { ResponseType, StandardResponse } from '@jolibox/types'; export interface IPlaceOrderJoliCoinParamas { productId: string; } export interface IJoliCoinProductInfo { quantity: number; } export interface IJolicoinPaymentContext { type: 'JOLI_COIN_ORDER_CONTEXT'; state: 'TO_CHECKOUT' | 'TO_VALIDATE'; productId: string; orderId: string; } class JolicoinPaymentResiter extends BasePaymentRegister { constructor(readonly productId: string) { super(); } async placeOrder( params: IPlaceOrderJoliCoinParamas ): Promise, unknown>> { try { const { response } = await fetch>( '/api/orders/checkout/joli-coin', { method: 'POST', data: { productId: params.productId } } ); if (response.code !== 'SUCCESS') { throw createPaymentError( response.errMsg ?? response.message ?? '', PaymentErrorCodeMap.JolicoinPlaceOrderRequestFailed ); } return { code: 'SUCCESS' as ResponseType, message: response.message ?? 'placeorder jolicoin success', data: response.data }; } catch (e) { console.info(`[JoliboxSDK] placeorder failed`, e); throw createPaymentInternalError( JSON.stringify(e) ?? 'jolicoin placeorder failed', PaymentErrorCodeMap.JolicoinPlaceOrderRequestFailed ); } } generatePaymentContext = (): IJolicoinPaymentContext => { return { type: 'JOLI_COIN_ORDER_CONTEXT', state: 'TO_VALIDATE', productId: this.productId, orderId: this.orderId! }; }; pay = async (): Promise> => { const callbackUrl = this.generateCallbackUrl(); console.info('callbackUrl', callbackUrl); return { code: 'SUCCESS' as ResponseType, message: 'jolicoin payment success' }; }; } export const createJolicoinPaymentHandler = () => { const productPaymentMap = new Map(); return async (productId: string) => { let instance = productPaymentMap.get(productId); if (!instance) { instance = new JolicoinPaymentResiter(productId); productPaymentMap.set(productId, instance); } await instance.startPayment({ productId }); // TODO: need to wait for the payment to be successful return { code: 'SUCCESS' as ResponseType, message: 'jolicoin payment success' }; }; };