import {z} from 'zod' export type Payment = {id?: number | undefined, invoice?: (string | null) | undefined, subscription?: (string | null) | undefined, customer?: (string | null) | undefined, amount: number, balance?: number | undefined, currency: string, date: string, payment_date?: (string | null) | undefined, description: string, source?: (number | null) | undefined, sources?: Array | undefined, status?: ('cancelled' | 'failed' | 'incomplete' | 'processing' | 'requires_payment_method' | 'succeeded') | undefined, failed_reason?: (string | null) | undefined, return_url?: (string | null) | undefined, enable_checkout?: boolean | undefined, metadata?: Record | undefined, test_mode?: boolean | undefined, created?: string | undefined, updated?: string | undefined}; export type PatchApiPaymentsIdArgs = {body: Payment}; export type PatchApiPaymentsIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({id: z.number().int().optional(), invoice: z.string().nullable().optional(), subscription: z.string().nullable().optional(), customer: z.string().nullable().optional(), amount: z.number(), balance: z.number().optional(), currency: z.string(), date: z.string(), payment_date: z.string().nullable().optional(), description: z.string().max(256), source: z.number().int().nullable().optional(), sources: z.array(z.number().int()).optional(), status: z.enum(["cancelled", "failed", "incomplete", "processing", "requires_payment_method", "succeeded"]).optional(), failed_reason: z.string().nullable().optional(), return_url: z.string().nullable().optional(), enable_checkout: z.boolean().optional(), metadata: z.object({}).optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: 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 }; /** * Partial update payment * * PATCH /payments/{id}/ */ export const patchApiPaymentsId = async (args: PatchApiPaymentsIdArgs, options?: PatchApiPaymentsIdOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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()) };