import {z} from 'zod' export type GetApiV2PaymentsArgs = {team_nanoid: string, state: 'all' | 'intent' | 'scheduled' | 'complete', start_date?: (number | string | unknown) | undefined, end_date?: (number | string | unknown) | undefined, query_type: 'payable' | 'receivable', recipient?: string | undefined}; export type GetApiV2PaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2PaymentsResponse = z.object({success: z.literal(true), data: z.object({items: z.array(z.object({recipient: z.object({nanoid: z.union([z.string().min(15).max(15), z.string().min(15).max(15)]), riseid: z.string().min(42).max(42), avatar: z.string(), first_name: z.string().nullable(), middle_name: z.string().nullable(), last_name: z.string().nullable(), company_name: z.string().nullable(), email: z.string().nullable(), address: z.object({country: z.string(), state: z.string().optional(), city: z.string().optional(), line_1: z.string().optional(), line_2: z.string().nullable().optional(), zip_code: z.string().optional()})}), payer: z.object({nanoid: z.string().min(15).max(15), riseid: z.string().min(42).max(42), avatar: z.string(), name: z.string()}), invoice: z.object({amount_cents: z.number(), automated_payment: z.boolean(), created_at: z.unknown(), currency: z.enum(["AED", "ARS", "AUD", "BDT", "BIF", "BOB", "BRL", "BTC", "CAD", "CDF", "CHF", "CLP", "CNY", "COP", "CRC", "CZK", "DKK", "EGP", "ETB", "EUR", "GBP", "GHS", "GMD", "GTQ", "HKD", "HUF", "IDR", "ILS", "INR", "JOD", "JPY", "KES", "KRW", "KWD", "LKR", "MAD", "MMK", "MNT", "MWK", "MXN", "MYR", "MZN", "NGN", "NOK", "NPR", "NZD", "PEN", "PHP", "PKR", "PLN", "QAR", "RON", "RWF", "SEK", "SGD", "THB", "TRY", "TZS", "UGX", "USD", "USDC", "USDT", "UYU", "UZS", "VND", "XAF", "XOF", "ZAR"]), documents_ids: z.array(z.string()), external_id: z.string().nullable().optional(), external_id_unique_claim: z.boolean().nullable().optional(), external_sow_url: z.string().nullable().optional(), flat_type: z.enum(["reimbursement", "product", "service"]).nullable().optional(), group_nanoid: z.string().min(15).max(15), group_onchain_id: z.string(), hourly_rate_usd_cents: z.number(), invoice_description: z.string().nullable().optional(), invoice_number: z.string().nullable(), invoice_status: z.enum(["draft", "creating", "open", "processing", "flushing", "closed", "open_canceled", "canceled", "failed"]), is_aor: z.boolean(), nanoid: z.string().min(15).max(15), pay_intent_time: z.number(), payer_nanoid: z.union([z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15)]), payer_payee_relationship: z.enum(["contractor", "aor_contractor", "team_employee", "external_recipient"]).nullable().optional(), payment_details: z.string().nullable().optional(), payment_onchain_id: z.string().nullable().optional(), payroll_program: z.enum(["riseworks_inc", "riseworks_inc_gb", "riseworks_eor_us", "riseworks_pps_us", "riseworks_eor_gb", "riseworks_eor_ca", "riseworks_eor_gr", "riseworks_eor_ie", "riseworks_eor_cy", "riseworks_eor_cz", "riseworks_eor_mx", "riseworks_eor_co", "riseworks_eor_in", "riseworks_eor_au", "riseworks_eor_nz", "riseworks_eor_za", "riseworks_eor_it", "riseworks_eor_pt", "riseworks_eor_fi", "riseworks_eor_cr", "riseworks_eor_ee", "riseworks_eor_mt", "riseworks_eor_pe", "riseworks_eor_nl", "riseworks_eor_lt", "riseworks_eor_sg", "riseworks_eor_ph", "riseworks_eor_pl", "riseworks_eor_ke", "riseworks_eor_ae", "riseworks_eor_ro", "riseworks_eor_ch", "riseworks_eor_sv", "riseworks_eor_cl", "riseworks_eor_pa", "riseworks_eor_es", "riseworks_eor_br", "riseworks_eor_dk"]).nullable().optional(), reason_id: z.number().nullable().optional(), recipient_nanoid: z.union([z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15), z.string().min(15).max(15)]), recurrence: z.enum(["daily", "weekly", "bi_weekly", "four_weeks", "monthly", "bi_monthly", "quartely", "six_months", "annually"]).nullable().optional(), rise_sow: z.boolean(), role_description: z.string().nullable().optional(), services_description: z.string().nullable().optional(), sow_document_nanoid: z.string().nullable().optional(), subscription_fee_metadata: z.object({subscription_type: z.number().int().gte(-9007199254740991).lte(9007199254740991), fee_month_epoch: z.number().int().gte(-9007199254740991).lte(9007199254740991), usage_month_epoch: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), recipients: z.array(z.object({address: z.string(), amount: z.number()})), v1_migration_type: z.enum(["V1_SUBSCRIPTION", "V1_CHARGEBACK"]).optional(), source_payment_invoice_number: z.string().optional()}).nullable().optional(), time_entries: z.array(z.object({date: z.string(), hours: z.number().int().gte(-9007199254740991).lte(9007199254740991), minutes: z.number().int().gte(-9007199254740991).lte(9007199254740991), notes: z.string()})), type: z.enum(["time_entry", "flat", "paynow", "payonce", "recurring", "milestone", "batch", "payroll_cash_requirement", "payroll_cash_requirement_chargeback", "payroll_net_payment", "external_recipient_payment", "subscription_fee"]), updated_at: z.unknown()}), payment: z.object({created_at: z.unknown(), creation_transaction: z.string().min(15).max(15).nullable(), group_onchain_id: z.string(), invoice_nanoid: z.string().min(15).max(15), nanoid: z.string().min(15).max(15), onchain_id: z.string(), payment_group_nanoid: z.string().min(15).max(15), process_transaction: z.string().min(15).max(15).nullable(), updated_at: z.unknown(), v1_metadata: z.string().nullable().optional(), state: z.enum(["Undefined", "Intent", "Scheduled", "Complete", "Removed"]), pay_at_time: z.number().int().gte(-9007199254740991).lte(9007199254740991), token_amount: z.string(), amount_cents: z.number().int().gte(-9007199254740991).lte(9007199254740991), currency_symbol: z.string(), token_decimals: z.number().int().gte(-9007199254740991).lte(9007199254740991), valid_minutes: z.number().int().gte(-9007199254740991).lte(9007199254740991)})}))})}); 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 }; /** * Query Payments * * GET /v2/payments */ export const getApiV2Payments = async (args: GetApiV2PaymentsArgs, options?: GetApiV2PaymentsOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.team_nanoid != null) query.append("team_nanoid", String(args.team_nanoid)) if (args.state != null) query.append("state", String(args.state)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.query_type != null) query.append("query_type", String(args.query_type)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(getApiV2PaymentsResponse, await res.json()) };