import {z} from 'zod' export type Amount = {amountLocalCurrency?: (string | null) | undefined, amountUSD?: (string | null) | undefined, currency?: (string | null) | undefined}; export type ClaimRequest = {additionalCost: Amount, amount: Amount, chargeId?: (string | null) | undefined, chargebackShippingInfo?: ({additionalShippingInfo?: (string | null) | undefined, carrier?: (string | null) | undefined, proofOfShippingURL?: (string | null) | undefined, signedProofOfShipping?: (boolean | null) | undefined, trackingNumber?: (string | null) | undefined} | null) | undefined, comments?: (string | null) | undefined, dueDate: string, externalClaimStatus?: (string | null) | undefined, goodsRecovered?: (boolean | null) | undefined, invoiceURL?: (string | null) | undefined, issueDate: string, orderId?: (string | null) | undefined, originalPayload?: ({[key: string]: unknown} | null) | undefined, processorChargebackCaseId?: (string | null) | undefined, processorName?: ('Adyen' | 'Airwallex' | 'AmazonPay' | 'AMEX' | 'AuthorizeNet' | 'Be2bill' | 'BlueSnap' | 'Braintree' | 'BusinessTrack' | 'Chase' | 'Checkout' | 'Citcon' | 'Citibank' | 'ConcoraCredit' | 'Credorax' | 'Cybersource' | 'Dalenys' | 'Discover' | 'Dlocal' | 'Ebanx' | 'Elavon' | 'Fidelity' | 'FirstData' | 'Fiserv' | 'Fortis' | 'Globalpay' | 'Ingenico' | 'IQ' | 'Klarna' | 'MCP' | 'MerchantConnect' | 'Merlink' | 'MidTrans' | 'NCR' | 'Nuvei' | 'OceanPay' | 'OmniPayGroup' | 'Paymentech' | 'Paymenttech' | 'PayPal' | 'PayPlus' | 'PaySafe' | 'PayU' | 'PayUEastEurope' | 'PayULatam' | 'Privvy' | 'Przelewy' | 'Revolut' | 'Safecharge' | 'Shift4' | 'Shopify' | 'Sofort' | 'Sprint' | 'Square' | 'Stripe' | 'Vantiv' | 'Wirecard' | 'Worldline' | 'Worldnet' | 'WorldpayAccertify' | 'WorldpayDisputes' | 'WorldpayMAI' | 'WorldpayWDP' | 'Xendit' | 'Zen' | null) | undefined, reason?: (string | null) | undefined, reasonCode?: (string | null) | undefined, reasonType: 'FRAUD' | 'SERVICE' | 'OTHER' | 'AUTHORIZATION' | 'PROCESSING_ERROR', sourceDetails?: (string | null) | undefined, sourceType: 'PROCESSOR_CB' | 'AMEX' | 'DISCOVER' | 'PAYPAL' | 'PROCESSOR_INQUIRY' | 'CUSTOMER_SUPPORT' | 'SHIPPING_CARRIER' | 'THIRD_PARTY' | 'FORTER' | 'OTHER', status: 'OPEN' | 'IN_DISPUTE' | 'WON' | 'LOST' | 'CANCELED', type: 'CHARGEBACK' | 'PRE_CHARGEBACK' | 'DEBIT_MEMO' | 'REQUEST_FOR_INFORMATION' | 'FRAUD_ALERT', wasRefunded?: (boolean | null) | undefined}; export type CreateApiV2ClaimsArgs = {'api-version': string, 'x-forter-siteid': string, Authorization: string, body: ClaimRequest}; export type CreateApiV2ClaimsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const claimResponse = z.object({message: z.string().max(1000), status: z.string().max(50)}); 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 }; /** * Dispute * * POST /v2/claims */ export const createApiV2Claims = async (args: CreateApiV2ClaimsArgs, options?: CreateApiV2ClaimsOptions) => { const {baseUrl = "https://api.forter-secure.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['api-version'] != null) headers.set("api-version", String(args['api-version'])) if (args['x-forter-siteid'] != null) headers.set("x-forter-siteid", String(args['x-forter-siteid'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/claims${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(claimResponse, await res.json()) };