updates
This commit is contained in:
@@ -22,19 +22,19 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
}) => {
|
||||
const [stripePromise, setStripePromise] = useState<Promise<any> | null>(null);
|
||||
const [clientSecret, setClientSecret] = useState<string | null>(null);
|
||||
const [publishableKey, setPublishableKey] = useState<string | null>(null);
|
||||
const [, setPublishableKey] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [paymentCompleted, setPaymentCompleted] = useState(false);
|
||||
|
||||
// Initialize Stripe payment intent
|
||||
|
||||
useEffect(() => {
|
||||
// Don't initialize if payment is already completed
|
||||
|
||||
if (paymentCompleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
// First, create payment intent to get publishable key
|
||||
|
||||
const initializeStripe = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
@@ -62,12 +62,12 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
setPublishableKey(publishable_key);
|
||||
setClientSecret(client_secret);
|
||||
|
||||
// Initialize Stripe with publishable key
|
||||
// loadStripe returns a Promise, so we don't need to wrap it
|
||||
|
||||
|
||||
const stripePromise = loadStripe(publishable_key);
|
||||
setStripePromise(stripePromise);
|
||||
|
||||
// Wait for Stripe to load before proceeding
|
||||
|
||||
const stripe = await stripePromise;
|
||||
if (!stripe) {
|
||||
throw new Error('Failed to load Stripe');
|
||||
@@ -90,7 +90,7 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
initializeStripe();
|
||||
}, [bookingId, amount, currency, onError, paymentCompleted]);
|
||||
|
||||
// Debug logging - must be before any conditional returns
|
||||
|
||||
useEffect(() => {
|
||||
if (clientSecret && stripePromise) {
|
||||
console.log('Stripe initialized successfully', { hasClientSecret: !!clientSecret, hasStripePromise: !!stripePromise });
|
||||
@@ -101,7 +101,7 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
|
||||
const handlePaymentSuccess = async (paymentIntentId: string) => {
|
||||
try {
|
||||
// Mark payment as completed to prevent re-initialization
|
||||
|
||||
setPaymentCompleted(true);
|
||||
|
||||
const response = await confirmStripePayment(paymentIntentId, bookingId);
|
||||
@@ -109,13 +109,13 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
if (response.success) {
|
||||
onSuccess();
|
||||
} else {
|
||||
// Reset payment completed flag if confirmation failed
|
||||
|
||||
setPaymentCompleted(false);
|
||||
throw new Error(response.message || 'Payment confirmation failed');
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error('Error confirming payment:', err);
|
||||
// Reset payment completed flag on error
|
||||
|
||||
setPaymentCompleted(false);
|
||||
const errorMessage = err.response?.data?.message || err.message || 'Payment confirmation failed';
|
||||
setError(errorMessage);
|
||||
@@ -132,9 +132,9 @@ const StripePaymentWrapper: React.FC<StripePaymentWrapperProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
// Don't show error if payment is completed
|
||||
|
||||
if (paymentCompleted) {
|
||||
return null; // Component will be unmounted by parent
|
||||
return null;
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
|
||||
Reference in New Issue
Block a user