import {z} from 'zod' export type BankAccountTransferRequest = {amount: number, accountNumber: string, accountName: string, bankCode: string, merchantTxRef: string, senderName?: string | undefined, narration?: string | undefined}; export type CreateApiV2TransfersBankSubAccountIdArgs = {accountId: string, subAccountId: string, body: BankAccountTransferRequest}; export type CreateApiV2TransfersBankSubAccountIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountTransferMetaObject = z.object({api_rrn: z.string().optional(), narration: z.string().optional(), recipientName: z.string().optional(), sender_name: z.string().optional(), merchantTxRef: z.string().optional(), api_client_id: z.string().optional(), currency: z.string().optional(), hooksEligible: z.string().optional(), banking_entity_id: z.string().optional(), banking_entity_user_id: z.string().optional(), banking_entity_type: z.string().optional(), self_transaction: z.string().optional(), transactionCategory: z.string().optional(), accountNumber: z.string().optional(), bankName: z.string().optional(), bankCode: z.string().optional(), sessionId: z.string().optional(), user_referral_code: z.string().optional(), amount_charged: z.string().optional(), paymentVendor: z.string().optional(), wallet_balance: z.string().optional(), wallet_currency: z.string().optional(), paymentVendorReference: z.string().optional(), agent_commission: z.string().optional(), useV2Fulfilment: z.string().optional()}); export const bankAccountTransferResult = z.object({amount: z.string(), source: z.string().optional(), sourceUserId: z.string().optional(), customerBillerId: z.string().optional(), productId: z.string().optional(), meta: bankAccountTransferMetaObject, fee: z.number(), timeCreated: z.string(), id: z.string(), type: z.enum(["withdrawal", "purchase", "transfer", "p2p", "online_checkout", "qrt_credit", "qrt_debit"]), status: z.enum(["SUCCESS", "PENDING_BILLING"])}); export const createApiV2TransfersBankSubAccountIdResponse = z.object({code: z.enum(["200", "201", "400", "401", "403", "429", "500"]), description: z.string(), data: bankAccountTransferResult}); 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 }; /** * Perform bank account transfer from the sub account * * POST /v2/transfers/bank/{subAccountId} */ export const createApiV2TransfersBankSubAccountId = async (args: CreateApiV2TransfersBankSubAccountIdArgs, options?: CreateApiV2TransfersBankSubAccountIdOptions) => { const {baseUrl = "https://api.nomba.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.accountId != null) headers.set("accountId", String(args.accountId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/transfers/bank/${encodeURIComponent(String(args.subAccountId))}${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(createApiV2TransfersBankSubAccountIdResponse, await res.json()) };