import {z} from 'zod' export type Address = {line1: string, line2?: string | undefined, city: string, state?: unknown | undefined, postalCode: string, country: unknown}; export type MerchantPreChargebackAlertsConfig = {merchantType?: string | undefined, ethocaAlert?: ({descriptors: Array<{descriptor: string, matchType: 'STARTS_WITH' | 'EXACT_MATCH'}>}) | undefined, verifiRdr?: ({bin?: string | undefined, caid?: string | undefined, arns?: Array | undefined}) | undefined}; export type MerchantContact = {phoneNumber?: string | undefined, email?: string | undefined, name?: {firstName: string, lastName: string} | undefined}; export type ExternalCustomerReference = string; export type CreateApiPaymentsMerchantsArgs = {body: {name: string, shortName?: string | undefined, website: string, categoryCode: string, business: {legalName: string, address: Address}, applePay?: {domains: Array} | undefined, preChargebackAlerts?: MerchantPreChargebackAlertsConfig | undefined, contact?: MerchantContact | undefined, externalCustomerReference?: ExternalCustomerReference | undefined}}; export type CreateApiPaymentsMerchantsOptions = {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 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 }; /** * Create a Merchant * * POST /payments/merchants */ export const createApiPaymentsMerchants = async (args: CreateApiPaymentsMerchantsArgs, options?: CreateApiPaymentsMerchantsOptions) => { const {baseUrl = "https://api.evervault.com", 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(/\/+$/, '')}/payments/merchants${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(merchant, await res.json()) };