1- import React , { useState , useEffect } from 'react'
1+ import React , { useState , useEffect , useRef } from 'react'
22import { usePayment } from '../hooks/usePayment'
33
44interface PaymentStatusProps {
@@ -21,10 +21,18 @@ const PaymentStatus: React.FC<PaymentStatusProps> = ({
2121 const [ error , setError ] = useState < string > ( '' )
2222
2323 const { checkPaymentStatus } = usePayment ( )
24+ const timeoutRef = useRef < number | null > ( null )
25+ const pollingAttemptsRef = useRef ( 0 )
2426
2527 useEffect ( ( ) => {
28+ // Reset polling when paymentIntentId changes
29+ setPollingAttempts ( 0 )
30+ setPaymentStatus ( 'pending' )
31+ setError ( '' )
32+ pollingAttemptsRef . current = 0
33+
2634 const pollPaymentStatus = async ( ) => {
27- if ( pollingAttempts >= maxPollingAttempts ) {
35+ if ( pollingAttemptsRef . current >= maxPollingAttempts ) {
2836 setError ( 'Payment confirmation timeout. Please contact support.' )
2937 onPaymentError ( 'Payment confirmation timeout' )
3038 return
@@ -43,9 +51,8 @@ const PaymentStatus: React.FC<PaymentStatusProps> = ({
4351
4452 case 'processing' :
4553 // Continue polling
46- setTimeout ( ( ) => {
47- setPollingAttempts ( prev => prev + 1 )
48- } , pollingInterval )
54+ pollingAttemptsRef . current += 1
55+ timeoutRef . current = setTimeout ( pollPaymentStatus , pollingInterval )
4956 break
5057
5158 case 'requires_payment_method' :
@@ -69,33 +76,40 @@ const PaymentStatus: React.FC<PaymentStatusProps> = ({
6976 onPaymentError ( status . last_payment_error . message || 'Payment failed' )
7077 } else {
7178 // Continue polling for unknown status
72- setTimeout ( ( ) => {
73- setPollingAttempts ( prev => prev + 1 )
74- } , pollingInterval )
79+ pollingAttemptsRef . current += 1
80+ timeoutRef . current = setTimeout ( pollPaymentStatus , pollingInterval )
7581 }
7682 }
7783 } else {
7884 // Continue polling if no status received
79- setTimeout ( ( ) => {
80- setPollingAttempts ( prev => prev + 1 )
81- } , pollingInterval )
85+ pollingAttemptsRef . current += 1
86+ timeoutRef . current = setTimeout ( pollPaymentStatus , pollingInterval )
8287 }
8388 } catch ( err : any ) {
8489 setError ( 'Failed to check payment status' )
8590 onPaymentError ( 'Failed to check payment status' )
8691
8792 // Continue polling on error
88- setTimeout ( ( ) => {
89- setPollingAttempts ( prev => prev + 1 )
90- } , pollingInterval )
93+ pollingAttemptsRef . current += 1
94+ timeoutRef . current = setTimeout ( pollPaymentStatus , pollingInterval )
9195 }
9296 }
9397
9498 // Start polling after initial delay
95- const timeoutId = setTimeout ( pollPaymentStatus , 1000 )
99+ timeoutRef . current = setTimeout ( pollPaymentStatus , 1000 )
96100
97- return ( ) => clearTimeout ( timeoutId )
98- } , [ paymentIntentId , pollingAttempts , maxPollingAttempts , pollingInterval , checkPaymentStatus , onPaymentSuccess , onPaymentError ] )
101+ return ( ) => {
102+ if ( timeoutRef . current ) {
103+ clearTimeout ( timeoutRef . current )
104+ timeoutRef . current = null
105+ }
106+ }
107+ } , [ paymentIntentId , maxPollingAttempts , pollingInterval , checkPaymentStatus , onPaymentSuccess , onPaymentError ] )
108+
109+ // Update polling attempts display
110+ useEffect ( ( ) => {
111+ setPollingAttempts ( pollingAttemptsRef . current )
112+ } , [ pollingAttemptsRef . current ] )
99113
100114 const getStatusMessage = ( ) => {
101115 switch ( paymentStatus ) {
0 commit comments