import {z} from 'zod' export type CreateApiApiV21CardsLookupArgs = {'Content-Type': string, Authorization: string, body: {token: string} | {card: {number: string, expirationMonth: string, expirationYear: string}}}; export type CreateApiApiV21CardsLookupOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV21CardsLookupResponse = z.object({maskedNumber: z.string().optional(), expirationMonth: z.number().int().optional(), expirationYear: z.number().int().optional(), scheme: z.enum(["MC", "VS"]).optional(), description: z.string().optional(), type: z.string().optional(), countryCode: z.string().optional(), classification: z.enum(["DEBIT", "PREPAID", "CREDIT", "CHARGE"]).optional(), profile: z.enum(["BUSINESS", "CONSUMER", "BOTH"]).optional(), uniqueReference: z.string().max(256).optional(), issuer: z.object({name: z.string().optional(), region: z.enum(["UNITED_STATES", "CANADA", "ASIA_PACIFIC", "LATING_AMERICA_AND_CARIBBEAN", "EUROPE", "V6_CE_EUROPE_MID_EAST_AND_AFRICA", "ME_SOUTH_ASIA_MID_EAST_AFRICA"]).optional(), countryCode: z.string().optional()}).optional(), installmentOptions: z.object({numbers: z.array(z.number().int()).optional(), provider: z.string().optional(), settings: z.object({minAmount: z.number().optional()}).optional()}).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 }; /** * Retrieve Card Data * * POST /api/v2_1/cards/lookup */ export const createApiApiV21CardsLookup = async (args: CreateApiApiV21CardsLookupArgs, options?: CreateApiApiV21CardsLookupOptions) => { const {baseUrl = "https://secure.payu.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2_1/cards/lookup${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(createApiApiV21CardsLookupResponse, await res.json()) };