import {z} from 'zod' export type GetApiPaymentReceiptsArgs = {page?: number | undefined, per_page?: number | undefined, invoice_id?: string | undefined}; export type GetApiPaymentReceiptsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "ISK", "JMD", "JPY", "KES", "KGS", "KHR", "KMF", "KRW", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SYP", "SZL", "THB", "TJS", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW"]); export const paymentMethodObject = z.object({lago_id: z.string(), is_default: z.boolean(), payment_provider_code: z.string().nullable(), payment_provider_name: z.string().nullable(), payment_provider_type: z.enum(["null", "adyen", "cashfree", "gocardless", "stripe", "flutterwave", "moneyhash"]).nullable(), provider_method_id: z.string(), created_at: z.string()}); export const paymentObject = z.object({lago_id: z.string(), lago_customer_id: z.string(), external_customer_id: z.string(), invoice_ids: z.array(z.string()), invoice_numbers: z.array(z.string()), lago_payable_id: z.string(), payable_type: z.enum(["Invoice", "PaymentRequest"]), amount_cents: z.number().int(), amount_currency: currency, status: z.string(), payment_status: z.enum(["null", "succeeded", "failed", "pending", "processing"]).nullable(), type: z.enum(["manual", "provider"]), reference: z.string().nullable(), payment_provider_code: z.string().nullable(), payment_provider_type: z.enum(["null", "adyen", "cashfree", "gocardless", "stripe", "flutterwave", "moneyhash"]).nullable(), external_payment_id: z.string().nullable(), provider_payment_id: z.string().nullable(), provider_customer_id: z.string().nullable(), next_action: z.record(z.string(), z.unknown()), created_at: z.string(), payment_method: paymentMethodObject.optional()}); export const paymentReceiptObject = z.object({lago_id: z.string(), created_at: z.string(), number: z.string(), file_url: z.string().nullable(), xml_url: z.string().nullable(), payment: paymentObject}); export const paginationMeta = z.object({current_page: z.number().int(), next_page: z.number().int().nullable().optional(), prev_page: z.number().int().nullable().optional(), total_pages: z.number().int(), total_count: z.number().int()}); export const paymentReceiptsPaginated = z.object({payment_receipts: z.array(paymentReceiptObject), meta: paginationMeta}); 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 all payment receipts * * GET /payment_receipts */ export const getApiPaymentReceipts = async (args: GetApiPaymentReceiptsArgs, options?: GetApiPaymentReceiptsOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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.invoice_id != null) query.append("invoice_id", String(args.invoice_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment_receipts${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(paymentReceiptsPaginated, await res.json()) };