import {z} from 'zod' export type CreateGenericPaymentV2Dto = {type: 'cash' | 'credit' | 'check', amount: number, confirmationCode?: string | undefined, date?: string | undefined}; export type CreateApiV2InvoicesIdPaymentsArgs = {id: string, Authorization: string, body: CreateGenericPaymentV2Dto}; export type CreateApiV2InvoicesIdPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseLinkedPaymentV2Dto = z.object({id: z.string().optional(), date: z.string().optional(), amount: z.number().optional(), tipAmount: z.number().optional(), type: z.enum(["charge", "cash", "credit", "check", "refund", "refund_offline", "bank_transfer", "bank_transfer_offline", "cash_app", "consumer_financing", "venmo", "zelle", "debit_offline", "installments", "interac", "check_deposit"]).optional(), status: z.enum(["pending", "failed", "succeeded"]).optional(), estimateId: z.string().nullable().optional(), confirmationCode: z.string().optional(), collectedById: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), jobId: z.string().nullable().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 }; /** * Add a payment to an Invoice * * POST /v2/invoices/{id}/payments */ export const createApiV2InvoicesIdPayments = async (args: CreateApiV2InvoicesIdPaymentsArgs, options?: CreateApiV2InvoicesIdPaymentsOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/invoices/${encodeURIComponent(String(args.id))}/payments${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(responseLinkedPaymentV2Dto, await res.json()) };