import {z} from 'zod' export type Card = {name: string, number: string, exp_month: number, exp_year: number, cvv?: string | undefined, merchant?: ({vgs_merchant_id?: string | undefined, sub_merchant_name?: string | undefined, sub_merchant_mid?: string | undefined}) | undefined}; export type CreateApiCardCheckArgs = {body: Card}; export type CreateApiCardCheckOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const checkedCard = z.object({name: z.string().max(255), number: z.string().min(13).max(19), exp_month: z.number().int().gte(1).lte(12), exp_year: z.number().int().gte(0).lte(99), cvv: z.string().optional(), merchant: z.object({vgs_merchant_id: z.string().max(255).optional(), sub_merchant_name: z.string().max(255).optional(), sub_merchant_mid: z.string().max(255).optional()}).optional(), failure_code: z.enum(["account-updater-not-supported", "merchant-not-found", "invalid-exp-date", "invalid-payload", "invalid-param", "internal-server-error"]).optional(), event: z.enum(["au_card.valid", "au_card.updated", "au_card.expired", "au_card.closed", "au_card.non_participating", "au_card.contact_cardholder_advice", "au_card.unknown", "au_card.enrolled", "au_card.opt_out"]).optional()}); export const checkCardData = z.object({data: checkedCard}); 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 }; /** * Check a card for updates in VGS Account Updater * * POST /card-check */ export const createApiCardCheck = async (args: CreateApiCardCheckArgs, options?: CreateApiCardCheckOptions) => { const {baseUrl = "https://sandbox.vgsapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/card-check${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(checkCardData, await res.json()) };