# All files / src/pages PaymentResultPage.tsx

0% Statements 0/45
0% Branches 0/26
0% Functions 0/15
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168

     import { useEffect, useState } from 'react';
    import { useNavigate, useSearchParams } from 'react-router-dom';
    import { useAuth } from '../hooks/useAuth';
    import { subscriptionApi } from '../api/subscription';
    import { CheckCircle, XCircle, Loader2, ArrowRight, AlertCircle } from 'lucide-react';

    import { useTranslation } from 'react-i18next';

    export { PaymentResultPage };

    function PaymentResultPage() {
      const navigate = useNavigate();
      const [searchParams] = useSearchParams();
      const { refreshUser } = useAuth();
      const { t } = useTranslation();
      const [loading, setLoading] = useState(true);
      const [status, setStatus] = useState<'success' | 'cancel' | 'error'>('success');
      const [message, setMessage] = useState('');

      useEffect(() => {
        const cancelled = searchParams.get('canceled');
        if (cancelled === 'true') {
          setStatus('cancel');
          setLoading(false);
          return;
        }

        const sessionId = searchParams.get('session_id');
        if (!sessionId) {
          // session_id 缺失时，直接查询用户当前订阅状态
          subscriptionApi.getStatus()
            .then((res) => {
              if (res.data && res.data.status === 'active') {
                setStatus('success');
                setMessage(t('payment.success.description'));
              } else if (res.data && res.data.plan === 'FREE') {
                setStatus('error');
                setMessage(t('payment.error.description'));
              }
            })
            .catch(() => {
              setStatus('error');
              setMessage(t('payment.error.action'));
            })
            .finally(() => {
              setLoading(false);
            });
          return;
        }

        // 主动调用后端接口验证支付状态
        subscriptionApi.verify(sessionId)
          .then((res) => {
            if (res.data.paid) {
              setStatus('success');
              setMessage(res.data.message);
              // 刷新用户信息以获取最新的订阅状态
              refreshUser().catch(() => {});
            } else {
              setStatus('error');
              setMessage(res.data.message || t('payment.pending.title'));
            }
          })
          .catch(() => {
            setStatus('error');
            setMessage(t('payment.error.action'));
          })
          .finally(() => {
            setLoading(false);
          });
      }, []);

      if (loading) {
        return (
          <div className="min-h-[calc(100vh-3.5rem)] flex items-center justify-center">
            <div className="text-center">
              <Loader2 className="w-8 h-8 text-accent animate-spin mx-auto mb-4" />
              <p className="text-[15px] text-text-secondary">{t('payment.pending.verifying')}</p>
            </div>
          </div>
        );
      }

      return (
        <div className="min-h-[calc(100vh-3.5rem)] flex items-center justify-center bg-gradient-to-b from-surface to-surface-secondary">
          <div className="text-center max-w-md px-6">
            {status === 'success' && (
              <>
                <div className="w-16 h-16 rounded-full bg-green/10 flex items-center justify-center mx-auto mb-6">
                  <CheckCircle className="w-10 h-10 text-green" />
                </div>
                <h2 className="text-hero font-bold text-text-primary mb-3">{t('payment.success.title')}</h2>
                <p className="text-body text-text-secondary mb-8">
                  {message || t('payment.success.description')}
                </p>
                <div className="flex gap-3 justify-center">
                  <button
                    onClick={() => navigate('/user/subscription')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-white bg-accent rounded-button hover:bg-accent-hover transition-colors"
                  >
                    {t('payment.success.viewSubscription')} <ArrowRight className="w-4 h-4" />
                  </button>
                  <button
                    onClick={() => navigate('/')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-text-primary border border-border rounded-button hover:bg-surface-secondary transition-colors"
                  >
                    {t('payment.success.backToHome')}
                  </button>
                </div>
              </>
            )}

            {status === 'cancel' && (
              <>
                <div className="w-16 h-16 rounded-full bg-yellow/10 flex items-center justify-center mx-auto mb-6">
                  <XCircle className="w-10 h-10 text-yellow" />
                </div>
                <h2 className="text-hero font-bold text-text-primary mb-3">{t('payment.cancelled.title')}</h2>
                <p className="text-body text-text-secondary mb-8">
                  {t('payment.cancelled.description')}
                </p>
                <div className="flex gap-3 justify-center">
                  <button
                    onClick={() => navigate('/pricing')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-white bg-accent rounded-button hover:bg-accent-hover transition-colors"
                  >
                    {t('payment.cancelled.reselect')}
                  </button>
                  <button
                    onClick={() => navigate('/')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-text-primary border border-border rounded-button hover:bg-surface-secondary transition-colors"
                  >
                    {t('payment.success.backToHome')}
                  </button>
                </div>
              </>
            )}

            {status === 'error' && (
              <>
                <div className="w-16 h-16 rounded-full bg-red/10 flex items-center justify-center mx-auto mb-6">
                  <AlertCircle className="w-10 h-10 text-red" />
                </div>
                <h2 className="text-hero font-bold text-text-primary mb-3">{t('payment.error.title')}</h2>
                <p className="text-body text-text-secondary mb-8">
                  {message}
                </p>
                <div className="flex gap-3 justify-center">
                  <button
                    onClick={() => navigate('/pricing')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-white bg-accent rounded-button hover:bg-accent-hover transition-colors"
                  >
                    {t('payment.pending.retry')}
                  </button>
                  <button
                    onClick={() => navigate('/user/subscription')}
                    className="inline-flex items-center gap-2 px-6 py-2.5 text-[14px] font-medium text-text-primary border border-border rounded-button hover:bg-surface-secondary transition-colors"
                  >
                    {t('payment.success.viewSubscription')}
                  </button>
                </div>
              </>
            )}
          </div>
        </div>
      );
    }