"use client"; import { useEffect, useState } from "react"; import { useParams, useRouter, useSearchParams } from "next/navigation"; import { request } from "@/services/response"; import { formatToman } from "@/lib/money"; import Cookies from "js-cookie"; import Layout from "@/components/layout"; export default function PaymentDetailsPage() { const params = useParams(); const router = useRouter(); const searchParams = useSearchParams(); const [payment, setPayment] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [paying, setPaying] = useState(false); const handleRetryPayment = () => { if (!payment?.appointment_uuid) return; setPaying(true); // بدون XHR: ریدایرکت خالص به entry واحد پرداخت در بکاند. const apiBase = process.env.NEXT_PUBLIC_API_URL; const gateway = payment.gateway || "mellat"; const ret = encodeURIComponent(`${window.location.origin}/payment/result`); window.location.href = `${apiBase}/api/v1/payment/order/${payment.appointment_uuid}` + `?gateway=${encodeURIComponent(gateway)}&return=${ret}`; }; useEffect(() => { // بررسی لاگین بودن کاربر const userInfo = Cookies.get("userInfo"); if (!userInfo) { router.push("/login"); return; } const fetchPaymentDetails = async () => { try { setLoading(true); const response = await request.getPayment(params.uuid); // سازگار با پاسخ flat (جدید) و double-nested (قدیمی) setPayment(response?.data?.data ?? response?.data ?? null); } catch (err) { setError("خطا در دریافت اطلاعات پرداخت"); console.error("Payment fetch error:", err); } finally { setLoading(false); } }; if (params.uuid) { fetchPaymentDetails(); } }, [params.uuid, router]); // پس از دیدن جزئیاتِ پرداختِ موفق، خودکار به تاریخچهٔ نوبتها (تب تأییدشده) برو. useEffect(() => { const isSuccess = searchParams.get("status") === "success" || payment?.status === "success"; if (isSuccess) { const t = setTimeout(() => router.push("/dashboard?sidebar=1"), 4000); return () => clearTimeout(t); } }, [payment, searchParams, router]); const getStatusLabel = (status) => { const statusMap = { pending: "در انتظار پرداخت", success: "پرداخت موفق", failed: "پرداخت ناموفق", canceled: "پرداخت لغو شد", refunded: "مسترد شده", }; return statusMap[status] || status; }; const getStatusColor = (status) => { const colorMap = { pending: "text-yellow-600 bg-yellow-50", success: "text-green-600 bg-green-50", failed: "text-red-600 bg-red-50", canceled: "text-red-600 bg-red-50", refunded: "text-gray-600 bg-gray-50", }; return colorMap[status] || "text-gray-600 bg-gray-50"; }; const getPaymentMethodLabel = (method) => { const methodMap = { mellat: "بانک ملت", sep: "سامان (سپ)", mock: "درگاه آزمایشی", }; return methodMap[method] || method; }; const formatDate = (timestamp) => { const date = new Date(parseInt(timestamp) * 1000); return new Intl.DateTimeFormat("fa-IR", { year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit", }).format(date); }; const formatAmount = (rials) => formatToman(rials); if (loading) { return ( در حال بارگذاری... ); } if (error) { return ( {error} ); } if (!payment) { return null; } return ( جزئیات پرداخت {/* وضعیت پرداخت */} وضعیت: {getStatusLabel(payment.status)} {/* مبلغ */} مبلغ: {formatAmount(payment.amount_rials)} تومان {/* روش پرداخت */} روش پرداخت: {getPaymentMethodLabel(payment.gateway)} {/* شناسه پرداخت */} شناسه پرداخت: {payment.uuid} {/* شماره مرجع */} {payment.reference_id && ( شماره مرجع: {payment.reference_id} )} {/* تاریخ ایجاد */} تاریخ ایجاد: {formatDate(payment.created_at)} {/* نوع سرویس */} نوع سرویس: {payment.type === "appointment" ? "رزرو نوبت" : payment.type} {/* دکمهها */} {["pending", "failed", "canceled"].includes(payment.status) && payment.appointment_uuid ? ( {paying ? "در حال انتقال به درگاه..." : payment.status === "pending" ? "پرداخت" : "تلاش مجدد"} ) : null} router.push("/dashboard?sidebar=1")} className="flex-1 bg-[#5559CE] hover:bg-[#4448b3] text-white font-bold py-3 px-6 rounded-lg transition-colors" > بازگشت به نوبت های من ); }
در حال بارگذاری...
{error}