import {z} from 'zod' export type VistaConnectOrderType = 'Standard' | 'GiftShop' | 'Subscription' | 'Tab'; export type GetApiOcapiV1WebPaymentMethodsArgs = {orderType: VistaConnectOrderType, siteId?: string | undefined, 'Connect-Region-Code'?: string | undefined}; export type GetApiOcapiV1WebPaymentMethodsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ocapiV1PaymentsMasterDataWebPaymentMethodsWebPaymentMethodType = z.enum(["Redirect", "Embedded"]); export const ocapiV1Translation = z.object({languageTag: z.string().min(1), text: z.string().min(1)}); export const ocapiV1Translatable = z.object({text: z.string().min(1), translations: z.array(ocapiV1Translation)}); export const ocapiV1PaymentsMasterDataWebPaymentMethod = z.object({type: ocapiV1PaymentsMasterDataWebPaymentMethodsWebPaymentMethodType, isDefault: z.boolean(), id: z.number().int(), name: ocapiV1Translatable, displayPriority: z.number().int(), isRecurringBillingSupported: z.boolean()}); export const ocapiV1PaymentsMasterDataWebPaymentMethodListDetails = z.object({webPaymentMethods: z.array(ocapiV1PaymentsMasterDataWebPaymentMethod)}); 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 }; /** * Gets the web payment method options for an in progress order base, based on the provided order type * * GET /ocapi/v1/web-payment-methods */ export const getApiOcapiV1WebPaymentMethods = async (args: GetApiOcapiV1WebPaymentMethodsArgs, options: GetApiOcapiV1WebPaymentMethodsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderType != null) query.append("orderType", String(args.orderType)) if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args['Connect-Region-Code'] != null) headers.set("Connect-Region-Code", String(args['Connect-Region-Code'])) const url = `${baseUrl.replace(/\/+$/, '')}/ocapi/v1/web-payment-methods${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(ocapiV1PaymentsMasterDataWebPaymentMethodListDetails, await res.json()) };