import {z} from 'zod' export type CreateApiV3CustomercardaccountArgs = {id: number, echo?: boolean | undefined, body: {code?: string | undefined, number?: string | undefined, magstripe?: string | undefined, expiryMonth?: string | undefined, expiryYear?: string | undefined, avsZip?: string | undefined, avsStreet?: string | undefined, cvv?: string | undefined, name?: string | undefined, isDefault?: boolean | undefined, alias?: string | undefined}}; export type CreateApiV3CustomercardaccountOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV3CustomercardaccountResponse = z.object({isDefault: z.boolean().optional(), id: z.number().int().optional(), created: z.string().optional(), alias: z.string().optional(), cardType: z.string().optional(), last4: z.string().optional(), cardId: z.string().optional(), token: z.string().optional(), expiryMonth: z.string().optional(), expiryYear: z.string().optional(), name: z.string().optional(), avsStreet: z.string().optional(), avsZip: z.string().optional(), hasContract: z.boolean().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 card account * * POST /v3/customercardaccount */ export const createApiV3Customercardaccount = async (args: CreateApiV3CustomercardaccountArgs, options?: CreateApiV3CustomercardaccountOptions) => { 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/customercardaccount${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(createApiV3CustomercardaccountResponse, await res.json()) };