Skip to content

Commit 00d8151

Browse files
authored
hotfix for the double payment action call. (#3795)
* hotfix for the double payemnt action call. THIS IS NOT FINAL, needs to be fixed properly * stricter cleanup
1 parent 51897ba commit 00d8151

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/features/payment/PaymentProvider.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type PaymentContextProvider = {
2626

2727
export 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+
2932
export 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

Comments
 (0)