import {z} from 'zod' export type AccountValidationRequest = {data: {type?: 'account-validations' | undefined, attributes: {cvc?: string | undefined, cardholder_address?: ({street?: string | undefined, postal_code: string}) | undefined, cardholder_name?: ({first_name?: string | undefined, middle_name?: string | undefined, last_name?: string | undefined}) | undefined, merchant: {merchant_name: string, address: {city: string, state: string, country: string, postal_code: string}}}}}; export type CreateApiCardsCardIdValidationsArgs = {card_id: string, Authorization: string, 'Content-Type': 'application/vnd.api+json', body: AccountValidationRequest}; export type CreateApiCardsCardIdValidationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const validationResult = z.object({type: z.enum(["cvc", "address", "name"]), status: z.enum(["match", "no_match", "partial_match", "not_supported", "system_unavailable", "invalid_data", "non_participating"]), detail: z.object({street_address: z.enum(["match", "no_match"]).optional(), postal_code: z.enum(["match", "no_match"]).optional()}).optional()}); export const observability = z.object({trace_id: z.string(), client_id: z.string(), vault_id: z.string(), account_id: z.string(), fingerprint: z.string()}); export const metadataResponse = z.object({observability: observability}); export const accountValidationResponse = z.object({data: z.object({id: z.string(), type: z.literal("account-validation-results"), attributes: z.object({transaction_identifier: z.string().optional(), attempted_at: z.string(), card_verification_status: z.enum(["verified", "not_verified"]), validation_results: z.array(validationResult).optional()}), relationships: z.object({card: z.object({data: z.object({type: z.literal("cards").optional(), id: z.string().optional()}).optional()}).optional()}).optional(), links: z.object({self: z.string().optional()}).optional()}), meta: metadataResponse.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 }; /** * Perform account validation checks * * POST /cards/{card_id}/validations */ export const createApiCardsCardIdValidations = async (args: CreateApiCardsCardIdValidationsArgs, options?: CreateApiCardsCardIdValidationsOptions) => { const {baseUrl = "https://sandbox.vgsapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.card_id))}/validations${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(accountValidationResponse, await res.json()) };