diff --git a/src/app/router/index.tsx b/src/app/router/index.tsx index cf29dbe..a373f80 100644 --- a/src/app/router/index.tsx +++ b/src/app/router/index.tsx @@ -51,6 +51,7 @@ const VisaMasterPaymentsPage = lazy(() => import("@/pages/internationalPayments/ const VisaMasterPaymentCreatePage = lazy(() => import("@/pages/internationalPayments/visaMasterPaymentsCreate")); const VisaMasterPaymentViewPage = lazy(() => import("@/pages/internationalPayments/visaMasterPaymentsView")); const VisaMasterPaymentEditPage = lazy(() => import("@/pages/internationalPayments/visaMasterPaymentsEdit")); +const VisaMasterPaymentCallbackPage = lazy(() => import("@/pages/internationalPayments/visaMasterPaymentCallback")); const SberPaymentPage = lazy(() => import("@/pages/internationalPayments/sberPayments")); const SberPaymentCreatePage = lazy(() => import("@/pages/internationalPayments/sberPaymentsCreate")); const SberPaymentViewPage = lazy(() => import("@/pages/internationalPayments/sberPaymentsView")); @@ -197,6 +198,7 @@ const routes: AppRoute[] = [ element: , errorElement: , children: [ + { path: "/visa-master/payment-callback", element: page(VisaMasterPaymentCallbackPage) }, { element: , errorElement: , diff --git a/src/features/visaMasterPayments/api/visaMasterPaymentsApi.ts b/src/features/visaMasterPayments/api/visaMasterPaymentsApi.ts index 196a077..ee5ef18 100644 --- a/src/features/visaMasterPayments/api/visaMasterPaymentsApi.ts +++ b/src/features/visaMasterPayments/api/visaMasterPaymentsApi.ts @@ -60,6 +60,11 @@ export interface PaymentResultResponse { updatedAt: string; } +export interface PaymentCallbackResponse { + paid: boolean; + data: string; +} + /** Kabul ediji + Upgradyý resminamalar (15 sany) — hem UI hem Payload ortak */ export interface IntlPaymentDocs { doc_sberbank_account: string | null; @@ -425,4 +430,11 @@ export const intlPaymentApi = { const { data } = await apiClient.get(`/visa-master-payment-orders/${id}/status`); return data; }, + + async callback(paymentId: number): Promise { + const { data } = await apiClient.get("/visa-master-payment-orders/callback", { + params: { paymentId }, + }); + return data; + }, }; diff --git a/src/features/visaMasterPayments/components/PayDialog.tsx b/src/features/visaMasterPayments/components/PayDialog.tsx index e10e4a6..e55af95 100644 --- a/src/features/visaMasterPayments/components/PayDialog.tsx +++ b/src/features/visaMasterPayments/components/PayDialog.tsx @@ -1,9 +1,9 @@ import { useEffect, useId, useMemo, useRef, useState } from "react"; import type { ChangeEvent, FormEvent, ReactNode } from "react"; import { useTranslation } from "react-i18next"; -import { AlertCircle, CheckCircle2, ExternalLink, Loader2, RefreshCw, XCircle } from "lucide-react"; -import { useQuoteIntlPayment, usePayIntlPayment, useCheckIntlPaymentStatus } from "../hooks/useVisaMasterPayments"; -import type { PaymentResultResponse, PayResponse } from "../api/visaMasterPaymentsApi"; +import { AlertCircle, CheckCircle2, ExternalLink, Loader2, XCircle } from "lucide-react"; +import { useQuoteIntlPayment, usePayIntlPayment, useCheckIntlPaymentCallback } from "../hooks/useVisaMasterPayments"; +import type { PaymentCallbackResponse, PayResponse } from "../api/visaMasterPaymentsApi"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; @@ -78,27 +78,6 @@ function FieldRow({ label, htmlFor, required, children }: FieldRowProps) { ); } -// Opens the bank's hosted payment page and returns the window handle so the -// caller can watch it for navigation back to successUrl (see watchBankPage -// below) - noopener/noreferrer would make window.open return null, so the -// opener link is severed from our side instead (popup.opener = null) once we -// have the handle, keeping the same tabnabbing mitigation. A popup blocker -// falls back to a same-tab navigation, which returns null - there's nothing -// to watch then, but the manual "Netijäni barla" button still works. -function openBankPage(formUrl: string): Window | null { - const popup = window.open(formUrl, "_blank"); - if (!popup || popup.closed || typeof popup.closed === "undefined") { - window.location.href = formUrl; - return null; - } - try { - popup.opener = null; - } catch { - // some browsers disallow this - not fatal, just skip the mitigation - } - return popup; -} - // ─── Form (mounted only while the dialog is open → state resets automatically) ─ interface PayPayload { @@ -271,9 +250,9 @@ interface PayDialogProps { export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { const { t } = useTranslation(); const { mutate: pay, isPending: isPaying } = usePayIntlPayment(); - const { mutate: checkStatus, isPending: isChecking } = useCheckIntlPaymentStatus(); + const { mutate: checkCallback, isPending: isChecking } = useCheckIntlPaymentCallback(); const [paymentInit, setPaymentInit] = useState(null); - const [paymentResult, setPaymentResult] = useState(null); + const [paymentResult, setPaymentResult] = useState(null); // Tracks the open bank-page popup so it can be polled for navigation back // to successUrl (GBÜS's callback) - see watchBankPage. @@ -292,12 +271,9 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { popupRef.current = null; } - // GBÜS redirects the popup to successUrl (the backend's own callback URL) - // once the bank-side flow ends. Reading a cross-origin window's location - // throws until it navigates back to our own origin, so most ticks just - // retry - this lets the result show up without the operator having to - // click "Netijäni barla" themselves once that happens. - function watchBankPage(popup: Window, successUrl: string) { + // Once GBÜS returns to its backend callback URL, send the popup to our + // callback page. That page requests the final payment state and informs the user. + function watchBankPage(popup: Window, successUrl: string, paymentId: number) { stopWatchingBankPage(); popupRef.current = popup; pollRef.current = setInterval(() => { @@ -313,9 +289,10 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { return; } if (href.startsWith(successUrl)) { - win.close(); stopWatchingBankPage(); - handleCheckStatus(); + win.location.replace( + new URL(`visa-master/payment-callback?paymentId=${paymentId}`, `${window.location.origin}${import.meta.env.BASE_URL}`).href, + ); } }, 1500); } @@ -343,30 +320,54 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { } function handleSubmit(payload: PayPayload) { + // Reserving the window while this click is still a user gesture prevents + // browsers from blocking the bank popup after the async status check. + const bankPopup = window.open("", "_blank"); + if (bankPopup) { + try { + bankPopup.opener = null; + } catch { + // Some browsers disallow this; navigation can still continue. + } + } + pay( { id: stableId, payload }, { onSuccess: (data) => { setPaymentInit(data); setPaymentResult(null); - if (data.formUrl) { - const popup = openBankPage(data.formUrl); - if (popup && data.successUrl) watchBankPage(popup, data.successUrl); + + const paymentId = data.paymentId; + if (!paymentId) { + bankPopup?.close(); + return; } + + checkCallback(paymentId, { + onSuccess: (result) => { + setPaymentResult(result); + if (result.paid || !data.formUrl) { + bankPopup?.close(); + return; + } + + if (!bankPopup) { + window.location.href = data.formUrl; + return; + } + + bankPopup.location.href = data.formUrl; + if (data.successUrl) watchBankPage(bankPopup, data.successUrl, paymentId); + }, + onError: () => bankPopup?.close(), + }); }, + onError: () => bankPopup?.close(), }, ); } - function handleCheckStatus() { - checkStatus(stableId, { - onSuccess: (result) => { - setPaymentResult(result); - stopWatchingBankPage(); - }, - }); - } - return ( @@ -382,24 +383,20 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { {paymentInit ? (
- + {isChecking ? : } - {t( - "intlPayment.pay.bankPageOpened", - "Bankyň töleg sahypasy açyldy. Kart maglumatlary girizilip gutarandan soň netijäni şu ýerden barlaň.", - )} + {isChecking + ? t("intlPayment.pay.checkingStatus", "Töleg ýagdaýy barlanýar...") + : paymentResult?.paid + ? t("intlPayment.paySuccess", "Töleg üstünlikli amala aşyryldy") + : t("intlPayment.pay.bankPageOpened", "Bankyň töleg sahypasy açyldy.")} {paymentResult ? ( {paymentResult.paid ? : } - - {paymentResult.paid - ? t("intlPayment.paySuccess", "Töleg üstünlikli amala aşyryldy") - : t("intlPayment.payFailed", "Töleg amala aşyrylmady")} - ({paymentResult.gatewayStatus}) - + {paymentResult.data} ) : null} @@ -407,24 +404,6 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { - {paymentInit.formUrl ? ( - - ) : null} -
) : ( diff --git a/src/features/visaMasterPayments/hooks/useVisaMasterPayments.ts b/src/features/visaMasterPayments/hooks/useVisaMasterPayments.ts index 31b6b21..a7794f0 100644 --- a/src/features/visaMasterPayments/hooks/useVisaMasterPayments.ts +++ b/src/features/visaMasterPayments/hooks/useVisaMasterPayments.ts @@ -111,3 +111,22 @@ export function useCheckIntlPaymentStatus() { }, }); } + +export function useCheckIntlPaymentCallback() { + const qc = useQueryClient(); + return useMutation({ + mutationFn: (paymentId: number) => intlPaymentApi.callback(paymentId), + onSuccess: () => { + qc.invalidateQueries({ queryKey: INTL_PAYMENT_KEYS.lists() }); + }, + }); +} + +export function useIntlPaymentCallback(paymentId: number | null) { + return useQuery({ + queryKey: [...INTL_PAYMENT_KEYS.all, "callback", paymentId] as const, + queryFn: () => intlPaymentApi.callback(paymentId!), + enabled: paymentId !== null, + retry: false, + }); +} diff --git a/src/lib/api/client.ts b/src/lib/api/client.ts index 485ea54..dbeee2d 100644 --- a/src/lib/api/client.ts +++ b/src/lib/api/client.ts @@ -62,7 +62,7 @@ apiClient.interceptors.response.use( return response; } - if (response.data && typeof response.data === "object" && "data" in response.data) { + if (response.data && typeof response.data === "object" && response.data.status === true && "data" in response.data) { response.data = (response.data as ApiSuccessResponse)?.data; } return response; diff --git a/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx b/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx new file mode 100644 index 0000000..c0764ae --- /dev/null +++ b/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx @@ -0,0 +1,51 @@ +import { CheckCircle2, Loader2, XCircle } from "lucide-react"; +import { Link, useSearchParams } from "react-router-dom"; +import { useTranslation } from "react-i18next"; +import { Alert, AlertDescription } from "@/components/ui/alert"; +import { Button } from "@/components/ui/button"; +import { useIntlPaymentCallback } from "@/features/visaMasterPayments/hooks/useVisaMasterPayments"; + +function getPaymentId(value: string | null): number | null { + if (!value || !/^\d+$/.test(value)) return null; + const paymentId = Number(value); + return Number.isSafeInteger(paymentId) && paymentId > 0 ? paymentId : null; +} + +export default function VisaMasterPaymentCallbackPage() { + const { t } = useTranslation(); + const [searchParams] = useSearchParams(); + const paymentId = getPaymentId(searchParams.get("paymentId")); + const { data, isLoading, isError } = useIntlPaymentCallback(paymentId); + + const invalidPaymentId = paymentId === null; + const failed = invalidPaymentId || isError; + + return ( +
+
+ {isLoading ? ( + + + {t("common.loading", "Ýüklenýär...")} + + ) : failed ? ( + + + {t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")} + + ) : data ? ( + + {data.paid ? : } + {data.data} + + ) : null} + + {!isLoading && ( + + )} +
+
+ ); +}