import {z} from 'zod' export type GetApiLedgerCashDepositsReferenceArgs = {reference: string}; export type GetApiLedgerCashDepositsReferenceOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["PENDING", "IN_PROGRESS", "FAILED", "SUCCESS"]); export const cashAccountDetails = z.object({accountInfoType: z.string(), accountName: z.string(), accountNumber: z.string(), reference: z.string().optional()}); export const paymentRail = z.enum(["ACH", "FEDWIRE", "SWIFT", "SEPA", "FPS", "FPS_CROSS_SCHEME", "NIP"]); export const paymentMethodFee = z.object({percentage: z.number().gte(0).lte(1), fixedAmount: z.number().gte(0), currency: z.string()}); export const partyDepositLimits = z.object({maxPerTransaction: z.number().gte(0).optional(), maxPerDay: z.number().gte(0).optional()}); export const paymentMethodLimits = z.object({minAmount: z.number().gte(0), firstParty: partyDepositLimits.optional(), thirdParty: partyDepositLimits.optional(), currency: z.string()}); export const paymentMethodTimeline = z.object({minValue: z.number().int().gte(0), maxValue: z.number().int().gte(0), unit: z.enum(["MINUTES", "HOURS", "BUSINESS_DAYS"])}); export const paymentMethodCapability = z.object({rail: paymentRail, fee: paymentMethodFee, limits: paymentMethodLimits.optional(), timeline: paymentMethodTimeline.optional()}); export const cashDepositPaymentDetails = z.object({details: cashAccountDetails.optional(), payinCurrency: z.string(), paymentMethods: z.array(paymentMethodCapability).optional()}); export const cashDepositTransaction = z.object({id: z.string(), reference: z.string(), accountTransactionId: z.string().optional(), currency: z.string(), status: transactionStatus, payinCurrency: z.string(), amount: z.number(), rate: z.number(), payinAmount: z.number(), feeAmount: z.number().optional(), feeInclusive: z.boolean().optional(), providerReference: z.string().optional(), paymentDetails: cashDepositPaymentDetails.optional(), createdDate: 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 }; /** * Get specific cash deposit * * GET /ledger/cash-deposits/{reference} */ export const getApiLedgerCashDepositsReference = async (args: GetApiLedgerCashDepositsReferenceArgs, options?: GetApiLedgerCashDepositsReferenceOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/cash-deposits/${encodeURIComponent(String(args.reference))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cashDepositTransaction, await res.json()) };