import {z} from 'zod' export type CreateApiDisputesArgs = {'Accept-Version': string, body: {transactionId: string, cardId?: string | undefined, disputeAmount?: number | undefined, disputeCurrency?: ('344' | '840') | undefined, cardholderName?: string | undefined, cardholderEmail?: string | undefined, contactEmail: string, disputeReason: 'FRAUD_EMV_LIABILITY_SHIFT_COUNTERFEIT' | 'FRAUD_EMV_LIABILITY_SHIFT_NON_COUNTERFEIT' | 'FRAUD_OTHER_CARD_PRESENT' | 'FRAUD_OTHER_CARD_ABSENT' | 'FRAUD_VISA_MONITORING_PROGRAM' | 'AUTHORIZATION_CARD_RECOVERY_BULLETIN' | 'AUTHORIZATION_DECLINED' | 'AUTHORIZATION_NO_AUTH_LATE_PRESENTMENT' | 'PROCESSING_ERROR_INCORRECT_TRANSACTION_CODE' | 'PROCESSING_ERROR_INCORRECT_CURRENCY' | 'PROCESSING_ERROR_INCORRECT_ACCOUNT_NUMBER' | 'PROCESSING_ERROR_INCORRECT_AMOUNT' | 'PROCESSING_ERROR_DUPLICATE_PROCESSING' | 'PROCESSING_ERROR_INVALID_DATA' | 'CONSUMER_MERCHANDISE_NOT_RECEIVED' | 'CONSUMER_CANCELLED_RECURRING' | 'CONSUMER_NOT_AS_DESCRIBED' | 'CONSUMER_COUNTERFEIT_MERCHANDISE' | 'CONSUMER_MISREPRESENTATION' | 'CONSUMER_CREDIT_NOT_PROCESSED' | 'CONSUMER_CANCELLED_MERCHANDISE' | 'CONSUMER_ORIGINAL_CREDIT_NOT_ACCEPTED' | 'CONSUMER_NON_RECEIPT_CASH_ATM'}}; export type CreateApiDisputesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDisputesResponse = z.object({id: z.string(), status: z.enum(["PENDING", "RECEIVED", "REVIEWING", "SUBMITTED", "REQUIRE_ACTION", "WON", "REFUNDED", "LOST", "DECLINED", "CANCELED"]), transactionId: z.string(), disputeAmount: z.number(), disputeCurrency: z.enum(["344", "840"]), disputeReason: z.string(), cardId: z.string(), cardholderName: z.string().nullable().optional(), cardholderEmail: z.string().nullable().optional(), contactEmail: z.string(), createdAt: z.string()}); 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 }; /** * Create a dispute * * POST /disputes */ export const createApiDisputes = async (args: CreateApiDisputesArgs, options?: CreateApiDisputesOptions) => { const {baseUrl = "https://sandbox.api.caas.reap.global", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Version'] != null) headers.set("Accept-Version", String(args['Accept-Version'])) if (ApiKeyAuth !== undefined) headers.set("x-reap-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/disputes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiDisputesResponse, await res.json()) };