import {z} from 'zod' export type Pan = string; export type Network = 'american-express' | 'bancontact' | 'diners' | 'mastercard' | 'visa'; export type ThreeDsCardRangesCardNumberRequest = {cardNumber: Pan, infoSource?: Network | undefined}; export type ProcessorTokenKey = string; export type ThreeDsCardRangesProcessorTokenRequest = {processorTokenKey: ProcessorTokenKey}; export type ThreeDsCardRangesRequest = ThreeDsCardRangesCardNumberRequest | ThreeDsCardRangesProcessorTokenRequest; export type CreateApi3dsCardRangesArgs = {body: ThreeDsCardRangesRequest}; export type CreateApi3dsCardRangesOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const network = z.enum(["american-express", "bancontact", "diners", "mastercard", "visa"]); export const threeDsCardRangesResponse = z.object({network: network, startRange: z.string(), endRange: z.string(), acsStartProtocolVersion: z.string(), acsEndProtocolVersion: z.string(), dsStartProtocolVersion: z.string(), dsEndProtocolVersion: z.string(), threeDSMethodURL: z.string().optional(), threeDSServerTransID: z.string().optional(), acsInfoIndicator: z.array(z.enum(["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"])).optional()}); export const createApi3dsCardRangesResponse = z.array(threeDsCardRangesResponse); 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 Ranges * * POST /3ds/cardRanges * * Also accepts auth: BearerToken */ export const createApi3dsCardRanges = async (args: CreateApi3dsCardRangesArgs, options?: CreateApi3dsCardRangesOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", 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(/\/+$/, '')}/3ds/cardRanges${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(createApi3dsCardRangesResponse, await res.json()) };