import {z} from 'zod' export type SchemasMerchantKey = string; export type ComponentsSchemasOffsetToken = string; export type GetApiPosChargesArgs = {merchantKey: SchemasMerchantKey, terminalId?: string | undefined, limit?: number | undefined, offsetToken?: ComponentsSchemasOffsetToken | undefined}; export type GetApiPosChargesOptions = {baseUrl?: string | undefined, MutualTLS?: string | undefined, fetch?: typeof fetch | undefined}; export const componentsSchemasOffsetToken = z.string().min(1).max(2048); export const chargeKey = z.string().min(5).max(120); export const schemasMerchantKey = z.string().min(5).max(120); export const openapiComponentsSchemasCurrencyCode2 = z.enum(["AED", "AFN", "ALL", "AMD", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SOS", "SRD", "SSP", "STN", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XCD", "XCG", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"]); export const schemasResponseAmountBreakdown = z.object({baseAmount: z.number().int().gte(1).lte(999999999999), cashback: z.number().int().gte(1).lte(999999999999).optional(), gratuity: z.number().int().gte(1).lte(999999999999).optional()}); export const schemasResponseAmount = z.object({value: z.number().int().gte(1).lte(999999999999), currency: openapiComponentsSchemasCurrencyCode2, breakdown: schemasResponseAmountBreakdown.optional()}); export const componentsSchemasIntent = z.enum(["purchase", "reservation", "refund"]); export const componentsSchemasCardEntry = z.literal("terminal"); export const schemasOrder = z.literal("counter"); export const terminalEntryMode = z.enum(["contactless", "manual-entry", "chip", "magstripe", "magstripe-fallback"]); export const schemasSequence = z.literal("subsequent"); export const schemasChargeType = z.object({intent: componentsSchemasIntent, cardEntry: componentsSchemasCardEntry, order: schemasOrder, terminalEntryMode: terminalEntryMode, sequence: schemasSequence.optional()}); export const transactionReference = z.string().min(1).max(120); export const chargeListItem = z.object({chargeKey: chargeKey, merchantKey: schemasMerchantKey, terminalId: z.string().min(1).max(8), created: z.string(), amount: schemasResponseAmount, status: z.enum(["approved", "canceled", "refunded"]), cardMaskedNumber: z.string(), type: schemasChargeType, transactionReference: transactionReference.optional()}); export const chargeListResponse = z.object({itemCount: z.number().int(), moreItems: z.boolean(), offsetToken: componentsSchemasOffsetToken.optional(), charges: z.array(chargeListItem)}); 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 Charges [EXPERIMENTAL] * * GET /pos/charges */ export const getApiPosCharges = async (args: GetApiPosChargesArgs, options?: GetApiPosChargesOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, MutualTLS} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchantKey != null) query.append("merchantKey", String(args.merchantKey)) if (args.terminalId != null) query.append("terminalId", String(args.terminalId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offsetToken != null) query.append("offsetToken", String(args.offsetToken)) if (MutualTLS !== undefined) headers.set("-", MutualTLS) const url = `${baseUrl.replace(/\/+$/, '')}/pos/charges${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(chargeListResponse, await res.json()) };