import {z} from 'zod' export type CollectionSimulationCurrency = 'NGN' | 'KES' | 'GHS' | 'GBP' | 'EUR' | 'USD' | 'UGX' | 'TZS' | 'ZAR' | 'ZMW' | 'CAD'; export type CollectionSimulationParty = {name: string, accountNumber: string, interacEmail?: string | undefined, bankCode?: string | undefined, bankName?: string | undefined} | {name: string, accountNumber?: string | undefined, interacEmail: string, bankCode?: string | undefined, bankName?: string | undefined}; export type CollectionSimulationRequest = {amount: number, currency: CollectionSimulationCurrency, payer: CollectionSimulationParty, payee: CollectionSimulationParty, reference?: string | undefined, narration?: string | undefined}; export type CreateApiCollectionsTransferSimulateArgs = {body: CollectionSimulationRequest}; export type CreateApiCollectionsTransferSimulateOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const collectionSimulationBankDetails = z.object({bankName: z.string().nullable(), bankCode: z.string().nullable(), swiftCode: z.string().nullable(), bic: z.string().nullable()}); export const collectionSimulationCurrency = z.enum(["NGN", "KES", "GHS", "GBP", "EUR", "USD", "UGX", "TZS", "ZAR", "ZMW", "CAD"]); export const collectionSimulationData = z.object({_id: z.string(), business: z.string(), virtualAccount: z.string().optional(), virtualAccountType: z.string().optional(), sessionId: z.string(), senderAccountName: z.string().nullable(), senderAccountNumber: z.string().nullable(), senderBankName: z.string().nullable(), senderBankDetails: collectionSimulationBankDetails, recipientAccountName: z.string().nullable(), recipientAccountNumber: z.string().nullable(), recipientBankName: z.string().nullable(), sourceCurrency: collectionSimulationCurrency, destinationCurrency: collectionSimulationCurrency, sourceAmount: z.number(), destinationAmount: z.number(), description: z.string(), amountReceived: z.number(), fee: z.number(), customerName: z.string(), settlementDestination: z.string(), status: z.enum(["successful", "failed", "pending"]), initiatedAt: z.string(), createdAt: z.string(), updatedAt: z.string(), reference: z.string(), metadata: z.object({customerId: z.string().optional()}).and(z.record(z.string(), z.unknown())).optional()}); export const collectionSimulationResponse = z.object({success: z.literal(true), message: z.literal("Collection processed successfully"), data: collectionSimulationData}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Simulate a collection * * POST /collections/transfer/simulate */ export const createApiCollectionsTransferSimulate = async (args: CreateApiCollectionsTransferSimulateArgs, options?: CreateApiCollectionsTransferSimulateOptions) => { const {baseUrl = "https://sandboxapi.fincra.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/collections/transfer/simulate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(collectionSimulationResponse, await res.json()) };