import {z} from 'zod' export type GetApiPaymentsArgs = {limit?: number | undefined, page?: string | undefined, filter?: ({customer_id?: ({eq?: string | undefined}) | undefined, status?: ({eq?: ('pending' | 'in_transit' | 'in_review' | 'paid' | 'failed' | 'canceled') | undefined}) | undefined, amount?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined}) | undefined, sort?: ('id' | '-id' | 'created_at' | '-created_at' | 'amount' | '-amount') | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({id: z.string().optional(), customer_id: z.string().optional(), source: z.enum(["api", "merchant_user", "admin_user", "customer_user", "payment_gateway", "check", "guest_checkout"]).optional(), amount: z.number().optional(), method: z.enum(["check", "ach_debit", "direct_deposit", "card", "merchant", "unapplied_payment_adjustment", "credit_note", "adjustment", "wire"]).optional(), status: z.enum(["pending", "in_transit", "in_review", "paid", "failed", "canceled"]).optional(), created_at: z.string().optional(), paid_at: z.string().optional(), canceled_at: z.string().optional(), failed_at: z.string().optional(), processed_at: z.string().optional(), scheduled_at: z.string().optional(), processing_fee: z.number().optional(), canceled_code: z.string().optional(), failed_code: z.string().optional(), dispute_code: z.string().optional(), forward_reason_code: z.enum(["ach", "check", "wire", "credit_card", "other"]).nullable().optional(), forward_reason_message: z.string().nullable().optional(), payment_links: z.array(z.object({record_id: z.string().optional(), record_type: z.literal("invoice").optional(), amount: z.number().optional()})).optional(), created_by_user_id: z.string().optional()}); export const paymentListResponse = z.object({count: z.number().int().optional(), limit: z.number().int().optional(), page: z.number().int().optional(), results: z.array(payment).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 * * Also accepts auth: basicAuth */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.sort != null) query.append("sort", String(args.sort)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) 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(paymentListResponse, await res.json()) };