import {z} from 'zod' export type GetApiPaymentsArgs = {Authorization: string, 'Sub-Account'?: string | undefined, payment_method_id?: string | undefined, void_id?: string | undefined, created_before?: string | undefined, created_after?: string | undefined, payment_status?: ('succeeded' | 'failed' | 'pending' | 'authorized' | 'refunded' | 'disputed') | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiPaymentsResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(z.unknown()).optional(), page_info: pageInfo.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 }; /** * List Payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Sub-Account'] != null) headers.set("Sub-Account", String(args['Sub-Account'])) if (args.payment_method_id != null) query.append("payment_method_id", String(args.payment_method_id)) if (args.void_id != null) query.append("void_id", String(args.void_id)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.payment_status != null) query.append("payment_status", String(args.payment_status)) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(getApiPaymentsResponse, await res.json()) };