import {z} from 'zod' export type GetApiPaymentsArgs = {page?: number | undefined, size?: number | undefined, sending_host?: string | undefined, receiving_host?: string | undefined, sending_account?: string | undefined, receiving_account?: string | undefined, sending_currency?: string | undefined, receiving_currency?: string | undefined, payment_id?: string | undefined, payment_type?: string | undefined, internal_id?: string | undefined, sender_end_to_end_id?: string | undefined, connector_role?: string | undefined, state?: string | undefined, states?: Array | undefined, before?: string | undefined, after?: string | undefined, range_field?: string | undefined, amount_range_field?: ('SENDING_AMOUNT' | 'RECEIVING_AMOUNT') | undefined, min_amount?: number | undefined, max_amount?: number | undefined, with_labels?: Array | undefined, without_labels?: Array | undefined, sort_field?: string | undefined, sort_direction?: string | undefined, load_additional_details?: boolean | undefined, payment_ids?: Array | undefined}; export type GetApiPaymentsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sort = z.object({direction: z.string().optional(), property: z.string().optional(), ignoreCase: z.boolean().optional(), nullHandling: z.string().optional(), ascending: z.boolean().optional(), descending: z.boolean().optional()}); export const fXRate = z.object({rate: z.number(), base_currency_code: z.string(), counter_currency_code: z.string(), type: z.string()}); export const quoteElement = z.object({quote_element_id: z.string(), quote_element_type: z.enum(["TRANSFER", "EXCHANGE", "EXCHANGE_TRADE", "CRYPTO_TRANSFER"]), quote_element_order: z.string(), sender_address: z.string(), receiver_address: z.string(), sending_amount: z.number(), receiving_amount: z.number(), sending_fee: z.number(), receiving_fee: z.number(), sending_currency_code: z.string().optional(), receiving_currency_code: z.string().optional(), fx_rate: fXRate.optional(), transfer_currency_code: z.string().optional()}); export const quote = z.object({quote_id: z.string(), created_at: z.string(), expires_at: z.string(), type: z.enum(["SENDER_AMOUNT", "RECEIVER_AMOUNT", "SENDER_INSTITUTION_AMOUNT", "RECEIVER_INSTITUTION_AMOUNT", "REVERSAL_AMOUNT"]), price_guarantee: z.string(), sender_address: z.string(), receiver_address: z.string(), amount: z.string(), currency_code: z.string(), currency_code_filter: z.string(), service_type: z.string(), quote_elements: z.array(quoteElement), liquidity_warning: z.string(), payment_method: z.string().optional(), payment_method_fields: z.string().optional(), payout_method_info: z.object({payout_method_name: z.string().optional(), payout_method_category: z.enum(["REAL_TIME_GROSS_SETTLEMENT_SYSTEM", "REAL_TIME_NET_SETTLEMENT_SYSTEM", "MASS_NET_PAYMENT_SYSTEM", "BOOK_TRANSFER", "CASH_PAYOUT", "WALLET_PAYMENT", "OTHER"]).optional(), description: z.string().optional(), estimated_time_to_credit: z.string().optional()}).optional()}); export const feeDetails = z.object({fee_value: z.number(), fee_currency: z.string(), category: z.string(), fee_description: z.string().optional()}); export const totalFees = z.object({total_fee: z.number(), fee_currency: z.string()}); export const feeInfo = z.object({nodes: z.record(z.string(), z.array(feeDetails)), total_fees: z.array(totalFees)}); export const paymentContract = z.object({sender_end_to_end_id: z.string().min(1).max(128), created_at: z.string(), expires_at: z.string(), quote: quote, fee_info: feeInfo.optional()}); export const rippleNetInfoEntry = z.object({info: z.string().optional(), created_at: z.string().optional()}); export const rippleNetInfo = z.object({node_address: z.string(), settlement_declined: z.array(rippleNetInfoEntry)}); export const executionResult = z.object({execution_result_id: z.string(), execution_timestamp: z.string(), execution_result_type: z.enum(["TRANSFER", "EXCHANGE", "EXCHANGE_TRADE", "CRYPTO_TRANSFER"]), execution_result_order: z.number().int(), sender_address: z.string(), receiver_address: z.string(), sending_amount: z.number(), receiving_amount: z.number(), sending_fee: z.number(), receiving_fee: z.number(), sending_currency_code: z.string().optional(), receiving_currency_code: z.string().optional(), fx_rate: fXRate.optional(), transfer_currency_code: z.string().optional(), intermediary_delta: z.number().optional(), incentive_type: z.string().optional(), incentive_value: z.number().optional(), transaction_hash: z.string().optional(), venue_id: z.string().optional(), fiat_adjusted_value: z.number().optional(), odl_payment_id: z.string().optional()}); export const liquidationDetails = z.object({id: z.string().optional(), status: z.string().optional(), failure_reason: z.string().optional(), failure_count: z.number().int().optional()}); export const label = z.object({label: z.string().optional()}); export const internalInfo = z.object({connector_role: z.enum(["SENDING", "RECEIVING", "INTERMEDIARY", "INTERNAL"]), labels: z.array(label), internal_id: z.string()}); export const userInfoEntry = z.object({json: z.object({}), created_at: z.string(), subState: z.string().optional()}); export const userInfo = z.object({node_address: z.string(), accepted: z.array(userInfoEntry), locked: z.array(userInfoEntry), lock_declined: z.array(userInfoEntry), retry_accept: z.array(userInfoEntry), retry_settlement: z.array(userInfoEntry), settlement: z.array(userInfoEntry), settlement_declined: z.array(userInfoEntry), failed: z.array(userInfoEntry), executed: z.array(userInfoEntry), completed: z.array(userInfoEntry), forwarded: z.array(userInfoEntry), returned: z.array(userInfoEntry)}); export const payment = z.object({payment_id: z.string(), contract_hash: z.string(), payment_state: z.enum(["ACCEPTED", "AWAITING_COLLECTION", "COMPLETED", "EXECUTED", "FAILED", "FORWARDED", "LOCK_DECLINED", "LOCKED", "PREPARED", "RETURNED", "SETTLEMENT_DECLINED"]), modified_at: z.string(), contract: paymentContract, ripplenet_info: z.array(rippleNetInfo).optional(), execution_condition: z.string(), crypto_transaction_id: z.string(), validator: z.string(), payment_type: z.enum(["REGULAR", "RETURN"]), returns_payment_with_id: z.string(), returned_by_payment_with_id: z.string(), execution_results: z.array(executionResult), liquidation_execution_results: z.array(executionResult).optional(), liquidation_details: liquidationDetails.optional(), push_forward_execution_results: z.array(executionResult).optional(), accepted_at: z.string(), locked_at: z.string().optional(), executed_at: z.string().optional(), completed_at: z.string().optional(), returned_at: z.string().optional(), internal_info: internalInfo, user_info: z.array(userInfo)}); export const payments = z.object({first: z.boolean().optional(), last: z.boolean().optional(), number: z.number().int().optional(), numberOfElements: z.number().int().optional(), size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), sort: z.array(sort).optional(), content: 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 }; /** * Get payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options: GetApiPaymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = 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.size != null) query.append("size", String(args.size)) if (args.sending_host != null) query.append("sending_host", String(args.sending_host)) if (args.receiving_host != null) query.append("receiving_host", String(args.receiving_host)) if (args.sending_account != null) query.append("sending_account", String(args.sending_account)) if (args.receiving_account != null) query.append("receiving_account", String(args.receiving_account)) if (args.sending_currency != null) query.append("sending_currency", String(args.sending_currency)) if (args.receiving_currency != null) query.append("receiving_currency", String(args.receiving_currency)) if (args.payment_id != null) query.append("payment_id", String(args.payment_id)) if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args.internal_id != null) query.append("internal_id", String(args.internal_id)) if (args.sender_end_to_end_id != null) query.append("sender_end_to_end_id", String(args.sender_end_to_end_id)) if (args.connector_role != null) query.append("connector_role", String(args.connector_role)) if (args.state != null) query.append("state", String(args.state)) if (args.states != null) query.append("states", args.states.join(',')) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.range_field != null) query.append("range_field", String(args.range_field)) if (args.amount_range_field != null) query.append("amount_range_field", String(args.amount_range_field)) if (args.min_amount != null) query.append("min_amount", String(args.min_amount)) if (args.max_amount != null) query.append("max_amount", String(args.max_amount)) if (args.with_labels != null) query.append("with_labels", args.with_labels.join(',')) if (args.without_labels != null) query.append("without_labels", args.without_labels.join(',')) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.load_additional_details != null) query.append("load_additional_details", String(args.load_additional_details)) if (args.payment_ids != null) query.append("payment_ids", args.payment_ids.join(',')) if (Bearer !== undefined) headers.set("Authorization", Bearer) 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(payments, await res.json()) };