import {z} from 'zod' export type InvoiceRecordPaymentInput = {amount: string, currency: string, payment_method: 'manual_bank_transfer' | 'issue_credit', external_reference?: (string | null) | undefined, paid_at: string, notes?: (string | null) | undefined}; export type CreateApiApiV1AdminInvoicesInvoiceIdPaymentsArgs = {invoiceId: string, body: InvoiceRecordPaymentInput}; export type CreateApiApiV1AdminInvoicesInvoiceIdPaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceResponse = z.object({success: z.boolean(), data: z.object({id: z.string(), invoice_number: z.string(), payment_reference: z.string(), merchant_id: z.string(), status: z.union([z.literal("draft"), z.literal("issued"), z.literal("paid"), z.literal("void")]), currency: z.string().min(3).max(3), period_start: z.string(), period_end: z.string(), total_amount: z.string(), amount_paid: z.string(), outstanding_amount: z.string(), transaction_count: z.number(), due_date: z.string().nullable(), issued_at: z.string().nullable(), issued_by_user_id: z.string().nullable(), paid_at: z.string().nullable(), voided_at: z.string().nullable(), voided_reason: z.string().nullable(), voided_by_user_id: z.string().nullable(), notes: z.string().nullable(), created_by_user_id: z.string().nullable(), created_at: z.string(), updated_at: z.string(), payments: z.array(z.object({id: z.string(), amount: z.string(), currency: z.string().min(3).max(3), payment_method: z.union([z.literal("manual_bank_transfer"), z.literal("immediately_settled"), z.literal("issue_credit")]), external_reference: z.string().nullable(), paid_at: z.string(), recorded_by_user_id: z.string().nullable(), recorded_at: z.string(), notes: z.string().nullable()})), fx_rates: z.array(z.object({source_currency: z.string().min(3).max(3), rate: z.string(), captured_at: z.string(), fetched_at: z.string().nullable(), provider: z.string()}))})}); 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 or pre-issue credit against an invoice. payment_method=manual_bank_transfer requires ISSUED (records received funds); issue_credit allowed on DRAFT or ISSUED (write-off / pre-issue discount, requires `notes`). Idempotent on (invoice_id, external_reference). * * POST /api/v1/admin/invoices/{invoiceId}/payments */ export const createApiApiV1AdminInvoicesInvoiceIdPayments = async (args: CreateApiApiV1AdminInvoicesInvoiceIdPaymentsArgs, options?: CreateApiApiV1AdminInvoicesInvoiceIdPaymentsOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/invoices/${encodeURIComponent(String(args.invoiceId))}/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(invoiceResponse, await res.json()) };