@@ -24,23 +24,12 @@ type PaymentContextProvider = {
24
24
25
25
export const PaymentContext = createContext < PaymentContextProps | undefined > ( undefined ) ;
26
26
27
- const EmptyPaymentProvider : React . FC < PaymentContextProvider > = ( { children } ) => {
28
- const contextValue = {
29
- setLoading : ( ) => { } ,
30
- performPayment : ( ) => { } ,
31
- paymentError : null ,
32
- } ;
33
- return < PaymentContext . Provider value = { contextValue } > { children } </ PaymentContext . Provider > ;
34
- } ;
35
-
36
- const FunctionalPaymentProvider : React . FC < PaymentContextProvider > = ( { children } ) => {
27
+ export const PaymentProvider : React . FC < PaymentContextProvider > = ( { children } ) => {
37
28
const [ loading , setLoading ] = useState < boolean > ( false ) ;
38
29
const partyId = useNavigationParam ( 'partyId' ) ;
39
30
const instanceGuid = useNavigationParam ( 'instanceGuid' ) ;
40
- const paymentInfo = usePaymentInformation ( ) ;
41
- const isPdf = useIsPdf ( ) ;
42
- const { next, busy } = useProcessNavigation ( ) || { } ;
43
31
const { mutate, error } = usePerformPayActionMutation ( partyId , instanceGuid ) ;
32
+ const isSubformPage = useIsSubformPage ( ) ;
44
33
45
34
const contextValue : PaymentContextProps = useMemo (
46
35
( ) => ( {
@@ -54,6 +43,31 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
54
43
[ setLoading , mutate , error ] ,
55
44
) ;
56
45
46
+ // If payment failed, stop loading
47
+ React . useEffect ( ( ) => {
48
+ if ( error ) {
49
+ setLoading ( false ) ;
50
+ }
51
+ } , [ error ] ) ;
52
+
53
+ if ( loading ) {
54
+ return < Loader reason = 'Navigating to external payment solution' /> ;
55
+ }
56
+
57
+ return (
58
+ < PaymentContext . Provider value = { contextValue } >
59
+ { children }
60
+ { ! isSubformPage && ! error && < PaymentNavigation /> }
61
+ </ PaymentContext . Provider >
62
+ ) ;
63
+ } ;
64
+
65
+ function PaymentNavigation ( ) {
66
+ const { next, busy } = useProcessNavigation ( ) || { } ;
67
+ const paymentInfo = usePaymentInformation ( ) ;
68
+ const isPdf = useIsPdf ( ) ;
69
+ const { setLoading, performPayment } = usePayment ( ) ;
70
+
57
71
const paymentDoesNotExist = paymentInfo ?. status === PaymentStatus . Uninitialized ;
58
72
const isPaymentProcess = useIsPayment ( ) ;
59
73
const actionCalled = useRef ( false ) ;
@@ -64,9 +78,9 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
64
78
if ( isPaymentProcess && paymentDoesNotExist && ! actionCalled . current && ! isPdf ) {
65
79
actionCalled . current = true ;
66
80
setLoading ( true ) ;
67
- mutate ( ) ;
81
+ performPayment ( ) ;
68
82
}
69
- } , [ isPaymentProcess , paymentDoesNotExist , mutate , isPdf ] ) ;
83
+ } , [ isPaymentProcess , paymentDoesNotExist , performPayment , setLoading , isPdf ] ) ;
70
84
71
85
const paymentCompleted = paymentInfo ?. status === PaymentStatus . Paid || paymentInfo ?. status === PaymentStatus . Skipped ;
72
86
const nextCalled = useRef ( false ) ;
@@ -78,29 +92,10 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
78
92
setLoading ( true ) ;
79
93
next ( { action : 'confirm' , nodeId : 'next-button' } ) ;
80
94
}
81
- } , [ paymentCompleted , next , busy , isPdf ] ) ;
82
-
83
- useEffect ( ( ) => {
84
- if ( error ) {
85
- setLoading ( false ) ;
86
- }
87
- } , [ error ] ) ;
88
-
89
- if ( loading ) {
90
- return < Loader reason = 'Navigating to external payment solution' /> ;
91
- }
92
-
93
- return < PaymentContext . Provider value = { contextValue } > { children } </ PaymentContext . Provider > ;
94
- } ;
95
+ } , [ paymentCompleted , setLoading , next , busy , isPdf ] ) ;
95
96
96
- export const PaymentProvider : React . FC < PaymentContextProvider > = ( { children } ) => {
97
- const isSubformPage = useIsSubformPage ( ) ;
98
- if ( isSubformPage ) {
99
- return < EmptyPaymentProvider > { children } </ EmptyPaymentProvider > ;
100
- }
101
-
102
- return < FunctionalPaymentProvider > { children } </ FunctionalPaymentProvider > ;
103
- } ;
97
+ return null ;
98
+ }
104
99
105
100
export const usePayment = ( ) => {
106
101
const context = useContext ( PaymentContext ) ;
0 commit comments