import {z} from 'zod' export type Metadata = {[key: string]: string}; export type TransferRequestV2 = {walletId: string, amount: number, currency: string, reference: string, beneficiary: {walletId: string}, metadata?: Metadata | undefined}; export type CreateApiPaymentV2TransfersArgs = {'Idempotency-Key': string, body: TransferRequestV2}; export type CreateApiPaymentV2TransfersOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const moneyFeeV2 = z.object({amount: z.number(), currency: z.string()}); export const transferIndividual = z.object({type: z.literal("INDIVIDUAL"), firstName: z.string().min(1), lastName: z.string().min(1)}); export const transferCompany = z.object({type: z.literal("COMPANY").optional(), legalName: z.string().min(1)}); export const metadata = z.record(z.string(), z.string().max(100)); export const transferResponseV2 = z.object({id: z.string(), reference: z.string(), type: z.literal("payment:transfer"), status: z.enum(["PROCESSING", "COMPLETED", "FAILED"]), method: z.literal("BOOK"), fees: z.object({processingFee: moneyFeeV2.optional()}), originator: z.object({amount: z.number(), currency: z.string(), walletId: z.string(), entity: z.discriminatedUnion("type", [transferIndividual, transferCompany])}), beneficiary: z.object({amount: z.number(), currency: z.string(), walletId: z.string(), entity: z.discriminatedUnion("type", [transferIndividual, transferCompany]).optional()}), metadata: metadata.optional(), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Create internal transfer * * POST /payment/v2/transfers */ export const createApiPaymentV2Transfers = async (args: CreateApiPaymentV2TransfersArgs, options?: CreateApiPaymentV2TransfersOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/payment/v2/transfers${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(transferResponseV2, await res.json()) };