import {z} from 'zod' export type PaymentStatus = 'CANCELED' | 'CREATED' | 'FAILED' | 'IN_REVIEW' | 'PRESENTING' | 'PROCESSING' | 'SUCCEEDED'; export type GetApiBillingPaymentTableArgs = {TENANT: string, 'API-KEY': string, page?: (number | null) | undefined, per_page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_direction?: ('asc' | 'desc' | null) | undefined, location_id?: (string | null) | undefined, id?: (string | null) | undefined, payer?: (string | null) | undefined, external_patient_id?: (string | null) | undefined, payer_type?: (string | null) | undefined, amount?: (Array | null) | undefined, last4?: (string | null) | undefined, comment?: (string | null) | undefined, payment_mediums?: (Array | null) | undefined, status?: (Array | null) | undefined, type?: (string | null) | undefined, tags?: (Array | null) | undefined, created_date?: (Array | string | null) | undefined, tz?: (string | null) | undefined, location_ids?: (Array | null) | undefined, creator_name?: (string | null) | undefined, merchant_account_ids?: (Array | null) | undefined}; export type GetApiBillingPaymentTableOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const paymentMethodType = z.enum(["CARD", "ACH", "APPLE_PAY"]); export const paymentMethodSummary = z.object({id: z.string(), patientId: z.string().nullable(), rainforestPaymentMethodId: z.string(), brand: z.string().nullable(), last4: z.string().nullable(), expMonth: z.number().int().nullable(), expYear: z.number().int().nullable(), accountHolderType: z.string().nullable(), accountNumberLast4: z.number().int().nullable(), bankName: z.string().nullable(), routingNumber: z.number().int().nullable(), description: z.string().nullable(), type: paymentMethodType.nullable(), isDefault: z.boolean().optional(), isArchived: z.boolean().optional()}); export const refundReason = z.enum(["DUPLICATE", "DISPUTE", "BOOKING_FEE", "OTHER"]); export const userType = z.enum(["PROVIDER", "PATIENT", "ASSISTANT"]); export const userTiny = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional()}); export const tableRefundOutput = z.object({id: z.string(), amount: z.number().int(), reason: refundReason, createdDate: z.string(), description: z.string().nullable().optional(), creator: userTiny.nullable().optional()}); export const itemWithQuantity = z.object({itemName: z.string(), description: z.string().nullable().optional(), chargeId: z.string().nullable().optional(), discountReason: z.string().nullable().optional(), discountAmount: z.number().int().nullable().optional(), discountPercentage: z.number().nullable().optional(), quantity: z.number().int(), unit: z.string().nullable().optional(), pricePerItem: z.number().int(), itemType: z.string().nullable().optional()}); export const paymentTableItem = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), patientId: z.string().nullable().optional(), externalPatientId: z.string().nullable().optional(), email: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), comment: z.string().nullable().optional(), amount: z.number().int(), currency: z.string(), status: z.string(), paymentMedium: z.string(), fee: z.number().int(), feeToPatient: z.number().int(), paymentMethod: paymentMethodSummary.nullable().optional(), createdDate: z.string(), refunds: z.array(tableRefundOutput), creator: userTiny.nullable().optional(), isPaymentPlan: z.boolean(), items: z.array(itemWithQuantity).optional(), isInsurancePayment: z.boolean(), locationId: z.string().nullable().optional()}); export const pagePaymentTableItem = z.object({data: z.array(paymentTableItem), total: z.number().int()}); export const paymentTable = z.object({items: pagePaymentTableItem, hasInsurancePayments: z.boolean()}); 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 /billing/payment/table */ export const getApiBillingPaymentTable = async (args: GetApiBillingPaymentTableArgs, options: GetApiBillingPaymentTableOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.id != null) query.append("id", String(args.id)) if (args.payer != null) query.append("payer", String(args.payer)) if (args.external_patient_id != null) query.append("external_patient_id", String(args.external_patient_id)) if (args.payer_type != null) query.append("payer_type", String(args.payer_type)) for (const item of args.amount ?? []) query.append("amount", String(item)) if (args.last4 != null) query.append("last4", String(args.last4)) if (args.comment != null) query.append("comment", String(args.comment)) for (const item of args.payment_mediums ?? []) query.append("payment_mediums", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.type != null) query.append("type", String(args.type)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.created_date != null) query.append("created_date", String(args.created_date)) if (args.tz != null) query.append("tz", String(args.tz)) for (const item of args.location_ids ?? []) query.append("location_ids", String(item)) if (args.creator_name != null) query.append("creator_name", String(args.creator_name)) for (const item of args.merchant_account_ids ?? []) query.append("merchant_account_ids", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/payment/table${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(paymentTable, await res.json()) };