import React, { useEffect } from 'react'; import { useSearchParams, useNavigate, Link } from 'react-router-dom'; import { useQuery } from '@tanstack/react-query'; import { CheckBadgeIcon, SparklesIcon, CalendarDaysIcon, CreditCardIcon, ArrowLeftIcon } from '@heroicons/react/24/outline'; import { toast } from 'sonner'; import { api } from '../lib/api'; import type { ApiResponse } from '../lib/api'; import type { MySubscriptionData } from '../types'; import { formatRial, formatDate } from '../lib/utils'; import { PLAN_FEATURE_LABELS, planMetaOf } from './SubscriptionPage'; interface PaymentDetails { uuid: string; order_id: string; amount_rials: number; status: string; gateway: string; reference_id: string | null; created_at: number; } /** * PaymentSuccessPage — landing shown when the payment gateway redirects back * after a subscription purchase (frontend_address + ?payment_uuid&status). * On success it shows the transaction receipt and the newly-active plan; any * non-success status redirects back to the plans page with an error toast. */ export default function PaymentSuccessPage() { const [params] = useSearchParams(); const navigate = useNavigate(); const paymentUuid = params.get('payment_uuid') ?? ''; const status = params.get('status') ?? ''; const isSuccess = status === 'success'; useEffect(() => { if (!isSuccess) { toast.error('پرداخت انجام نشد یا لغو شد'); navigate('/admin/subscription', { replace: true }); } }, [isSuccess, navigate]); const { data: payData, isLoading: payLoading } = useQuery>({ queryKey: ['payment', paymentUuid], queryFn: () => api.get(`/api/v1/payment/${paymentUuid}`), enabled: isSuccess && paymentUuid !== '', }); const { data: myData } = useQuery>({ queryKey: ['subscription-my'], queryFn: () => api.get('/api/v1/subscription/my'), enabled: isSuccess, }); if (!isSuccess) return null; const payment = payData?.data ?? null; const sub = myData?.data?.subscription ?? null; const meta = sub ? planMetaOf(sub.plan.name) : null; const months = sub?.period?.duration_months ?? 0; return (
{/* ── Success header ── */}

پرداخت شما با موفقیت انجام شد!

{/* ── Transaction receipt ── */}
{/* ── Purchased plan ── */}

پلن خریداری شده شما:

{sub && meta && (
پلن {meta.label}
{meta.desc}
{payment && months > 0 && ( هزینه ماهیانه: {formatRial(Math.round(payment.amount_rials / months))} )} {sub.expires_at && ( تاریخ انقضا: {formatDate(sub.expires_at)} )}
امکانات
    {Object.entries(sub.plan.features).filter(([, v]) => v).map(([key]) => (
  • {PLAN_FEATURE_LABELS[key] ?? key}
  • ))}
)} {/* ── Back ── */}
صفحه اشتراک‌ها
); } function Receipt({ label, value, loading, center, left }: { label: string; value: string; loading?: boolean; center?: boolean; left?: boolean }) { return (
{label}
{loading ?
:
{value}
}
); }