import {z} from 'zod' export type CardExpiry = {month: string, year: string}; export type ExternalCustomerReference = string; export type CreateApiPaymentsCardsCardIdSimulateArgs = {card_id: string, body: {updateType: 'new-account-number', expiry?: CardExpiry | undefined, number?: string | undefined, externalCustomerReference?: ExternalCustomerReference | undefined} | {updateType: 'new-expiry-date', expiry?: CardExpiry | undefined, externalCustomerReference?: ExternalCustomerReference | undefined} | {updateType: 'account-closure', externalCustomerReference?: ExternalCustomerReference | undefined}}; export type CreateApiPaymentsCardsCardIdSimulateOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const cardExpiry = z.object({month: z.string(), year: z.string()}); export const cardBrand = z.enum(["visa", "mastercard", "american-express", "discover", "diners-club", "jcb", "unionpay"]); export const cardFunding = z.enum(["debit", "credit", "prepaid", "deferred-debit", "charge"]); export const cardSegment = z.enum(["consumer", "commercial", "business", "government", "payouts", "all"]); export const card = z.object({id: z.string().optional(), number: z.string(), expiry: cardExpiry.optional(), bin: z.string(), lastFour: z.string(), brand: cardBrand.optional(), funding: cardFunding.optional(), segment: cardSegment.optional(), country: z.string().optional(), currency: z.string().optional(), issuer: z.string().optional(), status: z.enum(["active", "replaced", "closed", "invalid"]).optional(), replacement: z.string().nullable().optional(), automaticUpdates: z.enum(["enabled", "issuer-not-enrolled", "disabled"]).optional(), createdAt: z.number().int(), updatedAt: z.number().int().nullable().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 }; /** * Simulate a Card Update * * POST /payments/cards/{card_id}/simulate */ export const createApiPaymentsCardsCardIdSimulate = async (args: CreateApiPaymentsCardsCardIdSimulateArgs, options?: CreateApiPaymentsCardsCardIdSimulateOptions) => { const {baseUrl = "https://api.evervault.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/cards/${encodeURIComponent(String(args.card_id))}/simulate${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(card, await res.json()) };