@@ -26,6 +26,9 @@ type PaymentContextProvider = {
2626
2727export const PaymentContext = createContext < PaymentContextProps | undefined > ( undefined ) ;
2828
29+ // Module-level tracking to prevent duplicate payment calls across component remounts
30+ const paymentInitiatedForInstance = new Map < string , boolean > ( ) ;
31+
2932export const PaymentProvider : React . FC < PaymentContextProvider > = ( { children } ) => {
3033 const instanceOwnerPartyId = useNavigationParam ( 'instanceOwnerPartyId' ) ;
3134 const instanceGuid = useNavigationParam ( 'instanceGuid' ) ;
@@ -55,17 +58,29 @@ function PaymentNavigation() {
5558 const paymentInfo = usePaymentInformation ( ) ;
5659 const isPdf = useIsPdf ( ) ;
5760 const { performPayment, skipPayment } = usePayment ( ) ;
61+ const instanceGuid = useNavigationParam ( 'instanceGuid' ) ;
5862
5963 const paymentDoesNotExist = paymentInfo ?. status === PaymentStatus . Uninitialized ;
6064 const isPaymentProcess = useIsPayment ( ) ;
6165
6266 // If when landing on payment task, PaymentStatus is Uninitialized, initiate it by calling the pay action and
6367 // go to payment provider
6468 useEffect ( ( ) => {
65- if ( isPaymentProcess && paymentDoesNotExist && ! isPdf ) {
66- performPayment ( ) ;
69+ if ( isPaymentProcess && paymentDoesNotExist && ! isPdf && instanceGuid ) {
70+ // Check module-level map to prevent duplicate calls across remounts
71+ if ( ! paymentInitiatedForInstance . get ( instanceGuid ) ) {
72+ paymentInitiatedForInstance . set ( instanceGuid , true ) ;
73+ performPayment ( ) ;
74+ }
75+ } else if (
76+ instanceGuid &&
77+ paymentInfo ?. status !== undefined &&
78+ paymentInfo . status !== PaymentStatus . Uninitialized
79+ ) {
80+ // Clean up the flag once payment status is known and no longer Uninitialized
81+ paymentInitiatedForInstance . delete ( instanceGuid ) ;
6782 }
68- } , [ isPaymentProcess , paymentDoesNotExist , performPayment , isPdf ] ) ;
83+ } , [ isPaymentProcess , paymentDoesNotExist , performPayment , isPdf , instanceGuid , paymentInfo ?. status ] ) ;
6984
7085 const paymentCompleted = paymentInfo ?. status === PaymentStatus . Paid || paymentInfo ?. status === PaymentStatus . Skipped ;
7186
0 commit comments