import {z} from 'zod' export type CreateApiV1InvoicesInvoiceIdPaymentsGiftCardArgs = {invoice_id: string}; export type CreateApiV1InvoicesInvoiceIdPaymentsGiftCardOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InvoicesInvoiceIdPaymentsGiftCardResponse = z.object({is_applied: z.boolean().optional(), payment: z.object({payment_option: z.object({id: z.string().optional(), payment_mode: z.number().int().optional(), payment_name: z.string().optional(), card_number: z.string().optional(), additional_data: z.unknown()}).optional(), total_amount_paid: z.number().int().optional(), amount_paid: z.number().int().optional(), tip_amount: z.number().int().optional(), ssg_amount: z.number().int().optional(), fee_amount: z.number().int().optional(), cash_back_amount: z.number().int().optional(), transaction_id: z.string().optional(), payment_date: z.string().optional(), cash_regester_id: z.string().optional()}).optional(), error: z.object({code: z.number().int().optional(), message: z.string().optional()}).optional()}); 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 invoice payment by redeeming a guest's gift card * * POST /v1/invoices/{invoice_id}/payments/gift_card */ export const createApiV1InvoicesInvoiceIdPaymentsGiftCard = async (args: CreateApiV1InvoicesInvoiceIdPaymentsGiftCardArgs, options?: CreateApiV1InvoicesInvoiceIdPaymentsGiftCardOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/invoices/${encodeURIComponent(String(args.invoice_id))}/payments/gift_card${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(createApiV1InvoicesInvoiceIdPaymentsGiftCardResponse, await res.json()) };