import {z} from 'zod' export type GetApiPaymentsPaymentIdArgs = {payment_id: string}; export type GetApiPaymentsPaymentIdOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 payment by payment ID * * GET /payments/{payment_id} */ export const getApiPaymentsPaymentId = async (args: GetApiPaymentsPaymentIdArgs, options: GetApiPaymentsPaymentIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.payment_id))}${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(payment, await res.json()) };