import {z} from 'zod' export type GetApiDisputesArgs = {'Finix-Version'?: string | undefined, adjustment_transfer_id?: string | undefined, application_id?: string | undefined, amount?: number | undefined, 'amount.gt'?: number | undefined, 'amount.gte'?: number | undefined, 'amount.lt'?: number | undefined, 'amount.lte'?: number | undefined, after_cursor?: string | undefined, before_cursor?: string | undefined, 'created_at.gte'?: string | undefined, 'created_at.lte'?: string | undefined, buyer_identity_first_name?: string | undefined, buyer_identity_last_name?: string | undefined, buyer_business_name?: string | undefined, buyer_doing_business_as?: string | undefined, instrument_bin?: string | undefined, instrument_brand_type?: string | undefined, instrument_card_last4?: string | undefined, instrument_card_type?: string | undefined, instrument_fingerprint?: string | undefined, instrument_name?: string | undefined, instrument_type?: string | undefined, limit?: number | undefined, merchant_identity_id?: string | undefined, merchant_identity_name?: string | undefined, 'merchant_identity_name.like'?: string | undefined, merchant_id?: string | undefined, merchant_mid?: string | undefined, names?: string | undefined, 'names.like'?: string | undefined, response_state?: string | undefined, 'respond_by.lte'?: string | undefined, 'respond_by.gte'?: string | undefined, tags?: string | undefined, 'tags.like'?: string | undefined, 'tags.key'?: string | undefined, 'tags.value'?: string | undefined, transfer_id?: string | undefined, state?: string | undefined, 'updated_at.gte'?: string | undefined, 'updated_at.lte'?: string | undefined}; export type GetApiDisputesOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const page = z.object({limit: z.number().int().optional(), next_cursor: z.string().nullable().optional()}); export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const applicationId = z.string().min(1); export const tags = z.record(z.string(), z.unknown()).nullable(); export const dispute = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), action: z.string().nullable().optional(), amount: z.number().int().nullable().optional(), application: applicationId.optional(), dispute_details: z.object({arn: z.string().optional(), case_id: z.string().nullable().optional(), pin_debit_adjustment_number: z.string().nullable().optional(), reason_code: z.string().nullable().optional()}).optional(), evidence_submitted: z.enum(["INQUIRY", "CHARGEBACK", "NONE", "NOT_SUPPORTED", "UNKNOWN"]).optional(), identity: z.string().optional(), merchant: z.string().optional(), message: z.string().nullable().optional(), occurred_at: z.string().nullable().optional(), reason: z.enum(["CLERICAL", "FRAUD", "INQUIRY", "QUALITY", "TECHNICAL"]).optional(), respond_by: z.string().nullable().optional(), response_state: z.enum(["NEEDS_RESPONSE", "RESPONDED", "ACCEPTED", "NO_RESPONSE_ALLOWED", "UNKNOWN"]).optional(), state: z.enum(["INQUIRY", "PENDING", "LOST", "WON"]).optional(), transfer: z.string().optional(), tags: tags.optional(), _links: z.object({adjustment_transfers: z.object({href: z.string().optional()}).optional(), application: z.object({href: z.string().optional()}).optional(), evidence: z.object({href: z.string().optional()}).optional(), self: z.object({href: z.string().optional()}).optional(), transfer: z.object({href: z.string().optional()}).optional()}).optional()}); export const self = z.object({href: z.string().min(1).optional()}); export const next = z.object({href: z.string().optional()}); export const getApiDisputesResponse = z.object({page: page.optional(), _embedded: z.object({disputes: z.array(dispute).optional()}).optional(), _links: z.object({self: self.optional(), next: next.optional()}).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 /disputes */ export const getApiDisputes = async (args: GetApiDisputesArgs, options?: GetApiDisputesOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (args.adjustment_transfer_id != null) query.append("adjustment_transfer_id", String(args.adjustment_transfer_id)) if (args.application_id != null) query.append("application_id", String(args.application_id)) if (args.amount != null) query.append("amount", String(args.amount)) if (args['amount.gt'] != null) query.append("amount.gt", String(args['amount.gt'])) if (args['amount.gte'] != null) query.append("amount.gte", String(args['amount.gte'])) if (args['amount.lt'] != null) query.append("amount.lt", String(args['amount.lt'])) if (args['amount.lte'] != null) query.append("amount.lte", String(args['amount.lte'])) if (args.after_cursor != null) query.append("after_cursor", String(args.after_cursor)) if (args.before_cursor != null) query.append("before_cursor", String(args.before_cursor)) if (args['created_at.gte'] != null) query.append("created_at.gte", String(args['created_at.gte'])) if (args['created_at.lte'] != null) query.append("created_at.lte", String(args['created_at.lte'])) if (args.buyer_identity_first_name != null) query.append("buyer_identity_first_name", String(args.buyer_identity_first_name)) if (args.buyer_identity_last_name != null) query.append("buyer_identity_last_name", String(args.buyer_identity_last_name)) if (args.buyer_business_name != null) query.append("buyer_business_name", String(args.buyer_business_name)) if (args.buyer_doing_business_as != null) query.append("buyer_doing_business_as", String(args.buyer_doing_business_as)) if (args.instrument_bin != null) query.append("instrument_bin", String(args.instrument_bin)) if (args.instrument_brand_type != null) query.append("instrument_brand_type", String(args.instrument_brand_type)) if (args.instrument_card_last4 != null) query.append("instrument_card_last4", String(args.instrument_card_last4)) if (args.instrument_card_type != null) query.append("instrument_card_type", String(args.instrument_card_type)) if (args.instrument_fingerprint != null) query.append("instrument_fingerprint", String(args.instrument_fingerprint)) if (args.instrument_name != null) query.append("instrument_name", String(args.instrument_name)) if (args.instrument_type != null) query.append("instrument_type", String(args.instrument_type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.merchant_identity_id != null) query.append("merchant_identity_id", String(args.merchant_identity_id)) if (args.merchant_identity_name != null) query.append("merchant_identity_name", String(args.merchant_identity_name)) if (args['merchant_identity_name.like'] != null) query.append("merchant_identity_name.like", String(args['merchant_identity_name.like'])) if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (args.merchant_mid != null) query.append("merchant_mid", String(args.merchant_mid)) if (args.names != null) query.append("names", String(args.names)) if (args['names.like'] != null) query.append("names.like", String(args['names.like'])) if (args.response_state != null) query.append("response_state", String(args.response_state)) if (args['respond_by.lte'] != null) query.append("respond_by.lte", String(args['respond_by.lte'])) if (args['respond_by.gte'] != null) query.append("respond_by.gte", String(args['respond_by.gte'])) if (args.tags != null) query.append("tags", String(args.tags)) if (args['tags.like'] != null) query.append("tags.like", String(args['tags.like'])) if (args['tags.key'] != null) query.append("tags.key", String(args['tags.key'])) if (args['tags.value'] != null) query.append("tags.value", String(args['tags.value'])) if (args.transfer_id != null) query.append("transfer_id", String(args.transfer_id)) if (args.state != null) query.append("state", String(args.state)) if (args['updated_at.gte'] != null) query.append("updated_at.gte", String(args['updated_at.gte'])) if (args['updated_at.lte'] != null) query.append("updated_at.lte", String(args['updated_at.lte'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiDisputesResponse, await res.json()) };