update payment flow

This commit is contained in:
Jelaletdin12
2026-09-25 21:39:40 +05:00
parent 9e2a2606c9
commit 8dc61d7786
2 changed files with 83 additions and 17 deletions

View File

@@ -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;

View File

@@ -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 (
<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">
<div className="w-full max-w-md rounded-2xl border bg-background p-8 shadow-sm">
{isLoading ? (
<Alert>
<Loader2 className="h-4 w-4 animate-spin" />
<AlertDescription>{t("common.loading", "Ýüklenýär...")}</AlertDescription>
</Alert>
<div className="flex flex-col items-center gap-4 text-center">
<Loader2 className="h-12 w-12 animate-spin text-muted-foreground" />
<p className="text-sm text-muted-foreground">{t("common.loading", "Ýüklenýär...")}</p>
</div>
) : failed ? (
<Alert variant="destructive">
<XCircle className="h-4 w-4" />
<AlertDescription>{t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")}</AlertDescription>
</Alert>
<div className="flex flex-col items-center gap-4 text-center">
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10">
<XCircle className="h-9 w-9 text-destructive" />
</div>
<div className="space-y-1">
<h1 className="text-base font-semibold">{t("intlPayment.pay.callbackErrorTitle", "Netije barlanyp bilmedi")}</h1>
<p className="text-sm text-muted-foreground">{t("intlPayment.pay.callbackError", "Töleg netijesi tapylmady.")}</p>
</div>
{!invalidPaymentId && (
<Button variant="outline" size="sm" onClick={() => refetch()} className="gap-2">
<RefreshCw className="h-4 w-4" />
{t("common.retry", "Täzeden synanyşyň")}
</Button>
)}
<a href="tel:+99312345678" className="flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground">
<Phone className="h-3.5 w-3.5" />
{t("intlPayment.pay.contactOperator", "Operator bilen habarlaşyň")}
</a>
</div>
) : 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>
<div className="flex flex-col items-center gap-4 text-center">
<div
className={`flex h-16 w-16 items-center justify-center rounded-full ${data.paid ? "bg-emerald-500/10" : "bg-destructive/10"}`}
>
{data.paid ? <CheckCircle2 className="h-9 w-9 text-emerald-600" /> : <XCircle className="h-9 w-9 text-destructive" />}
</div>
<div className="space-y-1">
<h1 className="text-base font-semibold">
{data.paid ? t("intlPayment.paySuccessTitle", "Töleg üstünlikli!") : t("intlPayment.payFailedTitle", "Töleg amala aşmady")}
</h1>
<p className="text-sm text-muted-foreground">{data.data}</p>
</div>
</div>
) : null}
{!isLoading && (
<Button asChild className="w-full">
<Button asChild className="mt-6 w-full">
<Link to="/visa-master">{t("intlPayment.title", "Halkara tölegler (Visa/Mastercard)")}</Link>
</Button>
)}