import {z} from 'zod' export type OpenapiComponentsSchemasPan2 = string; export type AmountValueInteger = number; export type OpenapiComponentsSchemasCurrencyCode5 = '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' | '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' | '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 AmountRequest = {value: AmountValueInteger, currency: OpenapiComponentsSchemasCurrencyCode5}; export type AcceptorKey = string; export type Intent = 'purchase' | 'reservation' | 'account-status' | 'payout' | 'payout-status' | 'funding' | 'advice' | 'refund'; export type OpenapiComponentsSchemasCardEntry2 = 'e-commerce' | 'manual' | 'card-on-file' | 'token' | 'token-on-file' | 'terminal'; export type PayoutType = 'general-funds-disbursement' | 'online-gambling'; export type FundingType = 'account-to-account' | 'wallet-transfer'; export type OpenapiComponentsSchemasChargeType = {intent: Intent, cardEntry: OpenapiComponentsSchemasCardEntry2, payoutType?: PayoutType | undefined, fundingType?: FundingType | undefined} | {[key: string]: unknown}; export type SchemasThreeDsAuthentication = 'none' | 'attempt' | 'success'; export type SchemeFeeRequest = {cardNumber: OpenapiComponentsSchemasPan2, amount: AmountRequest, merchantAcceptorKey: AcceptorKey, chargeType: OpenapiComponentsSchemasChargeType, threeDsAuthentication: SchemasThreeDsAuthentication}; export type CreateApiFeesSchemeEstimateArgs = {body: SchemeFeeRequest}; export type CreateApiFeesSchemeEstimateOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const componentsSchemasCardNetwork = z.enum(["mastercard", "visa", "discover", "bancontact"]); export const conversionRateDate = z.string(); export const openapiComponentsSchemasCurrencyCode5 = 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", "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", "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 const currencyRateValue = z.string().min(1); export const operation = z.enum(["divide", "multiply"]); export const conversionRate = z.object({date: conversionRateDate, source: openapiComponentsSchemasCurrencyCode5, target: openapiComponentsSchemasCurrencyCode5, rate: currencyRateValue, operation: operation}); export const billingFrequency = z.enum(["daily", "weekly", "monthly", "quarterly"]); export const feeId = z.string().min(1).max(120); export const feeDescription = z.string().min(1).max(120); export const sourceAmountImpact = z.enum(["debit", "credit"]); export const sourceAmountValue = z.string().min(1); export const sourceAmount = z.object({impact: sourceAmountImpact, currency: openapiComponentsSchemasCurrencyCode5, value: sourceAmountValue}); export const convertedSourceAmount = z.object({impact: sourceAmountImpact, currency: openapiComponentsSchemasCurrencyCode5, value: sourceAmountValue}); export const tierId = z.string().min(1).max(120); export const tieredEnum = z.enum(["count", "volume"]); export const numberOfTiers = z.number().int().gte(2).lte(6); export const feeRateType = z.enum(["fixed", "basisPoints"]); export const impact = z.enum(["debit", "credit"]); export const value = z.string().min(1); export const tierNumber = z.number().int().gte(1).lte(6); export const upToIncluding = z.number().int().gte(0); export const above = z.number().int().gte(0); export const tieredFeeRateTier = z.object({impact: impact, value: value, tierNumber: tierNumber, upToIncluding: upToIncluding.optional(), above: above.optional()}); export const tieredBillingAmountTier = z.object({impact: impact, value: value, tierNumber: tierNumber, upToIncluding: upToIncluding.optional(), above: above.optional()}); export const tieredFeeBillingAmount = z.object({currency: openapiComponentsSchemasCurrencyCode5, amounts: z.array(tieredBillingAmountTier)}); export const nonTieredEnum = z.literal("none"); export const nonTieredFeeRateTier = z.object({impact: impact, value: value}); export const nonTieredBillingAmountTier = z.object({impact: impact, value: value}); export const nonTieredFeeBillingAmount = z.object({currency: openapiComponentsSchemasCurrencyCode5, amounts: z.array(nonTieredBillingAmountTier)}); export const publicFee = z.union([z.object({billingFrequency: billingFrequency, feeId: feeId, feeDescription: feeDescription, sourceAmount: sourceAmount, convertedSourceAmount: convertedSourceAmount.optional(), tierId: tierId.optional(), tiering: tieredEnum, numberOfTiers: numberOfTiers, feeRate: z.object({type: feeRateType, currency: openapiComponentsSchemasCurrencyCode5.optional(), tiers: z.array(tieredFeeRateTier)}), billingAmount: tieredFeeBillingAmount}), z.object({billingFrequency: billingFrequency, feeId: feeId, feeDescription: feeDescription, sourceAmount: sourceAmount, convertedSourceAmount: convertedSourceAmount.optional(), tierId: tierId.optional(), tiering: nonTieredEnum, feeRate: z.object({type: feeRateType, currency: openapiComponentsSchemasCurrencyCode5.optional(), tiers: z.array(nonTieredFeeRateTier)}), billingAmount: nonTieredFeeBillingAmount})]); export const publicSchemeFeeEstimate = z.object({cardNetwork: componentsSchemasCardNetwork, conversionRates: z.array(conversionRate).optional(), fees: z.array(publicFee)}); 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 scheme fee [EXPERIMENTAL] * * POST /fees/scheme/estimate * * Also accepts auth: BearerToken */ export const createApiFeesSchemeEstimate = async (args: CreateApiFeesSchemeEstimateArgs, options?: CreateApiFeesSchemeEstimateOptions) => { 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/scheme/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(publicSchemeFeeEstimate, await res.json()) };