import {z} from 'zod' export type ExternalCustomerReference = string; export type CreateApiPaymentsAcquirersArgs = {body: {name: string, description?: string | undefined, default?: boolean | undefined, configurations: Array<{network: 'visa' | 'mastercard' | 'american-express' | 'discover' | 'jcb' | 'unionpay', bin: string, acquirerMerchantIdentifier: string, country: unknown}>, externalCustomerReference?: ExternalCustomerReference | undefined}}; export type CreateApiPaymentsAcquirersOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const acquirerConfiguration = z.object({id: z.string(), name: z.string(), description: z.string().optional(), default: z.boolean(), configurations: z.array(z.object({network: z.enum(["visa", "mastercard", "american-express", "discover", "jcb", "unionpay"]), bin: z.string().min(6).max(11), acquirerMerchantIdentifier: z.string(), state: z.string(), country: z.string()}))}); 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 an Acquirer * * POST /payments/acquirers */ export const createApiPaymentsAcquirers = async (args: CreateApiPaymentsAcquirersArgs, options?: CreateApiPaymentsAcquirersOptions) => { 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/acquirers${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(acquirerConfiguration, await res.json()) };