import {z} from 'zod' export type OpenapiComponentsSchemasPan2 = string; export type SchemasAmountValue = number; export type CurrencyCodeEnum = 'AFN' | 'EUR' | 'ALL' | 'DZD' | 'USD' | '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' | 'CNH' | 'CNY' | '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'; export type IntegerAmount = {value: SchemasAmountValue, currency: CurrencyCodeEnum}; export type OpenapiComponentsSchemasAcceptorKeyRef = string; export type Intent = 'purchase' | 'reservation' | 'account-status' | 'payout' | 'payout-status' | 'funding' | 'advice' | 'refund'; export type OpenapiComponentsSchemasCardEntry = 'e-commerce' | 'manual' | 'card-on-file' | 'token' | 'token-on-file' | 'terminal'; export type ComponentsSchemasOrder = 'checkout' | 'unspecified' | 'mail' | 'phone' | 'one-click' | 'recurring' | 'ucof' | 'installment' | 'counter'; export type ComponentsSchemasSequence = 'initial' | 'subsequent'; export type SchemasTerminalEntryMode = 'contactless' | 'manual-entry' | 'chip' | 'magstripe' | 'magstripe-fallback'; export type TerminalCapability = 'contactChip' | 'contactlessChip' | 'contactMagStripe' | 'contactlessMagStripe' | 'offlineEncipheredPin' | 'offlinePlaintextPin' | 'onlinePin' | 'signature' | 'keyEntry' | 'noCvm' | 'oneTap'; export type SoftPosTerminalCapability = 'contactlessChip' | 'onlinePin' | 'signature' | 'noCvm' | 'oneTap'; export type Terminal = {type: 'pos', attended: boolean, capabilities: Array} | {type: 'mpos', attended: true, capabilities: Array} | {type: 'softpos', attended: true, capabilities: Array}; export type ComponentsSchemasChargeType = {intent: Intent, cardEntry: OpenapiComponentsSchemasCardEntry, order: ComponentsSchemasOrder, sequence?: ComponentsSchemasSequence | undefined, terminalEntryMode?: SchemasTerminalEntryMode | undefined, terminal?: Terminal | undefined, pinUsed?: boolean | undefined}; export type InterchangeFeeEstimateRequest = {cardNumber: OpenapiComponentsSchemasPan2, amount: IntegerAmount, merchantAcceptorKey: OpenapiComponentsSchemasAcceptorKeyRef, chargeType: ComponentsSchemasChargeType, threeDsAuthentication: 'none' | 'attempt' | 'success', isRefund: boolean, additionalNetworkProperties?: ({transactionRoutingMeans?: ('qr' | 'url' | 'wip' | 'man') | undefined}) | undefined}; export type CreateApiFeesInterchangeEstimateArgs = {body: InterchangeFeeEstimateRequest}; export type CreateApiFeesInterchangeEstimateOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const openapiComponentsSchemasCurrencyCode4 = z.string(); export const feeSegments = z.object({currency: openapiComponentsSchemasCurrencyCode4.optional(), fixed: z.number().optional(), basisPoints: z.number().optional(), max: z.number().optional(), min: z.number().optional()}); export const floatAmountValue = z.number(); export const floatAmount = z.object({value: floatAmountValue, currency: openapiComponentsSchemasCurrencyCode4}); export const rate = z.object({date: z.string().optional(), source: openapiComponentsSchemasCurrencyCode4.optional(), target: openapiComponentsSchemasCurrencyCode4.optional(), rate: z.number().int().optional(), exponent: z.number().int().optional(), operation: z.enum(["multiply", "divide"]).optional(), type: z.enum(["buy", "mid", "sell"]).optional()}); export const visaFeeDesignator = z.object({feeDescriptor: z.string(), feeProgramIndicator: z.string().optional()}); export const businessServiceArrangement = z.object({cardProgramIdentifier: z.string(), type: z.string(), idCode: z.string(), priority: z.string().optional(), selectionEnforcementIndicator: z.string().optional()}); export const mastercardFeeDesignator = z.object({ird: z.string(), businessServiceArrangement: businessServiceArrangement}); export const bancontactFeeDesignator = z.object({description: z.string().optional(), feeCode: z.string().optional()}); export const interchangeFeeEstimateResponse = z.object({segments: feeSegments, feeDate: z.string(), amount: floatAmount, rates: z.array(rate), designator: z.union([visaFeeDesignator, mastercardFeeDesignator, bancontactFeeDesignator])}); 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 }; /** * Estimate interchange fee * * POST /fees/interchange/estimate * * Also accepts auth: BearerToken */ export const createApiFeesInterchangeEstimate = async (args: CreateApiFeesInterchangeEstimateArgs, options?: CreateApiFeesInterchangeEstimateOptions) => { 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(/\/+$/, '')}/fees/interchange/estimate${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(interchangeFeeEstimateResponse, await res.json()) };