import {z} from 'zod' export type VirtualAccountCurrency = 'NGN' | 'GHS' | 'KES' | 'TZS' | 'RWF' | 'UGX' | 'XAF' | 'USD' | 'EUR' | 'GBP' | 'CAD'; export type VirtualAccountType = 'individual' | 'corporate'; export type VirtualAccountAddress = {countryOfResidence: string, number?: string | undefined, street: string, street2?: string | undefined, city: string, state: string, zip: string}; export type VirtualAccountIdentityDocument = {type: 'passport' | 'driverLicense' | 'votersCard' | 'nationalId' | 'idCard' | 'other', number: string, issuedCountryCode: string, issuedBy: string, issuedDate?: string | undefined, expirationDate?: string | undefined}; export type VirtualAccountIncomeBand = {lower: string, upper: string}; export type IndividualVirtualAccountKyc = {firstName: string, lastName: string, otherName?: string | undefined, email?: string | undefined, phone?: string | undefined, bvn?: string | undefined, address?: VirtualAccountAddress | undefined, document?: VirtualAccountIdentityDocument | undefined, birthDate?: string | undefined, nationality?: string | undefined, occupation?: string | undefined, taxCountry?: string | undefined, taxNumber?: string | undefined, monthlyTransactionVolume?: string | undefined, monthlyTransactionCount?: string | undefined, sourceOfIncome?: ('salary' | 'business_income' | 'investment' | 'gift' | 'inheritance' | 'real_estate' | 'loan' | 'pension' | 'grant' | 'trust' | 'crypto' | 'other') | undefined, accountDesignation?: string | undefined, employmentStatus?: ('employed' | 'self_employed' | 'unemployed' | 'student' | 'retired' | 'homemaker' | 'freelancer' | 'other') | undefined, incomeBand?: VirtualAccountIncomeBand | undefined, annualIncome?: string | undefined} | {[key: string]: unknown}; export type CorporateVirtualAccountKyc = {businessName?: string | undefined, email?: string | undefined, phone?: string | undefined, bvn?: string | undefined, bvnName?: string | undefined, rcNumber?: string | undefined, businessRegistrationName?: string | undefined, businessRegistrationNumber?: string | undefined, businessRegistrationCountry?: string | undefined, incorporationCountryCode?: string | undefined, incorporationDate?: string | undefined, taxIdentificationNumber?: string | undefined, taxCountry?: string | undefined, website?: string | undefined, address?: VirtualAccountAddress | undefined, tradingAddress?: VirtualAccountAddress | undefined, primarySourceOfFunds?: string | undefined, companyType?: string | undefined, businessCategory?: string | undefined, businessCategoryDescription?: string | undefined, primaryBusiness?: string | undefined, primaryBusinessDescription?: string | undefined, tradingName?: string | undefined, directorsName?: string | undefined, estimatedMonthlyTurnover?: string | undefined, projectedMonthlyInflow?: string | undefined, projectedMonthlyOutflow?: string | undefined, projectedTransactionFrequency?: string | undefined, projectedMonthlyTransactionCount?: string | undefined, purpose?: string | undefined, ultimateBeneficialOwners?: Array<{[key: string]: unknown}> | undefined, businessPartners?: Array<{[key: string]: unknown}> | undefined} | {[key: string]: unknown}; export type PermanentVirtualAccountRequest = {currency: VirtualAccountCurrency, accountType: VirtualAccountType, KYCInformation: IndividualVirtualAccountKyc | CorporateVirtualAccountKyc, merchantReference?: string | undefined, phoneNumber?: string | undefined, reason?: string | undefined, purpose?: ('third_party' | 'self') | undefined, entityName?: string | undefined, paymentFlowDescription?: string | undefined, meansOfId?: (string | Array) | undefined, utilityBill?: (string | Array) | undefined, bankStatement?: (string | Array) | undefined, leaseAgreement?: (string | Array) | undefined, sourceOfFunds?: (string | Array) | undefined, beneficialOwnershipCertificate?: (string | Array) | undefined, articleOfIncorporation?: (string | Array) | undefined, incorporationDocuments?: (string | Array) | undefined, accountAgreement?: (string | Array) | undefined, regulatoryEvidence?: (string | Array) | undefined, requestedCurrencies?: (Array<'USD' | 'EUR' | 'GBP' | 'CAD'>) | undefined, metadata?: {[key: string]: unknown} | undefined, accountOpeningDate?: string | undefined, isTermsAccepted?: boolean | undefined, termsAndConditionAcceptance?: {date: string, ipAddress: string} | undefined}; export type CreateApiProfileVirtualAccountsRequestsArgs = {body: PermanentVirtualAccountRequest}; export type CreateApiProfileVirtualAccountsRequestsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const virtualAccountCurrency = z.enum(["NGN", "GHS", "KES", "TZS", "RWF", "UGX", "XAF", "USD", "EUR", "GBP", "CAD"]); export const virtualAccountType = z.enum(["individual", "corporate"]); export const virtualAccountStatus = z.enum(["pending", "approved", "declined", "expired", "closed", "reviewed"]); export const virtualAccountBankDetails = z.object({accountNumber: z.string().optional(), accountName: z.string().optional(), bankName: z.string().optional(), bankCode: z.string().optional(), iban: z.string().optional(), swiftCode: z.string().optional(), routingNumber: z.string().optional(), paymentScheme: z.string().optional(), alternateAccountDetails: z.array(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())); export const virtualAccount = z.object({_id: z.string().optional(), business: z.union([z.string(), z.object({})]).optional(), parentBusiness: z.union([z.string(), z.object({})]).nullable().optional(), currency: virtualAccountCurrency.optional(), accountType: virtualAccountType.optional(), status: virtualAccountStatus.optional(), isActive: z.boolean().optional(), isPermanent: z.literal(true).optional(), accountNumber: z.string().nullable().optional(), accountInformation: virtualAccountBankDetails.nullable().optional(), merchantReference: z.string().nullable().optional(), KYCInformation: z.record(z.string(), z.unknown()).optional(), entityType: z.enum(["main_account", "sub_account"]).optional(), virtualAccountType: z.string().optional(), accountOpeningFee: z.number().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const virtualAccountResponse = z.object({success: z.literal(true), message: z.string(), data: virtualAccount}); 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 }; /** * Request a permanent virtual account * * POST /profile/virtual-accounts/requests */ export const createApiProfileVirtualAccountsRequests = async (args: CreateApiProfileVirtualAccountsRequestsArgs, options?: CreateApiProfileVirtualAccountsRequestsOptions) => { 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(/\/+$/, '')}/profile/virtual-accounts/requests${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(virtualAccountResponse, await res.json()) };