import {z} from 'zod' export type PaymentType = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31' | '32' | '33'; export type InvoicePaymentable = {invoice_id?: string | undefined, amount?: string | undefined}; export type CreditPaymentable = {credit_id?: string | undefined, amount?: string | undefined}; export type PaymentRequest = {client_id?: string | undefined, client_contact_id?: string | undefined, user_id?: string | undefined, type_id?: PaymentType | undefined, date?: string | undefined, transaction_reference?: string | undefined, assigned_user_id?: string | undefined, private_notes?: string | undefined, amount?: number | undefined, invoices?: Array | undefined, credits?: Array | undefined, number?: string | undefined}; export type CreateApiApiV1PaymentsArgs = {'X-API-TOKEN': string, 'X-Requested-With': string, include?: string | undefined, email_receipt?: boolean | undefined, body: PaymentRequest}; export type CreateApiApiV1PaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentType = z.enum(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33"]); export const paymentable = z.object({id: z.string().optional(), invoice_id: z.string().optional(), credit_id: z.string().optional(), refunded: z.number().optional(), amount: z.number().optional(), updated_at: z.number().optional(), created_at: z.number().optional()}); export const invoicePaymentable = z.object({invoice_id: z.string().optional(), amount: z.string().optional()}); export const creditPaymentable = z.object({credit_id: z.string().optional(), amount: z.string().optional()}); export const payment = z.object({id: z.string().optional(), client_id: z.string().optional(), invitation_id: z.string().optional(), client_contact_id: z.string().optional(), user_id: z.string().optional(), type_id: paymentType.optional(), date: z.string().optional(), transaction_reference: z.string().optional(), assigned_user_id: z.string().optional(), private_notes: z.string().optional(), is_manual: z.boolean().optional(), is_deleted: z.boolean().optional(), amount: z.number().optional(), refunded: z.number().optional(), updated_at: z.number().optional(), archived_at: z.number().optional(), company_gateway_id: z.string().optional(), paymentables: paymentable.optional(), invoices: z.array(invoicePaymentable).optional(), credits: z.array(creditPaymentable).optional(), number: z.string().optional(), category_id: z.string().optional(), custom_value1: z.string().optional(), custom_value2: z.string().optional(), custom_value3: z.string().optional(), custom_value4: z.string().optional(), exchange_currency_id: z.string().optional(), exchange_rate: z.number().optional(), idempotency_key: z.string().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 }; /** * Create payment * * POST /api/v1/payments */ export const createApiApiV1Payments = async (args: CreateApiApiV1PaymentsArgs, options?: CreateApiApiV1PaymentsOptions) => { const {baseUrl = "https://demo.invoiceninja.com", 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['X-API-TOKEN'] != null) headers.set("X-API-TOKEN", String(args['X-API-TOKEN'])) if (args['X-Requested-With'] != null) headers.set("X-Requested-With", String(args['X-Requested-With'])) if (args.include != null) query.append("include", String(args.include)) if (args.email_receipt != null) query.append("email_receipt", String(args.email_receipt)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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()) };