import {z} from 'zod' export type CreateSettlementFundingTransferAttempt = {amount?: number | undefined, instrument_id?: string | undefined}; export type CreateApiSettlementsSettlementIdFundingTransferAttemptsArgs = {'Finix-Version'?: string | undefined, 'Content-Type'?: string | undefined, body: CreateSettlementFundingTransferAttempt}; export type CreateApiSettlementsSettlementIdFundingTransferAttemptsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const amount = z.number().int(); export const applicationId = z.string().min(1); export const currency = z.enum(["CAD", "USD"]); export const tags = z.record(z.string(), z.unknown()).nullable(); export const settlementFundingTransferAttempt = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), amount: amount.optional(), application: applicationId.optional(), currency: currency.optional(), funding_transfer_id: z.string().optional(), instrument_id: z.string().nullable().optional(), merchant_id: z.string().optional(), failure_code: z.enum(["BALANCE_ACCOUNT_NOT_FOUND", "BALANCE_VALIDATION_FAILED", "INSUFFICIENT_BALANCE", "INVALID_BALANCE_ACCOUNT", "VELOCITY_LIMIT_EXCEEDED", "VELOCITY_RULE_NOT_FOUND", "VELOCITY_VALIDATION_FAILED"]).nullable().optional(), failure_message: z.string().nullable().optional(), state: z.enum(["PENDING", "FAILED", "SUCCEEDED"]).optional(), tags: tags.optional(), settlement_id: z.unknown(), _links: z.object({self: z.object({href: z.string().optional()}).optional()}).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 Settlement Funding Transfer Attempt * * POST /settlements/{settlement_id}/funding_transfer_attempts */ export const createApiSettlementsSettlementIdFundingTransferAttempts = async (args: CreateApiSettlementsSettlementIdFundingTransferAttemptsArgs, options?: CreateApiSettlementsSettlementIdFundingTransferAttemptsOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/settlements/${encodeURIComponent(String(args.settlement_id))}/funding_transfer_attempts${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(settlementFundingTransferAttempt, await res.json()) };