import {z} from 'zod' export type GetApiPaymentsMerchantsArgs = {page?: number | undefined, pageSize?: number | undefined, q?: string | undefined}; export type GetApiPaymentsMerchantsOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({line1: z.string().max(60), line2: z.string().max(60).optional(), city: z.string().max(50), state: z.string().optional(), postalCode: z.string().max(20), country: z.string()}); export const cardBrand = z.enum(["visa", "mastercard", "american-express", "discover", "diners-club", "jcb", "unionpay"]); export const merchantNetworkTokenStatus = z.object({enrolment: z.array(z.object({cardBrand: cardBrand, tokenRequestorIdentifier: z.string().nullable().optional(), status: z.enum(["active", "inactive"])}))}); export const merchantApplePay = z.object({domains: z.array(z.object({merchantUuid: z.string(), domain: z.string(), status: z.enum(["active", "pending", "failed"])}))}); export const merchantPreChargebackAlerts = z.object({enrolments: z.array(z.object({type: z.enum(["ETHOCA_ALERT", "VERIFI_RDR"]), status: z.enum(["PENDING", "ACTIVE", "ACTION_REQUIRED", "CANCELLED"]), ethocaAlert: z.object({descriptors: z.array(z.object({descriptor: z.string(), matchType: z.enum(["STARTS_WITH", "EXACT_MATCH"]), status: z.string().optional()})).optional()}).optional(), verifiRdr: z.object({bin: z.string().optional(), caid: z.string().optional(), arns: z.array(z.string()).optional()}).optional()}))}); export const merchantContact = z.object({phoneNumber: z.string().max(20).optional(), email: z.string().optional(), name: z.object({firstName: z.string().min(1).max(60), lastName: z.string().min(1).max(60)}).optional()}); export const merchant = z.object({id: z.string(), name: z.string(), shortName: z.string().optional(), website: z.string(), business: z.object({legalName: z.string(), address: address}), categoryCode: z.string(), networkTokens: merchantNetworkTokenStatus, applePay: merchantApplePay, preChargebackAlerts: merchantPreChargebackAlerts.optional(), contact: merchantContact.optional(), createdAt: z.number().int(), updatedAt: z.number().int().optional()}); export const getApiPaymentsMerchantsResponse = z.object({data: z.array(merchant), pageSize: z.number().int(), nextPage: z.number().int().nullable(), total: z.number().int()}); 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 }; /** * List Merchants * * GET /payments/merchants */ export const getApiPaymentsMerchants = async (args: GetApiPaymentsMerchantsArgs, options?: GetApiPaymentsMerchantsOptions) => { const {baseUrl = "https://api.evervault.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.q != null) query.append("q", String(args.q)) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/merchants${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(getApiPaymentsMerchantsResponse, await res.json()) };