import {z} from 'zod' export type Amount = {amountLocalCurrency?: (string | null) | undefined, amountUSD?: (string | null) | undefined, currency?: (string | null) | undefined}; export type BasicItemData = {category?: (string | null) | undefined, discount?: ({couponCodeUsed: string, couponDiscountAmount?: ({amountLocalCurrency?: (string | null) | undefined, amountUSD?: (string | null) | undefined, currency?: (string | null) | undefined} | null) | undefined, couponDiscountPercent?: (string | null) | undefined} | null) | undefined, name: string, price: Amount, productId?: (string | null) | undefined, quantity: number, type: 'TANGIBLE' | 'NON_TANGIBLE'}; export type CompensationData = {compensationTypeRequested?: ('REPLACEMENT' | 'REFUND' | 'REFUND_UPON_RETURN' | 'CREDIT' | 'MIXED' | 'UNKNOWN' | 'NO_COMPENSATION' | null) | undefined, initiationTime: number, internalReasonCategory?: (string | null) | undefined, reasonCategory?: ('CHANGED_MY_MIND' | 'DOES_NOT_FIT' | 'NOT_AS_DESCRIBED' | 'WRONG_ITEM' | 'ARRIVED_TOO_LATE' | 'LOW_QUALITY' | 'DAMAGED_GOODS' | 'GIFT_RETURN' | 'RETURN_TO_SENDER' | 'RETURN_POLICY_EXCEPTION' | 'OTHER' | 'UNKNOWN' | 'ITEM_NOT_RECEIVED' | 'ENTIRE_ORDER_NOT_RECEIVED' | 'OUTBOUND_ISSUE_CARRIER' | 'OUTBOUND_ISSUE_MERCHANT' | 'FAILED_DELIVERY' | 'INBOUND_LOSS' | 'INBOUND_ISSUE_CARRIER' | null) | undefined, requestedReturnType: 'SHIP_TO_WAREHOUSE' | 'IN_STORE' | 'NO_RETURN'}; export type CompensationItem = {basicItemData: BasicItemData, itemCompensationData: CompensationData}; export type CompensationRequest = {initiationType: 'CALL_CENTER' | 'IN_STORE' | 'CHAT' | 'ONLINE' | 'SELF_SERVICE', items: Array, orderCompensationData?: ({compensationTypeRequested?: ('REPLACEMENT' | 'REFUND' | 'REFUND_UPON_RETURN' | 'CREDIT' | 'MIXED' | 'UNKNOWN' | 'NO_COMPENSATION' | null) | undefined, initiationTime: number, internalReasonCategory?: (string | null) | undefined, reasonCategory?: ('CHANGED_MY_MIND' | 'DOES_NOT_FIT' | 'NOT_AS_DESCRIBED' | 'WRONG_ITEM' | 'ARRIVED_TOO_LATE' | 'LOW_QUALITY' | 'DAMAGED_GOODS' | 'GIFT_RETURN' | 'RETURN_TO_SENDER' | 'RETURN_POLICY_EXCEPTION' | 'OTHER' | 'UNKNOWN' | 'ITEM_NOT_RECEIVED' | 'ENTIRE_ORDER_NOT_RECEIVED' | 'OUTBOUND_ISSUE_CARRIER' | 'OUTBOUND_ISSUE_MERCHANT' | 'FAILED_DELIVERY' | 'INBOUND_LOSS' | 'INBOUND_ISSUE_CARRIER' | null) | undefined, requestedReturnType: 'SHIP_TO_WAREHOUSE' | 'IN_STORE' | 'NO_RETURN'} | null) | undefined, totalRequestedAmount: Amount}; export type ConnectionInformation = {checkoutToolsSessionId?: (string | null) | undefined, customerIP: string, forterMobileUID?: (string | null) | undefined, forterTokenCookie?: (string | null) | undefined, merchantDeviceIdentifier?: (string | null) | undefined, merchantProvidedSessionId?: (string | null) | undefined, userAgent: string}; export type ItemCompensationStatusData = {compensationTypeGranted: 'REPLACEMENT' | 'REFUND' | 'REFUND_UPON_RETURN' | 'CREDIT' | 'MIXED' | 'UNKNOWN' | 'NO_COMPENSATION', internalReasonCategory?: (string | null) | undefined, reasonCategory: 'CHANGED_MY_MIND' | 'DOES_NOT_FIT' | 'NOT_AS_DESCRIBED' | 'WRONG_ITEM' | 'ARRIVED_TOO_LATE' | 'LOW_QUALITY' | 'DAMAGED_GOODS' | 'GIFT_RETURN' | 'RETURN_TO_SENDER' | 'RETURN_POLICY_EXCEPTION' | 'OTHER' | 'UNKNOWN' | 'ITEM_NOT_RECEIVED' | 'ENTIRE_ORDER_NOT_RECEIVED' | 'OUTBOUND_ISSUE_CARRIER' | 'OUTBOUND_ISSUE_MERCHANT' | 'FAILED_DELIVERY' | 'INBOUND_LOSS' | 'INBOUND_ISSUE_CARRIER', returnCondition?: ('GOOD_CONDITION' | 'DAMAGED' | 'WRONG_ITEM' | 'NO_ITEM' | 'NOT_ARRIVED' | 'NO_RETURN' | null) | undefined, returnMethodGranted: 'SHIP_TO_WAREHOUSE' | 'IN_STORE' | 'NO_RETURN', statusLog?: ({arrivedToWarehouseTime?: (number | null) | undefined, initiationTime?: (number | null) | undefined, initiationType?: ('CALL_CENTER' | 'IN_STORE' | 'CHAT' | 'ONLINE' | 'SELF_SERVICE' | null) | undefined, shippedByCustomerTime?: (number | null) | undefined} | null) | undefined, updatedStatus: 'ACCEPTED_BY_MERCHANT' | 'REJECTED_BY_MERCHANT' | 'WITHDRAWN_BY_CUSTOMER'}; export type ItemCompensationStatus = {basicItemData: BasicItemData, statusData: ItemCompensationStatusData}; export type UnifiedCompensationRequestRequest = {accountId: string, additionalEventIdentifiers?: (Record | null) | undefined, additionalInformation?: ({[key: string]: unknown} | null) | undefined, compensationRequest: CompensationRequest, connectionInformation: ConnectionInformation, eventId: string, eventTime: number, hasProofOfPurchase?: (boolean | null) | undefined, isGuestAccount: boolean, merchantAgentData?: ({merchantAgentId?: (string | null) | undefined, merchantAgentName?: (string | null) | undefined} | null) | undefined, merchantIdentifiers?: ({merchantDomain?: (string | null) | undefined, merchantName?: (string | null) | undefined} | null) | undefined, originalOrderId: string, status?: ({hasProofOfPurchase?: (boolean | null) | undefined, itemStatus: Array, merchantAgentData?: ({merchantAgentId?: (string | null) | undefined, merchantAgentName?: (string | null) | undefined} | null) | undefined, replacementOrderId?: (string | null) | undefined, shippingRefunded?: (boolean | null) | undefined, shippingRefundedAmount?: ({amountLocalCurrency?: (string | null) | undefined, amountUSD?: (string | null) | undefined, currency?: (string | null) | undefined} | null) | undefined, statusData?: ({compensationTypeGranted: 'REPLACEMENT' | 'REFUND' | 'REFUND_UPON_RETURN' | 'CREDIT' | 'MIXED' | 'UNKNOWN' | 'NO_COMPENSATION', internalReasonCategory?: (string | null) | undefined, reasonCategory: 'CHANGED_MY_MIND' | 'DOES_NOT_FIT' | 'NOT_AS_DESCRIBED' | 'WRONG_ITEM' | 'ARRIVED_TOO_LATE' | 'LOW_QUALITY' | 'DAMAGED_GOODS' | 'GIFT_RETURN' | 'RETURN_TO_SENDER' | 'RETURN_POLICY_EXCEPTION' | 'OTHER' | 'UNKNOWN' | 'ITEM_NOT_RECEIVED' | 'ENTIRE_ORDER_NOT_RECEIVED' | 'OUTBOUND_ISSUE_CARRIER' | 'OUTBOUND_ISSUE_MERCHANT' | 'FAILED_DELIVERY' | 'INBOUND_LOSS' | 'INBOUND_ISSUE_CARRIER', returnCondition?: ('GOOD_CONDITION' | 'DAMAGED' | 'WRONG_ITEM' | 'NO_ITEM' | 'NOT_ARRIVED' | 'NO_RETURN' | null) | undefined, returnMethodGranted: 'SHIP_TO_WAREHOUSE' | 'IN_STORE' | 'NO_RETURN', statusLog?: ({arrivedToWarehouseTime?: (number | null) | undefined, initiationTime?: (number | null) | undefined, initiationType?: ('CALL_CENTER' | 'IN_STORE' | 'CHAT' | 'ONLINE' | 'SELF_SERVICE' | null) | undefined, shippedByCustomerTime?: (number | null) | undefined} | null) | undefined, updatedStatus: 'ACCEPTED_BY_MERCHANT' | 'REJECTED_BY_MERCHANT' | 'WITHDRAWN_BY_CUSTOMER'} | null) | undefined, totalGrantedAmount: Amount} | null) | undefined}; export type CreateApiV2UnifiedCompensationRequestIdArgs = {id: string, 'api-version': string, 'x-forter-siteid': string, Authorization: string, body: UnifiedCompensationRequestRequest}; export type CreateApiV2UnifiedCompensationRequestIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const unifiedCompensationRequestResponse = z.object({correlationId: z.string(), decisionReason: z.string().max(1000), forterDecision: z.string().max(1000), merchantPolicyId: z.string(), recommendations: z.array(z.string()), 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 }; /** * Compensation request * * POST /v2/unified-compensation-request/{id} */ export const createApiV2UnifiedCompensationRequestId = async (args: CreateApiV2UnifiedCompensationRequestIdArgs, options?: CreateApiV2UnifiedCompensationRequestIdOptions) => { 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/unified-compensation-request/${encodeURIComponent(String(args.id))}${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(unifiedCompensationRequestResponse, await res.json()) };