11import { type FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
22
3+ export enum PaymentErrorTypes {
4+ PaymentExpired = 'PaymentExpired' ,
5+ PaymentIncomplete = 'PaymentIncomplete'
6+ }
7+ export class PrePaymentError extends Error {
8+ public readonly component : FormComponent
9+ public readonly userMessage : string
10+
11+ /**
12+ * Whether to reset the component state and redirect to the component's page.
13+ * - `true`: Reset state and redirect (e.g., payment expired - user must re-enter)
14+ * - `false`: Keep state and stay on current page with error (e.g., capture failed - user can retry)
15+ */
16+ public readonly shouldResetState : boolean
17+
18+ /**
19+ * When supplied, an "Important" notification banner will be shown based on the value.
20+ */
21+ public readonly errorType : PaymentErrorTypes | undefined
22+
23+ constructor (
24+ component : FormComponent ,
25+ userMessage : string ,
26+ shouldResetState : boolean ,
27+ errorType ?: PaymentErrorTypes
28+ ) {
29+ super ( 'Payment capture failed' )
30+ this . name = 'PrePaymentError'
31+ this . component = component
32+ this . userMessage = userMessage
33+ this . shouldResetState = shouldResetState
34+ this . errorType = errorType
35+ }
36+
37+ getStateKeys ( ) {
38+ const extraStateKeys =
39+ this . component . page ?. getStateKeys ( this . component ) ?? [ ]
40+
41+ return [ this . component . name ] . concat ( extraStateKeys )
42+ }
43+ }
44+
345/**
446 * Thrown when form submission fails after payment has been captured.
547 * User needs to retry or contact support for a refund.
@@ -16,23 +58,6 @@ export class PostPaymentSubmissionError extends Error {
1658 }
1759}
1860
19- export interface InvalidComponentStateErrorOptions {
20- /**
21- * Whether to reset the component state and redirect to the component's page.
22- * - `true`: Reset state and redirect (e.g., payment expired - user must re-enter)
23- * - `false`: Keep state and stay on current page with error (e.g., capture failed - user can retry)
24- * @default true
25- */
26- shouldResetState ?: boolean
27-
28- /**
29- * Whether this error is due to payment expiry.
30- * When true, an "Important" notification banner will be shown on the payment page.
31- * @default false
32- */
33- isPaymentExpired ?: boolean
34- }
35-
3661/**
3762 * Thrown when a component has an invalid state. This is typically only required where state needs
3863 * to be checked against an external source upon submission of a form. For example: file upload
@@ -44,21 +69,13 @@ export interface InvalidComponentStateErrorOptions {
4469export class InvalidComponentStateError extends Error {
4570 public readonly component : FormComponent
4671 public readonly userMessage : string
47- public readonly shouldResetState : boolean
48- public readonly isPaymentExpired : boolean
4972
50- constructor (
51- component : FormComponent ,
52- userMessage : string ,
53- options : InvalidComponentStateErrorOptions = { }
54- ) {
73+ constructor ( component : FormComponent , userMessage : string ) {
5574 const message = `Invalid component state for: ${ component . name } `
5675 super ( message )
5776 this . name = 'InvalidComponentStateError'
5877 this . component = component
5978 this . userMessage = userMessage
60- this . shouldResetState = options . shouldResetState ?? true
61- this . isPaymentExpired = options . isPaymentExpired ?? false
6279 }
6380
6481 getStateKeys ( ) {
0 commit comments