import {z} from 'zod' export type CustomerId = string; export type PaymentMethodInstitution = {institutionId?: string | undefined, institutionName?: string | undefined, institutionCode?: string | undefined, institutionAddress?: string | undefined, institutionAddressLine1?: string | undefined, institutionAddressCity?: string | undefined, institutionAddressState?: string | undefined, institutionAddressZip?: string | undefined, institutionAddressCountry?: string | undefined, institutionBranch?: string | undefined, correspondentBankName?: string | undefined, correspondentBankAccountNumber?: string | undefined}; export type PaymentMethodRecipient = {recipientEmail?: string | undefined, recipientPhone?: string | undefined, recipientAddress?: string | undefined, recipientAddressLine1?: string | undefined, recipientAddressCity?: string | undefined, recipientAddressState?: string | undefined, recipientAddressZip?: string | undefined, recipientAddressCountry?: string | undefined, recipientIdType?: ('national_id' | 'passport' | 'drivers_license' | 'voters_card' | 'residence_permit' | 'bvn' | 'tax_id') | undefined, recipientIdNumber?: string | undefined, recipientUpiId?: string | undefined, recipientName?: string | undefined}; export type PaymentMethodTransaction = {transactionInvoice?: string | undefined, transactionNarration?: string | undefined}; export type Amount = number | string; export type CreateApiApiV1PaymentMethodArgs = {body: {channel: 'BANK_ACCOUNT' | 'MOBILE_MONEY' | 'VIRTUAL_BANK_ACCOUNT' | 'ACH_BANK_ACCOUNT' | 'INTERAC' | 'UPI' | 'SWIFT' | 'WE_CHAT' | 'ALIPAY', customerId?: CustomerId | undefined, type?: ('DEPOSIT' | 'WITHDRAW') | undefined, accountName?: string | undefined, accountNumber?: string | undefined, countryCode: string, institution?: PaymentMethodInstitution | undefined, recipient?: PaymentMethodRecipient | undefined, sender?: PaymentMethodRecipient | undefined, transaction?: PaymentMethodTransaction | undefined, meta?: {[key: string]: unknown} | undefined, walletTag?: string | undefined, amount?: Amount | undefined, wechatId?: string | undefined, district?: string | undefined}}; export type CreateApiApiV1PaymentMethodOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customerId = z.string(); export const paymentMethodInstitution = z.object({institutionId: z.string().optional(), institutionName: z.string().optional(), institutionCode: z.string().optional(), institutionAddress: z.string().optional(), institutionAddressLine1: z.string().optional(), institutionAddressCity: z.string().optional(), institutionAddressState: z.string().optional(), institutionAddressZip: z.string().optional(), institutionAddressCountry: z.string().optional(), institutionBranch: z.string().optional(), correspondentBankName: z.string().optional(), correspondentBankAccountNumber: z.string().optional()}); export const paymentMethodTransaction = z.object({transactionInvoice: z.string().optional(), transactionNarration: z.string().optional()}); export const paymentMethodRecipient = z.object({recipientEmail: z.string().optional(), recipientPhone: z.string().optional(), recipientAddress: z.string().optional(), recipientAddressLine1: z.string().optional(), recipientAddressCity: z.string().optional(), recipientAddressState: z.string().optional(), recipientAddressZip: z.string().optional(), recipientAddressCountry: z.string().optional(), recipientIdType: z.enum(["national_id", "passport", "drivers_license", "voters_card", "residence_permit", "bvn", "tax_id"]).optional(), recipientIdNumber: z.string().optional(), recipientUpiId: z.string().optional(), recipientName: z.string().optional()}); export const paymentMethodBankAddress = z.object({line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), country: z.string().optional()}); export const paymentMethod = z.object({paymentMethodId: z.string().optional(), customerId: customerId.optional(), reference: z.string().optional(), institution: paymentMethodInstitution.optional(), transaction: paymentMethodTransaction.optional(), recipient: paymentMethodRecipient.optional(), channel: z.enum(["BANK_ACCOUNT", "MOBILE_MONEY", "SWIFT", "INTERAC", "UPI", "WE_CHAT", "ALIPAY", "CARD", "CRYPTO", "VIRTUAL_BANK_ACCOUNT", "POOL_ACCOUNT", "ACH_BANK_ACCOUNT", "PAYBILL_TILL", "RFP", "VIRTUAL_CARD"]).optional(), countryCode: z.string().optional(), currency: z.string().optional(), capabilities: z.array(z.string()).optional(), accountName: z.string().optional(), accountNumber: z.string().optional(), routingNumber: z.string().optional(), bankAddress: paymentMethodBankAddress.optional(), status: z.enum(["active", "pending", "deleted", "expired", "blocked"]).optional(), last4: z.string().optional(), brand: z.enum(["Visa", "MasterCard", "Discover", "American Express", "JCB", "Diners Club", "Eftpos Australia", "UnionPay", "Unknown"]).optional(), expiration: z.object({month: z.number().int().optional(), year: z.number().int().optional()}).optional(), cardName: z.string().optional(), expiresInMinutes: z.number().int().optional(), amount: z.union([z.number(), z.string()]).optional(), extra: z.record(z.string(), z.unknown()).optional(), meta: z.object({}).optional()}); export const createApiApiV1PaymentMethodResponse = z.object({data: paymentMethod.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 }; /** * Create a payment method * * POST /api/v1/payment-method */ export const createApiApiV1PaymentMethod = async (args: CreateApiApiV1PaymentMethodArgs, options?: CreateApiApiV1PaymentMethodOptions) => { const {baseUrl = "https://sandbox.api.afriex.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-method${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(createApiApiV1PaymentMethodResponse, await res.json()) };