import {z} from 'zod' export type ChargeKey = string; export type SchemasActionReference = string; export type CancelRequest = {reference?: SchemasActionReference | undefined}; export type CreateApiPosChargesChargeKeyCancelArgs = {'Idempotency-Key'?: string | undefined, chargeKey: ChargeKey, body: CancelRequest}; export type CreateApiPosChargesChargeKeyCancelOptions = {baseUrl?: string | undefined, MutualTLS?: string | undefined, fetch?: typeof fetch | undefined}; export const schemasActionKey = z.string(); export const chargeKey = z.string().min(5).max(120); export const schemasActionReference = z.string().min(1).max(100); 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 isoFieldResponseCode = z.string(); export const isoFieldResponseCodeDescription = z.string(); export const authorizationIsoFieldsAmericanExpressWithDiscriminator = z.object({network: z.literal("american-express"), responseCode: isoFieldResponseCode, responseCodeDescription: isoFieldResponseCodeDescription}); export const schemasAuthorizationIsoFieldsBancontactWithDiscriminator = z.object({network: z.literal("bancontact"), responseCode: isoFieldResponseCode, responseCodeDescription: isoFieldResponseCodeDescription}); export const authorizationIsoFieldsDiscoverWithDiscriminator = z.object({network: z.literal("discover"), responseCode: isoFieldResponseCode, responseCodeDescription: isoFieldResponseCodeDescription}); export const authorizationIsoFieldsMastercardWithDiscriminator = z.object({network: z.literal("mastercard"), responseCode: isoFieldResponseCode, responseCodeDescription: isoFieldResponseCodeDescription}); export const authorizationIsoFieldsVisaWithDiscriminator = z.object({network: z.literal("visa"), responseCode: isoFieldResponseCode, responseCodeDescription: isoFieldResponseCodeDescription}); export const authorizationIsoFieldsUnknown = z.object({network: z.literal("unknown"), responseCode: z.literal("unknown"), responseCodeDescription: z.literal("unknown")}); export const authorizationResponseForReversalActions = z.union([authorizationIsoFieldsAmericanExpressWithDiscriminator, schemasAuthorizationIsoFieldsBancontactWithDiscriminator, authorizationIsoFieldsDiscoverWithDiscriminator, authorizationIsoFieldsMastercardWithDiscriminator, authorizationIsoFieldsVisaWithDiscriminator, authorizationIsoFieldsUnknown]); export const openapiComponentsSchemasCreated = z.string(); export const componentsSchemasLastModified = z.string(); export const schemasVersion = z.number().int(); export const schemasReversalAction = z.object({key: schemasActionKey, chargeKey: chargeKey, type: z.literal("reversal"), reference: schemasActionReference.optional(), status: z.object({authorization: z.enum(["approved", "declined"])}), replacementAmount: schemasResponseAmount, authorizationResponse: authorizationResponseForReversalActions, created: openapiComponentsSchemasCreated, lastModified: componentsSchemasLastModified.optional(), version: schemasVersion}); 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 }; /** * Cancel Charge [EXPERIMENTAL] * * POST /pos/charges/{chargeKey}/cancel */ export const createApiPosChargesChargeKeyCancel = async (args: CreateApiPosChargesChargeKeyCancelArgs, options?: CreateApiPosChargesChargeKeyCancelOptions) => { 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('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (MutualTLS !== undefined) headers.set("-", MutualTLS) const url = `${baseUrl.replace(/\/+$/, '')}/pos/charges/${encodeURIComponent(String(args.chargeKey))}/cancel${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(schemasReversalAction, await res.json()) };