diff --git a/app/components/auth/PaymentModal.tsx b/app/components/auth/PaymentModal.tsx index 3946878..7a40465 100644 --- a/app/components/auth/PaymentModal.tsx +++ b/app/components/auth/PaymentModal.tsx @@ -24,28 +24,42 @@ interface PaymentResponse { return_url: string; } +interface PaymentStatusResponse { + status: string; + error?: string; +} + export function PaymentModal({ isOpen, onClose, paymentData, onPaymentSuccess }: PaymentModalProps) { const [timeLeft, setTimeLeft] = useState(parseInt(paymentData.expires_in)); const checkPaymentStatus = useCallback(async () => { try { const response = await fetch(`/api/check-payment-status?orderNo=${paymentData.no}`); - const data = await response.json(); + if (!response.ok) { + if (response.status === 404) { + console.error('Order not found'); + return; + } + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json() as PaymentStatusResponse; if (data.status === 'completed') { - clearInterval(timer); onPaymentSuccess(); onClose(); toast.success('支付成功!'); } } catch (error) { console.error('Error checking payment status:', error); + toast.error('检查支付状态时出错,请稍后再试'); } }, [paymentData.no, onPaymentSuccess, onClose]); useEffect(() => { if (!isOpen) return; - const timer = setInterval(() => { + let timer: NodeJS.Timeout; + + const checkAndUpdateStatus = () => { setTimeLeft((prevTime) => { if (prevTime <= 1) { clearInterval(timer); @@ -57,7 +71,9 @@ export function PaymentModal({ isOpen, onClose, paymentData, onPaymentSuccess }: }); checkPaymentStatus(); - }, 3000); // 每3秒检查一次支付状态 + }; + + timer = setInterval(checkAndUpdateStatus, 3000); // 每3秒检查一次支付状态 return () => clearInterval(timer); }, [isOpen, onClose, checkPaymentStatus]); diff --git a/app/components/auth/SubscriptionDialog.tsx b/app/components/auth/SubscriptionDialog.tsx index 00c9f54..6e384aa 100644 --- a/app/components/auth/SubscriptionDialog.tsx +++ b/app/components/auth/SubscriptionDialog.tsx @@ -15,10 +15,19 @@ interface SubscriptionDialogProps { } interface UserSubscription { - plan: SubscriptionPlan; - tokensLeft: number; - nextReloadDate: string; -} + tokenBalance: number; + subscription: { + plan: { + _id: string; + name: string; + tokens: number; + price: number; + description: string; + save_percentage?: number; + }; + expirationDate: string; + } | null; + } interface PaymentResponse { status: string; @@ -164,14 +173,14 @@ export function SubscriptionDialog({ isOpen, onClose }: SubscriptionDialogProps)
- {isAuthenticated && userSubscription && ( + {isAuthenticated && userSubscription && userSubscription.subscription && (