import {z} from 'zod' export type CreateApiV3CustomerbankaccountArgs = {id: number, echo?: boolean | undefined, body: {accountNumber?: string | undefined, routingNumber?: string | undefined, name?: string | undefined, type?: string | undefined, code?: string | undefined, isDefault?: boolean | undefined, addressId?: number | undefined}}; export type CreateApiV3CustomerbankaccountOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV3CustomerbankaccountResponse = z.object({id: z.number().int().optional(), addressid: z.number().int().optional(), hash: z.string().optional(), last4: z.string().optional(), cardId: z.string().optional(), token: z.string().optional(), routingNumber: z.string().optional(), name: z.string().optional(), isDefault: z.boolean().optional(), address: z.object({id: z.number().int().optional(), name: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), country: z.string().optional(), created: z.string().optional(), isDefault: z.boolean().optional()}).optional(), type: z.string().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 vaulted bank account * * POST /v3/customerbankaccount */ export const createApiV3Customerbankaccount = async (args: CreateApiV3CustomerbankaccountArgs, options?: CreateApiV3CustomerbankaccountOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", 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 (args.id != null) query.append("id", String(args.id)) if (args.echo != null) query.append("echo", String(args.echo)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/customerbankaccount${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(createApiV3CustomerbankaccountResponse, await res.json()) };