import {z} from 'zod' export type BinKey = string; export type GetApiBinsBinKeyArgs = {binKey: BinKey}; export type GetApiBinsBinKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageSystem = z.enum(["DMS", "SMS"]); export const binKey = z.string().min(5).max(120); export const status = z.enum(["active", "archived"]); export const extendedFreeTextField = z.string().min(1).max(160); export const acquirerAgentKey = z.string(); export const countryCodeAlpha3 = z.string().min(3).max(3); export const countryCodeAlpha2 = z.string().min(2).max(2); export const countryCodeNumeric = z.string().min(3).max(3); export const countryCode = z.object({alpha3: countryCodeAlpha3, alpha2: countryCodeAlpha2, numeric: countryCodeNumeric}); export const created = z.string(); export const lastModified = z.string(); export const version = z.number().int(); export const visaBin = z.object({network: z.literal("visa"), messageSystem: messageSystem, acquirerIdCode: z.string().min(6).max(6), acquirerBin: z.string().min(6).max(6), bid: z.string().min(8).max(8), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const mastercardBin = z.object({network: z.literal("mastercard"), messageSystem: messageSystem, acquirerIdCode: z.string().min(6).max(6), acquirerBin: z.string().min(6).max(6), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const bancontactBin = z.object({network: z.literal("bancontact"), messageSystem: messageSystem, acquirerIdCode: z.string().min(6).max(6), acquirerBin: z.string().min(6).max(6), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const authorizationCurrency = z.enum(["EUR", "USD", "AFN", "ALL", "DZD", "AOA", "XCD", "ARS", "AMD", "AWG", "AUD", "AZN", "BSD", "BHD", "BDT", "BBD", "BYN", "BZD", "XOF", "BMD", "INR", "BTN", "BOB", "BAM", "BWP", "NOK", "BRL", "BND", "BGN", "BIF", "CVE", "KHR", "XAF", "CAD", "KYD", "CLP", "CNY", "CNH", "COP", "KMF", "CDF", "NZD", "CRC", "HRK", "CUP", "CUC", "ANG", "CZK", "DKK", "DJF", "DOP", "EGP", "SVC", "ERN", "ETB", "FKP", "FJD", "XPF", "GMD", "GEL", "GHS", "GIP", "GTQ", "GBP", "GNF", "GYD", "HTG", "HNL", "HKD", "HUF", "ISK", "IDR", "IRR", "IQD", "ILS", "JMD", "JPY", "JOD", "KZT", "KES", "KPW", "KRW", "KWD", "KGS", "LAK", "LBP", "LSL", "ZAR", "LRD", "LYD", "CHF", "MOP", "MKD", "MGA", "MWK", "MYR", "MVR", "MRU", "MUR", "MXN", "MDL", "MNT", "MAD", "MZN", "MMK", "NAD", "NPR", "NIO", "NGN", "OMR", "PKR", "PAB", "PGK", "PYG", "PEN", "PHP", "PLN", "QAR", "RON", "RUB", "RWF", "SHP", "WST", "STN", "SAR", "RSD", "SCR", "SLL", "SLE", "SGD", "SBD", "SOS", "SSP", "LKR", "SDG", "SRD", "SZL", "SEK", "SYP", "TWD", "TJS", "TZS", "THB", "TOP", "TTD", "TND", "TRY", "TMT", "UGX", "UAH", "AED", "UYU", "UYW", "UZS", "VUV", "VES", "VND", "YER", "ZMW", "ZWL", "ZWG"]); export const amexModel = z.enum(["payfac", "direct_merchant", "payment_designation"]); export const amexBin = z.object({network: z.literal("american-express"), messageSystem: z.literal("DMS"), acquirerIdCode: z.string().min(6).max(11), acquirerBin: z.union([z.string(), z.string().min(6).max(11), z.string().min(6).max(11)]), authorizationCurrency: authorizationCurrency.optional(), amexModel: amexModel.optional(), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const discoverAcquirerBin = z.string().min(1).max(11); export const discoverIsoId = z.string().min(1).max(11); export const discoverBin = z.object({network: z.literal("discover"), messageSystem: z.literal("DMS"), acquirerIdCode: z.string().min(1).max(11), acquirerBin: discoverAcquirerBin, isoId: discoverIsoId.optional(), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const dxsCode = z.string().min(3).max(3); export const dinersBin = z.object({network: z.literal("diners"), messageSystem: z.literal("DMS"), acquirerBin: z.string().min(6).max(11), acquirerIdCode: z.string().min(6).max(11), dxsCode: dxsCode, key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const unionpayBin = z.object({network: z.literal("unionpay"), messageSystem: z.literal("DMS"), acquirerIdCode: z.string().min(8).max(11), key: binKey, status: status, description: extendedFreeTextField, acquirerAgentKey: acquirerAgentKey.optional(), acquirerCountryCode: countryCode, shared: z.boolean(), created: created, lastModified: lastModified.optional(), version: version}); export const bin = z.discriminatedUnion("network", [visaBin, mastercardBin, bancontactBin, amexBin, discoverBin, dinersBin, unionpayBin]); 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 }; /** * Get Bin * * GET /bins/{binKey} * * Also accepts auth: BearerToken */ export const getApiBinsBinKey = async (args: GetApiBinsBinKeyArgs, options?: GetApiBinsBinKeyOptions) => { 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('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bins/${encodeURIComponent(String(args.binKey))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bin, await res.json()) };