369 lines
17 KiB
TypeScript
369 lines
17 KiB
TypeScript
import type React from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { AlertTriangle, Database, Eye, ExternalLink } from "lucide-react";
|
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog";
|
|
import { useGetOnlinePaymentDetail, useGetOnlinePaymentById } from "@/features/onlinePaymentHistory/hooks/useOnlinePaymentsHistory";
|
|
import { GatewayStatusBadge, PaidBadge } from "@/features/onlinePaymentHistory/components/PaymentStatusBadge";
|
|
import { formatFromApiDate } from "@/lib/utils/dateUtils";
|
|
import { rawEnumKey } from "@/constants/enumLabels";
|
|
|
|
function resolveLang(lang: string): "en" | "ru" | "tm" {
|
|
if (lang === "ru") return "ru";
|
|
if (lang === "tk") return "tm";
|
|
return "en";
|
|
}
|
|
|
|
interface PaymentDetailDialogProps {
|
|
paymentId: number | null;
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
export function PaymentDetailDialog({ paymentId, open, onOpenChange }: PaymentDetailDialogProps) {
|
|
const { t } = useTranslation();
|
|
const { data, isLoading, isError } = useGetOnlinePaymentDetail(paymentId ?? 0, { lang: "tm" });
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-lg max-h-[85vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<div className="flex items-center gap-2 pr-6 flex-wrap">
|
|
<Database className="size-5 text-primary shrink-0" />
|
|
<DialogTitle className="text-base font-semibold">
|
|
{t("onlinePayments.paymentViewTitle", "Halk bankdan gelen netije")}: #{paymentId}
|
|
</DialogTitle>
|
|
</div>
|
|
<DialogDescription className="text-xs">{t("onlinePayments.gatewayResponse.title", "Gateway response")}</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
{isLoading ? (
|
|
<div className="space-y-3 py-2">
|
|
<Skeleton className="h-6 w-1/3" />
|
|
<Skeleton className="h-4 w-1/2" />
|
|
<Skeleton className="h-72 w-full rounded-lg" />
|
|
</div>
|
|
) : isError || !data ? (
|
|
<div className="py-8 text-center text-sm text-destructive">{t("common.error", "Maglumat ýüklenende ýalňyşlyk ýüze çykdy")}</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{data.gatewayResponse && (data.gatewayResponse.errorCode != null || data.gatewayResponse.errorMessage) && (
|
|
<div className="flex items-start gap-2.5 rounded-lg border border-destructive/30 bg-destructive/10 p-3 text-destructive">
|
|
<AlertTriangle className="mt-0.5 size-4 shrink-0" />
|
|
<div className="space-y-0.5 text-xs">
|
|
<div className="font-semibold">
|
|
{t("onlinePayments.gatewayErrorTitle", "Gateway error")}
|
|
{data.gatewayResponse.errorCode != null && (
|
|
<span>
|
|
{" "}
|
|
({t("onlinePayments.gateway.errorCode", "Error code")}: {data.gatewayResponse.errorCode})
|
|
</span>
|
|
)}
|
|
</div>
|
|
{data.gatewayResponse.errorMessage && <div>{data.gatewayResponse.errorMessage}</div>}
|
|
</div>
|
|
</div>
|
|
)}
|
|
<pre className="max-h-[65vh] overflow-auto rounded-lg border border-border bg-muted/30 p-4 text-xs leading-5 text-foreground whitespace-pre-wrap break-all">
|
|
{JSON.stringify(data.gatewayResponse ?? {}, null, 2)}
|
|
</pre>
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
// ─── Shared Row helper ────────────────────────────────────────────────────────
|
|
|
|
function Row({ label, children }: { label: string; children: React.ReactNode }) {
|
|
return (
|
|
<div className="flex gap-2 text-xs">
|
|
<span className="text-muted-foreground font-medium min-w-[130px] shrink-0">{label}</span>
|
|
<span className="text-foreground break-all">{children}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ─── LastStepDialog ───────────────────────────────────────────────────────────
|
|
|
|
export interface LastStepDialogProps {
|
|
/** The ID used to fetch the step history from /payment-history/:id. */
|
|
orderId: number | null;
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
/**
|
|
* Opens a dialog showing only the most recent payment history step / operation
|
|
* for the given order. Used by the Eye icon in PaymentHistoryTable.
|
|
* Fetches its own data independently via useGetOnlinePaymentById (/payment-history/:id).
|
|
*/
|
|
export function LastStepDialog({ orderId, open, onOpenChange }: LastStepDialogProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const lang = resolveLang(i18n.language);
|
|
const { data: rawSteps, isLoading, isError } = useGetOnlinePaymentById(orderId ?? 0);
|
|
|
|
const steps: any[] = Array.isArray(rawSteps)
|
|
? rawSteps
|
|
: (rawSteps as any)?.data && Array.isArray((rawSteps as any).data)
|
|
? (rawSteps as any).data
|
|
: (rawSteps as any)?.items && Array.isArray((rawSteps as any).items)
|
|
? (rawSteps as any).items
|
|
: rawSteps && typeof rawSteps === "object" && (rawSteps as any).id
|
|
? [rawSteps]
|
|
: [];
|
|
|
|
const lastStep = steps.length
|
|
? [...steps].sort((a, b) => new Date(b.createdAt || 0).getTime() - new Date(a.createdAt || 0).getTime())[0]
|
|
: null;
|
|
|
|
const branchDisplay =
|
|
typeof lastStep?.branchName === "string"
|
|
? lastStep.branchName
|
|
: lastStep?.branchName?.[lang] || lastStep?.branchName?.tm || lastStep?.branchName?.en || null;
|
|
|
|
const fullName = [lastStep?.passportName, lastStep?.passportSurname].filter(Boolean).join(" ");
|
|
const detailEntries = Object.entries(lastStep?.detail ?? {});
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-lg max-h-[85vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<div className="flex items-center gap-2 pr-6 flex-wrap">
|
|
<Eye className="size-5 text-primary shrink-0" />
|
|
<DialogTitle className="text-base font-semibold">{t("onlinePayments.lastStep.title", "Iň soňky işlem")}</DialogTitle>
|
|
{lastStep && (
|
|
<>
|
|
{(lastStep.gatewayStatus || lastStep.step) && <GatewayStatusBadge status={lastStep.gatewayStatus || lastStep.step} />}
|
|
{typeof lastStep.paid === "boolean" && <PaidBadge paid={lastStep.paid} />}
|
|
</>
|
|
)}
|
|
</div>
|
|
{(lastStep?.orderNumber || lastStep?.orderId || orderId) && (
|
|
<DialogDescription className="text-xs">
|
|
{t("onlinePayments.columns.orderNumber", "Sargyt belgisi")}:{" "}
|
|
{lastStep?.orderNumber || `Order #${lastStep?.orderId || orderId}`}
|
|
</DialogDescription>
|
|
)}
|
|
</DialogHeader>
|
|
|
|
{isLoading ? (
|
|
<div className="space-y-3 py-2">
|
|
<Skeleton className="h-6 w-1/3" />
|
|
<Skeleton className="h-4 w-1/2" />
|
|
<Skeleton className="h-28 w-full rounded-lg" />
|
|
<Skeleton className="h-28 w-full rounded-lg" />
|
|
</div>
|
|
) : isError || !lastStep ? (
|
|
<div className="py-8 text-center text-sm text-muted-foreground">{t("onlinePayments.noHistory", "Töleg taryhy tapylmady")}</div>
|
|
) : (
|
|
<div className="space-y-3 text-xs">
|
|
{/* Section 1: General */}
|
|
<div className="rounded-lg border border-border p-3 space-y-2 bg-muted/10">
|
|
<p className="font-semibold text-muted-foreground uppercase text-[11px] tracking-wide">
|
|
{t("onlinePayments.sectionGeneral", "Esasy maglumatlar")}
|
|
</p>
|
|
<div className="space-y-1.5">
|
|
{lastStep.id != null && (
|
|
<Row label={t("common.id", "ID")}>
|
|
<span className="font-mono font-medium">{lastStep.id}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.orderId != null && (
|
|
<Row label={t("onlinePayments.columns.orderId", "Sargyt ID")}>
|
|
<Link to={`/visa-master/${lastStep.orderId}`} className="font-mono text-primary hover:underline">
|
|
#{lastStep.orderId}
|
|
</Link>
|
|
</Row>
|
|
)}
|
|
{lastStep.orderNumber && (
|
|
<Row label={t("onlinePayments.columns.orderNumber", "Sargyt belgisi")}>
|
|
<span className="font-mono">{lastStep.orderNumber}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.type && (
|
|
<Row label={t("onlinePayments.columns.type", "Görnüşi")}>
|
|
<span className="uppercase font-medium">{lastStep.type}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.description && (
|
|
<Row label={t("onlinePayments.columns.description", "Düşündiriş")}>
|
|
<span>{lastStep.description}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.gatewayReference && (
|
|
<Row label={t("onlinePayments.columns.gatewayReference", "Gateway Ref")}>
|
|
<span className="font-mono text-[11px] break-all">{lastStep.gatewayReference}</span>
|
|
</Row>
|
|
)}
|
|
{(lastStep.apiClientName || lastStep.apiClient) && (
|
|
<Row label={t("onlinePayments.columns.apiClient", "API Client")}>
|
|
<span className="font-mono">{lastStep.apiClientName || lastStep.apiClient}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.callbackStatus && (
|
|
<Row label={t("onlinePayments.columns.callbackStatus", "Callback ýagdaýy")}>
|
|
<span className="uppercase font-medium">{lastStep.callbackStatus}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.createdAt && (
|
|
<Row label={t("onlinePayments.columns.createdAt", "Döredilen wagty")}>
|
|
<span>{formatFromApiDate(lastStep.createdAt)}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.callbackAt && (
|
|
<Row label={t("onlinePayments.columns.callbackAt", "Callback wagty")}>
|
|
<span>{formatFromApiDate(lastStep.callbackAt)}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.updatedAt && (
|
|
<Row label={t("onlinePayments.columns.updatedAt", "Täzelenen wagty")}>
|
|
<span>{formatFromApiDate(lastStep.updatedAt)}</span>
|
|
</Row>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Section 2: Financial */}
|
|
{(lastStep.amountTmt != null || lastStep.totalAmountTmt != null || lastStep.amountUsd != null) && (
|
|
<div className="rounded-lg border border-border p-3 space-y-2 bg-muted/10">
|
|
<p className="font-semibold text-muted-foreground uppercase text-[11px] tracking-wide">
|
|
{t("onlinePayments.sectionFinancial", "Maliýe maglumatlary")}
|
|
</p>
|
|
<div className="space-y-1.5">
|
|
{lastStep.totalAmountTmt != null && (
|
|
<Row label={t("onlinePayments.columns.totalAmount", "Jemi töleg möçberi")}>
|
|
<span className="font-semibold text-primary">
|
|
{lastStep.totalAmountTmt?.toLocaleString?.() ?? lastStep.totalAmountTmt} TMT
|
|
</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.amountTmt != null && (
|
|
<Row label={t("onlinePayments.columns.amountTmt", "Töleg möçberi")}>
|
|
<span className="font-medium">{lastStep.amountTmt?.toLocaleString?.() ?? lastStep.amountTmt} TMT</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.bankFeeTmt != null && (
|
|
<Row label={t("onlinePayments.columns.bankFeeTmt", "Bank komissiýasy")}>
|
|
<span>{lastStep.bankFeeTmt?.toLocaleString?.() ?? lastStep.bankFeeTmt} TMT</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.gbusFeeTmt != null && (
|
|
<Row label={t("onlinePayments.columns.gbusFeeTmt", "GBUS komissiýasy")}>
|
|
<span>{lastStep.gbusFeeTmt?.toLocaleString?.() ?? lastStep.gbusFeeTmt} TMT</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.amountUsd != null && (
|
|
<Row label={t("onlinePayments.columns.amountUsd", "Möçber (USD)")}>
|
|
<span>{lastStep.amountUsd} $</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.exchangeRateUsd != null && (
|
|
<Row label={t("onlinePayments.columns.exchangeRateUsd", "Hümmet (USD)")}>
|
|
<span>{lastStep.exchangeRateUsd} TMT</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.paymentPeriod && (
|
|
<Row label={t("onlinePayments.columns.paymentPeriod", "Töleg döwri")}>
|
|
<span>{formatFromApiDate(lastStep.paymentPeriod)}</span>
|
|
</Row>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Section 3: Customer & Branch */}
|
|
{(fullName || lastStep.branchName || lastStep.createdByName || lastStep.userId) && (
|
|
<div className="rounded-lg border border-border p-3 space-y-2 bg-muted/10">
|
|
<p className="font-semibold text-muted-foreground uppercase text-[11px] tracking-wide">
|
|
{t("onlinePayments.sectionCustomer", "Müşderi we Şahamça")}
|
|
</p>
|
|
<div className="space-y-1.5">
|
|
{fullName && (
|
|
<Row label={t("onlinePayments.columns.passport", "Müşderi")}>
|
|
<span className="font-medium">{fullName}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.userId != null && (
|
|
<Row label={t("onlinePayments.columns.userId", "Ulanyjy ID")}>
|
|
<Link to={`/all-users/${lastStep.userId}`} className="font-mono text-primary hover:underline">
|
|
#{lastStep.userId}
|
|
</Link>
|
|
</Row>
|
|
)}
|
|
{branchDisplay && (
|
|
<Row label={t("onlinePayments.columns.branch", "Şahamça")}>
|
|
<span>{branchDisplay}</span>
|
|
</Row>
|
|
)}
|
|
{lastStep.createdByName && (
|
|
<Row label={t("onlinePayments.columns.createdByName", "Döreden işgär")}>
|
|
<span>{lastStep.createdByName}</span>
|
|
</Row>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Section 4: External URLs */}
|
|
{(lastStep.formUrl || lastStep.successUrl) && (
|
|
<div className="rounded-lg border border-border p-3 space-y-2 bg-muted/10">
|
|
<p className="font-semibold text-muted-foreground uppercase text-[11px] tracking-wide">
|
|
{t("onlinePayments.sectionUrls", "Baglanyşyklar")}
|
|
</p>
|
|
<div className="space-y-1.5">
|
|
{lastStep.formUrl && (
|
|
<Row label={t("onlinePayments.columns.formUrl", "Töleg Form URL")}>
|
|
<a
|
|
href={lastStep.formUrl}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="inline-flex items-center gap-1 text-primary hover:underline break-all"
|
|
>
|
|
<ExternalLink className="size-3 shrink-0" />
|
|
<span className="truncate max-w-[260px]">{lastStep.formUrl}</span>
|
|
</a>
|
|
</Row>
|
|
)}
|
|
{lastStep.successUrl && (
|
|
<Row label={t("onlinePayments.columns.successUrl", "Success URL")}>
|
|
<a
|
|
href={lastStep.successUrl}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="inline-flex items-center gap-1 text-primary hover:underline break-all"
|
|
>
|
|
<ExternalLink className="size-3 shrink-0" />
|
|
<span className="truncate max-w-[260px]">{lastStep.successUrl}</span>
|
|
</a>
|
|
</Row>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Legacy detail entries if present */}
|
|
{detailEntries.length > 0 && (
|
|
<div className="rounded-lg border border-border p-3 space-y-2 bg-muted/10">
|
|
<p className="font-semibold text-muted-foreground uppercase text-[11px] tracking-wide">
|
|
{t("onlinePayments.detail.details", "Jikme-jikler")}
|
|
</p>
|
|
<div className="space-y-1.5">
|
|
{detailEntries.map(([key, value]) => (
|
|
<Row key={key} label={t(`onlinePayments.detailKeys.${key}`, rawEnumKey(key))}>
|
|
<span>{String(value)}</span>
|
|
</Row>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|