import {z} from 'zod' export type GetApiCatalogPaymentsInvoiceIntializePaymentArgs = {transactionDetails?: Record | undefined, billingInfo?: Record | undefined, shippingInfo?: Record | undefined, paymentDetails?: Record | undefined}; export type GetApiCatalogPaymentsInvoiceIntializePaymentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCatalogPaymentsInvoiceIntializePaymentResponse = z.object({ProcessorId: z.unknown(), TokenFK: z.string().optional(), HostedPaymentURL: z.string().optional(), Mode: z.number().int().optional(), TokenDetails: z.unknown(), MetaData: z.unknown(), Error: z.unknown()}); 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 }; /** * Collect payment for a class booking by using a new credit/debit card * * GET /Catalog/Payments/Invoice/IntializePayment */ export const getApiCatalogPaymentsInvoiceIntializePayment = async (args: GetApiCatalogPaymentsInvoiceIntializePaymentArgs, options?: GetApiCatalogPaymentsInvoiceIntializePaymentOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.transactionDetails != null) query.append("transactionDetails", JSON.stringify(args.transactionDetails)) if (args.billingInfo != null) query.append("billingInfo", JSON.stringify(args.billingInfo)) if (args.shippingInfo != null) query.append("shippingInfo", JSON.stringify(args.shippingInfo)) if (args.paymentDetails != null) query.append("paymentDetails", JSON.stringify(args.paymentDetails)) const url = `${baseUrl.replace(/\/+$/, '')}/Catalog/Payments/Invoice/IntializePayment${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(getApiCatalogPaymentsInvoiceIntializePaymentResponse, await res.json()) };