import {z} from 'zod' export type GetApiPaymentsPaymentArgs = {payment: string}; export type GetApiPaymentsPaymentOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentsPaymentResponse = z.object({id: z.string().optional(), status: z.string().optional(), amount: z.string().optional(), type: z.string().optional(), parent_type: z.string().optional(), parent_id: z.string().optional(), payment_attempts: z.array(z.object({id: z.string().optional(), created_at: z.string().optional(), expected_completion_date: z.string().optional(), status: z.string().optional(), failure_code: z.unknown(), payment_method: z.string().optional(), payment_instrument: z.string().optional(), wire_details: z.unknown(), ach_details: z.object({ach_trace_id: z.string().optional(), description: z.string().optional(), transaction_type_code: z.string().optional()}).optional()})).optional(), can_retry: z.boolean().optional(), can_refund: z.boolean().optional(), can_cancel: z.boolean().optional(), cancel_deadline: z.string().optional(), direction: z.string().optional(), company: z.string().optional(), can_reverse: z.boolean().optional(), reversal_deadline: 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 }; /** * Get a payment * * GET /payments/{payment} */ export const getApiPaymentsPayment = async (args: GetApiPaymentsPaymentArgs, options?: GetApiPaymentsPaymentOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.payment))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPaymentsPaymentResponse, await res.json()) };