import {z} from 'zod' export type DisputeStatus = 'dispute_opened' | 'dispute_expired' | 'dispute_accepted' | 'dispute_cancelled' | 'dispute_challenged' | 'dispute_won' | 'dispute_lost'; export type DisputeStage = 'pre_dispute' | 'dispute' | 'pre_arbitration' | 'arbitration' | 'dispute_reversal'; export type GetApiDisputesListArgs = {limit?: (number | null) | undefined, dispute_status?: (DisputeStatus | null) | undefined, dispute_stage?: (DisputeStage | null) | undefined, reason?: (string | null) | undefined, connector?: (string | null) | undefined, received_time?: (string | null) | undefined, 'received_time.lt'?: (string | null) | undefined, 'received_time.gt'?: (string | null) | undefined, 'received_time.lte'?: (string | null) | undefined, 'received_time.gte'?: (string | null) | undefined}; export type GetApiDisputesListOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const stringMinorUnit = z.string(); export const currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"]); export const disputeStage = z.enum(["pre_dispute", "dispute", "pre_arbitration", "arbitration", "dispute_reversal"]); export const disputeStatus = z.enum(["dispute_opened", "dispute_expired", "dispute_accepted", "dispute_cancelled", "dispute_challenged", "dispute_won", "dispute_lost"]); export const disputeResponse = z.object({dispute_id: z.string(), payment_id: z.string(), attempt_id: z.string(), amount: stringMinorUnit, currency: currency, dispute_stage: disputeStage, dispute_status: disputeStatus, connector: z.string(), connector_status: z.string(), connector_dispute_id: z.string(), connector_reason: z.string().nullable().optional(), connector_reason_code: z.string().nullable().optional(), challenge_required_by: z.string().nullable().optional(), connector_created_at: z.string().nullable().optional(), connector_updated_at: z.string().nullable().optional(), created_at: z.string(), profile_id: z.string().nullable().optional(), merchant_connector_id: z.string().nullable().optional(), is_already_refunded: z.boolean()}); export const getApiDisputesListResponse = z.array(disputeResponse); 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 }; /** * Disputes - List Disputes * * GET /disputes/list */ export const getApiDisputesList = async (args: GetApiDisputesListArgs, options: GetApiDisputesListOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.dispute_status != null) query.append("dispute_status", String(args.dispute_status)) if (args.dispute_stage != null) query.append("dispute_stage", String(args.dispute_stage)) if (args.reason != null) query.append("reason", String(args.reason)) if (args.connector != null) query.append("connector", String(args.connector)) if (args.received_time != null) query.append("received_time", String(args.received_time)) if (args['received_time.lt'] != null) query.append("received_time.lt", String(args['received_time.lt'])) if (args['received_time.gt'] != null) query.append("received_time.gt", String(args['received_time.gt'])) if (args['received_time.lte'] != null) query.append("received_time.lte", String(args['received_time.lte'])) if (args['received_time.gte'] != null) query.append("received_time.gte", String(args['received_time.gte'])) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/disputes/list${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(getApiDisputesListResponse, await res.json()) };