fixed pay dialog flow
This commit is contained in:
@@ -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: <AuthGuard />,
|
||||
errorElement: <PageErrorFallback />,
|
||||
children: [
|
||||
{ path: "/visa-master/payment-callback", element: page(VisaMasterPaymentCallbackPage) },
|
||||
{
|
||||
element: <DashboardLayout />,
|
||||
errorElement: <PageErrorFallback />,
|
||||
|
||||
@@ -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<PaymentResultResponse>(`/visa-master-payment-orders/${id}/status`);
|
||||
return data;
|
||||
},
|
||||
|
||||
async callback(paymentId: number): Promise<PaymentCallbackResponse> {
|
||||
const { data } = await apiClient.get<PaymentCallbackResponse>("/visa-master-payment-orders/callback", {
|
||||
params: { paymentId },
|
||||
});
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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<PayResponse | null>(null);
|
||||
const [paymentResult, setPaymentResult] = useState<PaymentResultResponse | null>(null);
|
||||
const [paymentResult, setPaymentResult] = useState<PaymentCallbackResponse | null>(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 (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogContent className="sm:max-w-lg">
|
||||
@@ -382,24 +383,20 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) {
|
||||
{paymentInit ? (
|
||||
<div className="space-y-4">
|
||||
<Alert>
|
||||
<ExternalLink className="h-4 w-4" />
|
||||
{isChecking ? <Loader2 className="h-4 w-4 animate-spin" /> : <ExternalLink className="h-4 w-4" />}
|
||||
<AlertDescription>
|
||||
{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.")}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
{paymentResult ? (
|
||||
<Alert variant={paymentResult.paid ? "default" : "destructive"}>
|
||||
{paymentResult.paid ? <CheckCircle2 className="h-4 w-4" /> : <XCircle className="h-4 w-4" />}
|
||||
<AlertDescription>
|
||||
{paymentResult.paid
|
||||
? t("intlPayment.paySuccess", "Töleg üstünlikli amala aşyryldy")
|
||||
: t("intlPayment.payFailed", "Töleg amala aşyrylmady")}
|
||||
<span className="ml-2 text-xs text-muted-foreground">({paymentResult.gatewayStatus})</span>
|
||||
</AlertDescription>
|
||||
<AlertDescription>{paymentResult.data}</AlertDescription>
|
||||
</Alert>
|
||||
) : null}
|
||||
|
||||
@@ -407,24 +404,6 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) {
|
||||
<Button type="button" variant="outline" onClick={() => handleOpenChange(false)} disabled={isChecking}>
|
||||
{t("common.close", "Ýap")}
|
||||
</Button>
|
||||
{paymentInit.formUrl ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
const popup = openBankPage(paymentInit.formUrl!);
|
||||
if (popup && paymentInit.successUrl) watchBankPage(popup, paymentInit.successUrl);
|
||||
}}
|
||||
disabled={isChecking}
|
||||
>
|
||||
<ExternalLink className="mr-2 h-4 w-4" />
|
||||
{t("intlPayment.pay.openBankPage", "Bank sahypasyny aç")}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button type="button" onClick={handleCheckStatus} disabled={isChecking}>
|
||||
{isChecking ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <RefreshCw className="mr-2 h-4 w-4" />}
|
||||
{t("intlPayment.pay.checkStatus", "Netijäni barla")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</div>
|
||||
) : (
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<unknown>)?.data;
|
||||
}
|
||||
return response;
|
||||
|
||||
@@ -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 (
|
||||
<main className="flex min-h-screen items-center justify-center bg-muted/30 px-4 py-8">
|
||||
<div className="w-full max-w-md space-y-4">
|
||||
{isLoading ? (
|
||||
<Alert>
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
<AlertDescription>{t("common.loading", "Ýüklenýär...")}</AlertDescription>
|
||||
</Alert>
|
||||
) : failed ? (
|
||||
<Alert variant="destructive">
|
||||
<XCircle className="h-4 w-4" />
|
||||
<AlertDescription>{t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")}</AlertDescription>
|
||||
</Alert>
|
||||
) : data ? (
|
||||
<Alert variant={data.paid ? "default" : "destructive"}>
|
||||
{data.paid ? <CheckCircle2 className="h-4 w-4" /> : <XCircle className="h-4 w-4" />}
|
||||
<AlertDescription>{data.data}</AlertDescription>
|
||||
</Alert>
|
||||
) : null}
|
||||
|
||||
{!isLoading && (
|
||||
<Button asChild className="w-full">
|
||||
<Link to="/visa-master">{t("intlPayment.title", "Halkara tölegler (Visa/Mastercard)")}</Link>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user