feat: refactor payment handling to use direct backend redirect instead of API call

This commit is contained in:
hamed
2026-07-02 17:08:52 +03:30
parent e83a666adb
commit e6b55c6bad
4 changed files with 116 additions and 39 deletions
+11 -21
View File
@@ -51,29 +51,19 @@ function Paying({ setStep, appointmentId, expiresAt, doctor, data, isForAnother,
const expired = timeLeft === 0;
const progress = Math.max(0, Math.min(100, (timeLeft / PAYMENT_TTL) * 100));
const handlePayment = async () => {
const handlePayment = () => {
if (!appointmentId) return;
if (!testMode && !selectedBank) return;
const gateway = testMode ? "mellat" : selectedBank;
if (!gateway) return;
setLoading(true);
try {
const res = await request.postAppointmentPayment({
appointment_uuid: appointmentId,
gateway: testMode ? "mellat" : selectedBank,
frontend_address: `${window.location.origin}/payment/result`,
});
// فقط به بک‌اند می‌رویم؛ بک‌اند صلاحیت را تأیید و به درگاه بانک منتقل می‌کند.
// کلاینت هرگز مستقیم به بانک ریکوست نمی‌زند.
const payUrl = res?.data?.pay_url;
if (payUrl) {
window.location.href = payUrl;
return;
}
throw new Error("pay_url missing");
} catch (error) {
console.error("Payment error:", error);
alert("خطا در شروع پرداخت. لطفاً دوباره تلاش کنید.");
setLoading(false);
}
// بدون هیچ XHR: مرورگر مستقیماً به entry بک‌اند می‌رود؛ بک‌اند صلاحیت را تأیید،
// Payment را می‌سازد و به درگاه بانک منتقل می‌کند.
const apiBase = process.env.NEXT_PUBLIC_API_URL;
const ret = encodeURIComponent(`${window.location.origin}/payment/result`);
window.location.href =
`${apiBase}/api/v1/payment/order/${appointmentId}` +
`?gateway=${encodeURIComponent(gateway)}&return=${ret}`;
};
return (