import {z} from 'zod' export type GetApiV1CustomerDisputesArgs = {start_time?: string | undefined, disputed_transaction_id?: string | undefined, page_size?: number | undefined, next_page_token?: string | undefined, dispute_state?: ('REQUIRED_ACTION' | 'REQUIRED_OTHER_PARTY_ACTION' | 'UNDER_PAYPAL_REVIEW' | 'RESOLVED' | 'OPEN_INQUIRIES' | 'APPEALABLE') | undefined, update_time_before?: string | undefined, update_time_after?: string | undefined}; export type GetApiV1CustomerDisputesOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string().min(20).max(64); export const disputeReason = z.enum(["MERCHANDISE_OR_SERVICE_NOT_RECEIVED", "MERCHANDISE_OR_SERVICE_NOT_AS_DESCRIBED", "UNAUTHORISED", "CREDIT_NOT_PROCESSED", "DUPLICATE_TRANSACTION", "INCORRECT_AMOUNT", "PAYMENT_BY_OTHER_MEANS", "CANCELED_RECURRING_BILLING", "PROBLEM_WITH_REMITTANCE", "OTHER"]).min(1).max(255); export const status = z.enum(["ACTIVE", "INACTIVE"]).min(1).max(20); export const disputeState = z.enum(["OPEN_INQUIRIES", "REQUIRED_ACTION", "REQUIRED_OTHER_PARTY_ACTION", "UNDER_PAYPAL_REVIEW", "APPEALABLE", "RESOLVED"]).min(1).max(255); export const currencyCode = z.string().min(3).max(3); export const money = z.object({currency_code: currencyCode, value: z.string().max(32)}); export const cryptocurrencySymbol = z.enum(["BTC", "ETH", "BCH", "LTC", "PYUSD", "LINK", "SOL", "MATIC"]).min(1).max(10); export const cryptocurrencyQuantity = z.string().min(1).max(40); export const cryptocurrencySubunitsQuantity = z.string().min(1).max(81); export const cryptocurrencyDecimals = z.number().int().gte(0).lte(40); export const cryptocurrency = z.object({asset_symbol: cryptocurrencySymbol, quantity: cryptocurrencyQuantity, quantity_in_subunits: cryptocurrencySubunitsQuantity.optional(), decimals: cryptocurrencyDecimals.optional()}); export const disputeLifecycleStage = z.enum(["INQUIRY", "CHARGEBACK", "PRE_ARBITRATION", "ARBITRATION"]).min(1).max(255); export const disputeChannel = z.enum(["INTERNAL", "EXTERNAL", "ALERT"]).min(1).max(255); export const linkDescription = z.object({href: z.string(), rel: z.string(), method: z.enum(["GET", "POST", "PUT", "DELETE", "HEAD", "CONNECT", "OPTIONS", "PATCH"]).optional()}); export const definitionsLinkDescriptionList = z.array(linkDescription); export const disputeInfo = z.object({dispute_id: z.string().min(1).max(255).optional(), create_time: dateTime.optional(), update_time: dateTime.optional(), reason: disputeReason.optional(), status: status.optional(), dispute_state: disputeState.optional(), dispute_amount: money.optional(), dispute_asset: cryptocurrency.optional(), dispute_life_cycle_stage: disputeLifecycleStage.optional(), dispute_channel: disputeChannel.optional(), buyer_response_due_date: dateTime.optional(), seller_response_due_date: dateTime.optional(), links: definitionsLinkDescriptionList.optional()}); export const disputeInfoList = z.array(disputeInfo); export const disputeSearch = z.object({items: disputeInfoList.optional(), links: definitionsLinkDescriptionList.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 }; /** * List disputes * * GET /v1/customer/disputes * * Also accepts auth: Oauth2, Oauth2, Oauth2, Oauth2 */ export const getApiV1CustomerDisputes = async (args: GetApiV1CustomerDisputesArgs, options?: GetApiV1CustomerDisputesOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.disputed_transaction_id != null) query.append("disputed_transaction_id", String(args.disputed_transaction_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (args.dispute_state != null) query.append("dispute_state", String(args.dispute_state)) if (args.update_time_before != null) query.append("update_time_before", String(args.update_time_before)) if (args.update_time_after != null) query.append("update_time_after", String(args.update_time_after)) if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customer/disputes${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(disputeSearch, await res.json()) };