PaymentConfirmCard.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use client';
  2. import { useEffect, useState, useRef } from 'react';
  3. import { useSearchParams } from 'next/navigation';
  4. import { useRouter } from '@/lib/i18n/navigation';
  5. import { Loader2, ArrowLeft } from 'lucide-react';
  6. import { paymentService } from '@/lib/services';
  7. import { useLanguage } from '@/lib/i18n/LanguageContext';
  8. export function PaymentConfirmFallback() {
  9. const { t } = useLanguage();
  10. return (
  11. <div className="bg-white border-2 border-black shadow-[6px_6px_0px_#000] w-full max-w-md p-10 flex flex-col items-center justify-center font-mono">
  12. <Loader2 className="w-8 h-8 text-black animate-spin mb-3" />
  13. <p className="text-black text-xs font-bold uppercase">[ {t('payment_confirm.verifying')} ]</p>
  14. </div>
  15. );
  16. }
  17. export default function PaymentConfirmCard() {
  18. const searchParams = useSearchParams();
  19. const router = useRouter();
  20. const { t } = useLanguage();
  21. const hasFetched = useRef(false);
  22. const [status, setStatus] = useState<'loading' | 'success' | 'error'>('loading');
  23. const [message, setMessage] = useState(() => t('payment_confirm.verifying'));
  24. const [paymentId, setPaymentId] = useState<string>('');
  25. useEffect(() => {
  26. const pid = searchParams.get('payment_id');
  27. const token = searchParams.get('token');
  28. if (!pid || !token) {
  29. setStatus('error');
  30. setMessage(t('payment_confirm.param_missing'));
  31. return;
  32. }
  33. setPaymentId(pid);
  34. const confirmPayment = async () => {
  35. if (hasFetched.current) return;
  36. hasFetched.current = true;
  37. try {
  38. await paymentService.checkConfirm(Number(pid));
  39. setStatus('success');
  40. setMessage(t('payment_confirm.success_msg'));
  41. } catch (error: any) {
  42. console.error(error);
  43. setStatus('error');
  44. const errorMsg = error.response?.data?.message || error.message || t('payment_confirm.unknown_error');
  45. setMessage(`${t('payment_confirm.failed_prefix')}${errorMsg}`);
  46. }
  47. };
  48. confirmPayment();
  49. }, [searchParams, t]);
  50. return (
  51. <div className="bg-white border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-md overflow-hidden font-mono">
  52. {/* 顶部状态条 */}
  53. <div className="bg-black text-white px-5 py-3 flex items-center justify-between border-b-2 border-black text-xs font-bold select-none">
  54. <div className="flex items-center gap-2">
  55. <span className={`w-2.5 h-2.5 inline-block border border-black ${
  56. status === 'loading' ? 'bg-amber-300 animate-pulse' : status === 'success' ? 'bg-amber-300' : 'bg-rose-500'
  57. }`} />
  58. <span>[ {t('payment.confirm_info')} ]</span>
  59. </div>
  60. <span className="text-neutral-400">#PAY-{paymentId || '0000'}</span>
  61. </div>
  62. <div className="p-6 sm:p-8 text-center space-y-5">
  63. {/* 状态徽标 */}
  64. <div className="flex justify-center">
  65. {status === 'loading' && (
  66. <div className="w-16 h-16 bg-neutral-100 border-2 border-black shadow-[3px_3px_0px_#000] flex items-center justify-center">
  67. <Loader2 className="w-8 h-8 text-black animate-spin" />
  68. </div>
  69. )}
  70. {status === 'success' && (
  71. <div className="w-16 h-16 bg-amber-300 border-2 border-black shadow-[3px_3px_0px_#000] flex items-center justify-center font-black text-2xl text-black">
  72. ✓
  73. </div>
  74. )}
  75. {status === 'error' && (
  76. <div className="w-16 h-16 bg-rose-100 border-2 border-black shadow-[3px_3px_0px_#000] flex items-center justify-center font-black text-2xl text-rose-600">
  77. ✕
  78. </div>
  79. )}
  80. </div>
  81. {/* 标题与描述 */}
  82. <div>
  83. <h1 className="text-lg font-black text-black uppercase">
  84. {status === 'loading'
  85. ? `[ ${t('payment_confirm.badge_verifying')} ]`
  86. : status === 'success'
  87. ? `[ ${t('payment_confirm.badge_success')} ]`
  88. : `[ ${t('payment_confirm.badge_failed')} ]`}
  89. </h1>
  90. <p className="text-xs text-neutral-600 font-sans mt-2 leading-relaxed">
  91. {message || t('payment_confirm.verifying')}
  92. </p>
  93. </div>
  94. {/* 返回控制台按钮 */}
  95. <div className="pt-4 border-t-2 border-dashed border-neutral-300">
  96. <button
  97. onClick={() => router.push('/dashboard')}
  98. className="w-full py-3 bg-neutral-100 hover:bg-amber-300 text-black border-2 border-black font-black text-xs shadow-[3px_3px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 active:shadow-none flex items-center justify-center gap-2 uppercase"
  99. >
  100. <ArrowLeft size={14} />
  101. <span>[ {t('payment_confirm.back_to_dashboard')} ]</span>
  102. </button>
  103. </div>
  104. </div>
  105. </div>
  106. );
  107. }