import {z} from 'zod' export type PaymentWrite = {payment: {payment_method_id?: string | undefined, amount_in_dollars?: number | undefined, paid_on?: string | undefined, company_id?: string | undefined, check_number?: string | undefined, posted?: boolean | undefined, info?: string | undefined, note?: string | undefined, cc_name?: string | undefined, cc_type?: string | undefined, cc_auth_code?: string | undefined, cc_number?: string | undefined, amount_in_cents?: number | undefined, balance_in_cents?: number | undefined, applied_payments_in_cents?: number | undefined, accounting_sync_state?: string | undefined, void_reason?: string | undefined, void_date?: string | undefined, credit_card_payment?: ({card_type?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, card_number?: string | undefined, expiry_date?: string | undefined, street?: string | undefined, street1?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, zip?: string | undefined, cvs?: string | undefined, verification_token?: string | undefined, shopvox_pay_payment_token?: string | undefined}) | undefined, ach_payment?: ({account_holder_name?: string | undefined, verification_token?: string | undefined}) | undefined, invoice_payments?: (Array<{invoice_id?: string | undefined, amount_in_dollars?: number | undefined, selected?: boolean | undefined}>) | undefined, order_payments?: (Array<{work_order_id?: string | undefined, amount_in_dollars?: number | undefined, selected?: boolean | undefined}>) | undefined}}; export type CreateApiPaymentsArgs = {body: PaymentWrite}; export type CreateApiPaymentsOptions = {baseUrl?: string | undefined, accountId?: string | undefined, authToken?: string | undefined, fetch?: typeof fetch | undefined}; export const namedRef = z.object({id: z.string().optional(), name: z.string().optional()}); export const payment = z.object({id: z.string().optional(), checkNumber: z.string().nullable().optional(), ccName: z.string().nullable().optional(), ccType: z.string().nullable().optional(), ccAuthCode: z.string().nullable().optional(), ccNumber: z.string().nullable().optional(), amountInDollars: z.number().nullable().optional(), accountingSyncState: z.string().nullable().optional(), balanceInCents: z.number().int().nullable().optional(), appliedPaymentsInCents: z.number().int().nullable().optional(), amountInCents: z.number().int().nullable().optional(), posted: z.boolean().optional(), info: z.string().nullable().optional(), company: z.object({id: z.string().optional(), name: z.string().optional(), phoneWithExt: z.string().nullable().optional(), specialNotes: z.string().nullable().optional()}).optional(), paymentMethod: namedRef.optional(), invoices: z.array(z.object({id: z.string().optional(), txnNumber: z.string().optional(), amountInDollars: z.number().nullable().optional(), paymentId: z.string().optional(), totalPaymentsInCents: z.number().int().nullable().optional(), balanceInCents: z.number().int().nullable().optional()})).optional(), workOrders: z.array(z.object({id: z.string().optional(), amountInDollars: z.number().nullable().optional(), paymentId: z.string().optional(), totalPaymentsInCents: z.number().int().nullable().optional(), balanceInCents: z.number().int().nullable().optional()})).optional(), createdById: z.string().nullable().optional(), createdBy: namedRef.optional(), updatedById: z.string().nullable().optional(), updatedBy: namedRef.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 }; /** * Record a payment * * POST /payments */ export const createApiPayments = async (args: CreateApiPaymentsArgs, options?: CreateApiPaymentsOptions) => { const {baseUrl = "https://api.shopvox.com/v1", fetch: fetchFn = fetch, accountId, authToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accountId !== undefined) query.set("account_id", accountId) if (authToken !== undefined) query.set("authToken", authToken) const url = `${baseUrl.replace(/\/+$/, '')}/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(payment, await res.json()) };