feat: enhance payment flow with dynamic URL handling and improved error messaging
This commit is contained in:
+20
-20
@@ -23,9 +23,9 @@ export default function PaymentDetailsPage() {
|
||||
gateway: payment.gateway || "mellat",
|
||||
frontend_address: `${window.location.origin}/payment/result`,
|
||||
});
|
||||
const redirectUrl = res?.data?.redirect_url;
|
||||
if (redirectUrl) {
|
||||
window.location.href = redirectUrl;
|
||||
const payUrl = res?.data?.pay_url;
|
||||
if (payUrl) {
|
||||
window.location.href = payUrl;
|
||||
} else {
|
||||
setPaying(false);
|
||||
}
|
||||
@@ -46,7 +46,8 @@ export default function PaymentDetailsPage() {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await request.getPayment(params.uuid);
|
||||
setPayment(response?.data?.data ?? null);
|
||||
// سازگار با پاسخ flat (جدید) و double-nested (قدیمی)
|
||||
setPayment(response?.data?.data ?? response?.data ?? null);
|
||||
} catch (err) {
|
||||
setError("خطا در دریافت اطلاعات پرداخت");
|
||||
console.error("Payment fetch error:", err);
|
||||
@@ -65,6 +66,7 @@ export default function PaymentDetailsPage() {
|
||||
pending: "در انتظار پرداخت",
|
||||
success: "پرداخت موفق",
|
||||
failed: "پرداخت ناموفق",
|
||||
canceled: "پرداخت لغو شد",
|
||||
refunded: "مسترد شده",
|
||||
};
|
||||
return statusMap[status] || status;
|
||||
@@ -75,6 +77,7 @@ export default function PaymentDetailsPage() {
|
||||
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";
|
||||
@@ -207,22 +210,19 @@ export default function PaymentDetailsPage() {
|
||||
|
||||
{/* دکمهها */}
|
||||
<div className="mt-8 flex gap-4">
|
||||
{payment.status === "pending" ? (
|
||||
<>
|
||||
<button
|
||||
onClick={handleRetryPayment}
|
||||
disabled={paying || !payment.appointment_uuid}
|
||||
className="flex-1 bg-[#5559CE] hover:bg-[#4448b3] disabled:opacity-60 text-white font-bold py-3 px-6 rounded-lg transition-colors"
|
||||
>
|
||||
{paying ? "در حال انتقال به درگاه..." : "پرداخت"}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="flex-1 bg-gray-200 hover:bg-gray-300 text-[#3B3B3B] font-bold py-3 px-6 rounded-lg transition-colors"
|
||||
>
|
||||
بروزرسانی وضعیت
|
||||
</button>
|
||||
</>
|
||||
{["pending", "failed", "canceled"].includes(payment.status) &&
|
||||
payment.appointment_uuid ? (
|
||||
<button
|
||||
onClick={handleRetryPayment}
|
||||
disabled={paying}
|
||||
className="flex-1 bg-[#5559CE] hover:bg-[#4448b3] disabled:opacity-60 text-white font-bold py-3 px-6 rounded-lg transition-colors"
|
||||
>
|
||||
{paying
|
||||
? "در حال انتقال به درگاه..."
|
||||
: payment.status === "pending"
|
||||
? "پرداخت"
|
||||
: "تلاش مجدد"}
|
||||
</button>
|
||||
) : null}
|
||||
<button
|
||||
onClick={() => router.push("/dashboard?sidebar=2")}
|
||||
|
||||
@@ -10,8 +10,10 @@ function PaymentResultRedirect() {
|
||||
|
||||
useEffect(() => {
|
||||
const paymentUuid = searchParams.get("payment_uuid");
|
||||
const status = searchParams.get("status");
|
||||
if (paymentUuid) {
|
||||
router.replace(`/payment/${paymentUuid}`);
|
||||
const q = status ? `?status=${status}` : "";
|
||||
router.replace(`/payment/${paymentUuid}${q}`);
|
||||
} else {
|
||||
router.replace("/dashboard?sidebar=2");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user