import {z} from 'zod' export type FiatCurrencyCode = 'USD' | 'EUR' | 'GBP'; export type Money = {value: number, currency: FiatCurrencyCode}; export type IndividualDetails = {firstName: string, lastName: string, dateOfBirth: string}; export type PayoutRequestAddress = {addressLine1: string, addressLine2?: string | undefined, region?: string | undefined, city: string, country: string, postCode?: string | undefined}; export type Address = {addressLine1?: string | undefined, addressLine2?: string | undefined, region?: string | undefined, city?: string | undefined, country: string, postCode?: string | undefined}; export type IntermediaryBank = {bankName?: string | undefined, code: string, address?: Address | undefined}; export type BankDetails = {accountNumber?: string | undefined, code?: string | undefined, address?: Address | undefined, accountType?: ('CHEQUE' | 'SAVINGS' | 'TRANSMISSION') | undefined, bankTypeIdentifier?: ('SCAN' | 'IBAN' | 'SWIFT' | 'ABA' | 'CUBIX' | 'UNKNOWN') | undefined, intermediaryBanks?: Array | undefined}; export type PayoutBeneficiaryDetails = {beneficiaryType: 'SELF_OWNED' | 'THIRD_PARTY', transferDestination: 'INTERNATIONAL' | 'LOCAL', currency: 'USD' | 'EUR' | 'GBP', individualDetails?: IndividualDetails | undefined, businessDetails?: ({businessName: string, companyRegistrationNumber?: string | undefined}) | undefined, address: PayoutRequestAddress, bankDetails: BankDetails}; export type Metadata = {[key: string]: string}; export type RequestDetails = {originator: {ipAddress?: string | undefined}}; export type UnifiedPayoutRequest = {walletId: string, amount: Money, paymentReference: string, instruction: {type: 'FIAT', paymentMethod?: ('ACH' | 'ACH_SAME_DAY' | 'SEPA_CT' | 'SEPA_INST' | 'FEDWIRE' | 'RTP' | 'FEDNOW') | undefined, beneficiary: {details?: PayoutBeneficiaryDetails | undefined}}, metadata?: Metadata | undefined, requestDetails?: RequestDetails | undefined}; export type CreateApiPaymentV1PayoutsArgs = {'X-Idempotency-Key': string, body: UnifiedPayoutRequest}; export type CreateApiPaymentV1PayoutsOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const fiatCurrencyCode = z.enum(["USD", "EUR", "GBP"]); export const money = z.object({value: z.number(), currency: fiatCurrencyCode}); export const companyDetails = z.object({name: z.string()}); export const individualDetails = z.object({firstName: z.string(), lastName: z.string(), dateOfBirth: z.string()}); export const bankAccount = z.object({format: z.enum(["SCAN", "IBAN", "SWIFT", "ABA", "CUBIX"]).optional(), bankName: z.string().optional(), accountNumber: z.string().optional(), bankCode: z.string().optional(), bankAddress: z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), region: z.string().optional(), city: z.string().optional(), countryCode: z.string().optional(), postCode: z.string().optional(), fullAddress: z.string().optional()}).optional()}); export const unifiedPayoutDetails = z.object({type: z.enum(["FIAT", "CRYPTO"]).optional(), beneficiary: z.object({entityType: z.enum(["INDIVIDUAL", "COMPANY"]).optional(), companyDetails: companyDetails.optional(), individualDetails: individualDetails.optional(), address: z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), region: z.string().optional(), city: z.string().optional(), countryCode: z.string().optional(), postCode: z.string().optional(), fullAddress: z.string().optional()}).optional(), bankAccount: bankAccount.optional(), correspondentBic: z.string().optional()}).optional()}); export const metadata = z.record(z.string(), z.string().max(100)); export const unifiedPayoutResponse = z.object({transactionReference: z.string().optional(), paymentReference: z.string().optional(), amount: money.optional(), fee: money.optional(), walletId: z.string().optional(), status: z.enum(["PENDING_APPROVAL", "PROCESSING", "COMPLETED", "FAILED", "CANCELLED", "RETURNED", "ON_HOLD"]).optional(), createdAt: z.string().optional(), details: unifiedPayoutDetails.optional(), metadata: metadata.optional()}); 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 }; /** * Initiate Payout * * POST /payment/v1/payouts */ export const createApiPaymentV1Payouts = async (args: CreateApiPaymentV1PayoutsArgs, options?: CreateApiPaymentV1PayoutsOptions) => { 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['X-Idempotency-Key'] != null) headers.set("X-Idempotency-Key", String(args['X-Idempotency-Key'])) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/payment/v1/payouts${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(unifiedPayoutResponse, await res.json()) };