From 8dc61d7786d8259d8fb7f7663b2df14c260b448c Mon Sep 17 00:00:00 2001 From: Jelaletdin12 Date: Fri, 25 Sep 2026 21:39:40 +0500 Subject: [PATCH] update payment flow --- .../components/PayDialog.tsx | 23 ++++++ .../visaMasterPaymentCallback/index.tsx | 77 +++++++++++++++---- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/src/features/visaMasterPayments/components/PayDialog.tsx b/src/features/visaMasterPayments/components/PayDialog.tsx index e55af95..921f19f 100644 --- a/src/features/visaMasterPayments/components/PayDialog.tsx +++ b/src/features/visaMasterPayments/components/PayDialog.tsx @@ -30,6 +30,9 @@ const MONTHS = [ /** Digits with optional decimal part (max 2 digits). Accepts "." or ",". */ const AMOUNT_PATTERN = /^\d*([.,]\d{0,2})?$/; +// Shared channel name — must match the callback page exactly. +const PAYMENT_CHANNEL_NAME = "visa-master-payment"; + // ─── Helpers ────────────────────────────────────────────────────────────────── function currentMonthValue(): string { @@ -308,6 +311,26 @@ export function PayDialog({ id, open, onOpenChange }: PayDialogProps) { stopWatchingBankPage(); }, [id, stableId]); + // Listens for the callback tab/popup broadcasting the final result. This is + // the primary sync path: it's instant and doesn't depend on the popup still + // being open or the URL-watch polling above succeeding. + useEffect(() => { + if (typeof BroadcastChannel === "undefined") return; + + const channel = new BroadcastChannel(PAYMENT_CHANNEL_NAME); + channel.onmessage = (event) => { + const msg = event.data as { type?: string; paymentId?: number; paid?: boolean; message?: string } | undefined; + if (!msg || msg.type !== "PAYMENT_RESULT") return; + if (!paymentInit?.paymentId || msg.paymentId !== paymentInit.paymentId) return; + + setPaymentResult({ paid: !!msg.paid, data: msg.message ?? "" }); + stopWatchingBankPage(); + popupRef.current?.close(); + }; + + return () => channel.close(); + }, [paymentInit?.paymentId]); + // Block every close path (ESC, overlay click, X button) while the payment request is in flight. function handleOpenChange(next: boolean) { if (isPaying || isChecking) return; diff --git a/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx b/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx index c0764ae..3fd186c 100644 --- a/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx +++ b/src/pages/internationalPayments/visaMasterPaymentCallback/index.tsx @@ -1,10 +1,13 @@ -import { CheckCircle2, Loader2, XCircle } from "lucide-react"; +import { useEffect } from "react"; +import { CheckCircle2, Loader2, XCircle, RefreshCw, Phone } 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"; +// Must match PayDialog.tsx's PAYMENT_CHANNEL_NAME exactly. +const PAYMENT_CHANNEL_NAME = "visa-master-payment"; + function getPaymentId(value: string | null): number | null { if (!value || !/^\d+$/.test(value)) return null; const paymentId = Number(value); @@ -15,33 +18,73 @@ export default function VisaMasterPaymentCallbackPage() { const { t } = useTranslation(); const [searchParams] = useSearchParams(); const paymentId = getPaymentId(searchParams.get("paymentId")); - const { data, isLoading, isError } = useIntlPaymentCallback(paymentId); + const { data, isLoading, isError, refetch } = useIntlPaymentCallback(paymentId); const invalidPaymentId = paymentId === null; const failed = invalidPaymentId || isError; + // Broadcast the result back to the opener tab (PayDialog), then auto-close + // this tab if it was opened as a popup. Runs once per resolved result. + useEffect(() => { + if (!data || paymentId === null || typeof BroadcastChannel === "undefined") return; + + const channel = new BroadcastChannel(PAYMENT_CHANNEL_NAME); + channel.postMessage({ type: "PAYMENT_RESULT", paymentId, paid: data.paid, message: data.data }); + channel.close(); + + if (window.opener) { + const timer = setTimeout(() => window.close(), 2500); + return () => clearTimeout(timer); + } + }, [data, paymentId]); + return (
-
+
{isLoading ? ( - - - {t("common.loading", "Ýüklenýär...")} - +
+ +

{t("common.loading", "Ýüklenýär...")}

+
) : failed ? ( - - - {t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")} - +
+
+ +
+
+

{t("intlPayment.pay.callbackErrorTitle", "Netije barlanyp bilmedi")}

+

{t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")}

+
+ + {!invalidPaymentId && ( + + )} + + + {t("intlPayment.pay.contactOperator", "Operator bilen habarlaşyň")} + +
) : data ? ( - - {data.paid ? : } - {data.data} - +
+
+ {data.paid ? : } +
+
+

+ {data.paid ? t("intlPayment.paySuccessTitle", "Töleg üstünlikli!") : t("intlPayment.payFailedTitle", "Töleg amala aşmady")} +

+

{data.data}

+
+
) : null} {!isLoading && ( - )}