import "server-only"; import type { NormalizedPaymentEvent, OrderStatus, PaymentCheckoutResult, PaymentSnapshot, } from "@salixcommerce/core"; export type PaymentFailureCode = | "IDEMPOTENCY_CONFLICT" | "ILLEGAL_PAYMENT_TRANSITION" | "INVALID_REQUEST" | "ORDER_NOT_FOUND" | "PAYMENT_ALREADY_PENDING" | "PAYMENT_AMOUNT_MISMATCH" | "PAYMENT_EVENT_CONFLICT" | "PAYMENT_NOT_FOUND" | "PAYMENTS_UNAVAILABLE" | "PROVIDER_FAILURE" | "REFUND_EXCEEDS_PAYMENT"; export interface PaymentFailure { readonly code: PaymentFailureCode; readonly message: string; readonly status: 400 | 404 | 409 | 500 | 503; } export type PaymentResult = { readonly data: T; readonly ok: true } | { readonly error: PaymentFailure; readonly ok: false }; export interface PaymentEventResult { readonly eventResult: "applied" | "ignored"; readonly orderReference: string; readonly orderStatus: OrderStatus; readonly payment: PaymentSnapshot; readonly replayed: boolean; } export interface PaymentRepository { applyEvent( input: Readonly<{ event: NormalizedPaymentEvent; eventHash: string }>, ): Promise>; loadAuthorizedPayment( capabilityHash: string, provider: string, providerPaymentReference: string, ): Promise; loadLatestPayment( capabilityHash: string, orderReference: string, ): Promise; recordCheckout( input: Readonly<{ capabilityHash: string; checkout: PaymentCheckoutResult; idempotencyKey: string; requestHash: string; }>, ): Promise>; } export function paymentFailure( code: PaymentFailureCode, message: string, status: PaymentFailure["status"], ): PaymentFailure { return Object.freeze({ code, message, status }); }