import {z} from 'zod' export type CreateApiCatalogPaymentsInvoiceIntializeCloudPaymentArgs = {transactionDetails?: Record | undefined, billingInfo?: Record | undefined, shippingInfo?: Record | undefined, paymentDetails?: Record | undefined}; export type CreateApiCatalogPaymentsInvoiceIntializeCloudPaymentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCatalogPaymentsInvoiceIntializeCloudPaymentResponse = z.object({InvoiceTransactionId: z.string().optional(), PrintReciept: z.boolean().optional(), TransactionId: z.string().optional(), RequireSignature: z.boolean().optional(), success: z.boolean().optional(), ToastMessage: z.unknown(), Error: z.unknown(), HostedPaymentURL: z.unknown(), TokenFK: z.unknown(), ProcessingMessage: 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 the guest's saved credit/debit card * * POST /Catalog/Payments/Invoice/IntializeCloudPayment */ export const createApiCatalogPaymentsInvoiceIntializeCloudPayment = async (args: CreateApiCatalogPaymentsInvoiceIntializeCloudPaymentArgs, options?: CreateApiCatalogPaymentsInvoiceIntializeCloudPaymentOptions) => { 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/IntializeCloudPayment${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCatalogPaymentsInvoiceIntializeCloudPaymentResponse, await res.json()) };