import {z} from 'zod' export type CreateApiDisbursementsPayoutsCadArgs = {body: {business: string, sourceCurrency: 'CAD', destinationCurrency: 'CAD', amount: number, description: string, customerReference: string, paymentDestination: 'bank_account', paymentScheme: 'interac', beneficiary: {type: 'individual', firstName: string, lastName?: string | undefined, accountHolderName: string, interacEmail: string, country: 'CA', securityQuestion?: string | undefined, securityAnswer?: string | undefined} | {type: 'corporate', accountHolderName: string, interacEmail: string, country: 'CA', securityQuestion?: string | undefined, securityAnswer?: string | undefined}} | {business: string, sourceCurrency: string, destinationCurrency: 'CAD', amount: number, quoteReference: string, description: string, customerReference: string, paymentDestination: 'bank_account', paymentScheme: 'interac', beneficiary: {type: 'individual', firstName: string, lastName?: string | undefined, accountHolderName: string, interacEmail: string, country: 'CA', securityQuestion?: string | undefined, securityAnswer?: string | undefined} | {type: 'corporate', accountHolderName: string, interacEmail: string, country: 'CA', securityQuestion?: string | undefined, securityAnswer?: string | undefined}}}; export type CreateApiDisbursementsPayoutsCadOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDisbursementsPayoutsCadResponse = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.object({id: z.number().int().optional(), reference: z.string().optional(), customerReference: z.string().optional(), status: z.string().optional(), message: z.string().nullable().optional(), isDocumentRequired: z.boolean().optional(), documentsRequired: z.array(z.string()).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 CAD Payout * * POST /disbursements/payouts#cad */ export const createApiDisbursementsPayoutsCad = async (args: CreateApiDisbursementsPayoutsCadArgs, options?: CreateApiDisbursementsPayoutsCadOptions) => { 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(/\/+$/, '')}/disbursements/payouts#cad${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(createApiDisbursementsPayoutsCadResponse, await res.json()) };