feat(payment): add raw logs, IP/UA capture, exponential backoff, inventory hold, IPaymentGateway interface and admin health check
Deploy Canina / deploy (push) Successful in 1m53s

This commit is contained in:
parsa aghaei committed 2026-08-16 12:24:20 +03:30
1 parent 5ace474d71
commit 6e98b8779d
18 files changed
+11978 -9477

No files matched your search

+147 -11
View File
@@ -18,7 +18,11 @@ import {
ArrowUpDown,
ExternalLink,
ShieldCheck,
RotateCcw
RotateCcw,
Activity,
Globe,
Monitor,
Code
} from 'lucide-react';
import { toast } from 'react-hot-toast';
import api from '../services/api';
@@ -40,6 +44,10 @@ interface Transaction {
message?: string | null;
description?: string | null;
type: string;
ipAddress?: string | null;
userAgent?: string | null;
rawRequest?: any;
rawResponse?: any;
paidAt?: string | null;
createdAt: string;
user?: {
@@ -68,9 +76,21 @@ interface Stats {
successRate: number;
}
interface GatewayHealth {
gatewayName: string;
status: 'ONLINE' | 'DEGRADED' | 'OFFLINE';
latencyMs: number;
merchantConfigured: boolean;
activeMerchant: string;
message: string;
checkedAt: string;
}
export default function Transactions() {
const [transactions, setTransactions] = useState<Transaction[]>([]);
const [stats, setStats] = useState<Stats | null>(null);
const [health, setHealth] = useState<GatewayHealth | null>(null);
const [healthLoading, setHealthLoading] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [isReconciling, setIsReconciling] = useState(false);
const [page, setPage] = useState(1);
@@ -89,6 +109,7 @@ export default function Transactions() {
const [selectedTx, setSelectedTx] = useState<Transaction | null>(null);
const [liveInquiryLoading, setLiveInquiryLoading] = useState(false);
const [liveInquiryData, setLiveInquiryData] = useState<any>(null);
const [showRawLogs, setShowRawLogs] = useState(false);
const fetchStats = async () => {
try {
@@ -101,6 +122,22 @@ export default function Transactions() {
}
};
const fetchHealth = async (silent = false) => {
try {
if (!silent) setHealthLoading(true);
const res = await api.get('/payment/admin/health');
if (res.data) {
setHealth(res.data);
if (!silent) toast.success(`وضعیت درگاه زیبال: ${res.data.status} (${res.data.latencyMs}ms)`);
}
} catch (e) {
console.error('Failed to fetch gateway health', e);
if (!silent) toast.error('عدم دسترسی به سرویس Health Check درگاه');
} finally {
if (!silent) setHealthLoading(false);
}
};
const fetchTransactions = useCallback(async () => {
try {
setIsLoading(true);
@@ -131,6 +168,7 @@ export default function Transactions() {
useEffect(() => {
fetchTransactions();
fetchStats();
fetchHealth(true);
}, [fetchTransactions]);
const handleCopy = (text: string, title: string) => {
@@ -143,7 +181,7 @@ export default function Transactions() {
setIsReconciling(true);
const res = await api.post('/payment/admin/reconcile');
toast.success(
`انطباق تراکنش‌ها انجام شد: ${res.data.verified} تایید شده، ${res.data.failed} ناموفق`,
`انطباق انجام شد: ${res.data.verified} تایید شده، ${res.data.failed} ناموفق`,
);
fetchTransactions();
fetchStats();
@@ -181,20 +219,63 @@ export default function Transactions() {
لاگ‌ها و مدیریت تراکنش‌های مالی
</h2>
<p className="text-gray-500 font-medium mt-1">
رهگیری لحظه‌ای پرداخت‌های آنلاین زیبال، کارت به کارت، کیف پول و عیب‌یابی تراکنش‌ها
رهگیری لحظه‌ای پرداخت‌های آنلاین زیبال، بررسی لاگ‌های خام و عیب‌یابی تراکنش‌ها
</p>
</div>
<button
onClick={handleReconcile}
disabled={isReconciling}
className="bg-indigo-50 text-indigo-700 hover:bg-indigo-100 border border-indigo-200 text-xs font-bold px-4 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-sm disabled:opacity-50"
>
<RotateCcw className={`w-4 h-4 ${isReconciling ? 'animate-spin' : ''}`} />
<span>{isReconciling ? 'در حال استعلام...' : 'انطباق خودکار تراکنش‌های معلق'}</span>
</button>
<div className="flex items-center gap-2 flex-wrap">
<button
onClick={() => fetchHealth(false)}
disabled={healthLoading}
className="bg-purple-50 text-purple-700 hover:bg-purple-100 border border-purple-200 text-xs font-bold px-3.5 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-sm disabled:opacity-50"
>
<Activity className={`w-4 h-4 text-purple-600 ${healthLoading ? 'animate-spin' : ''}`} />
<span>پایش سلامت درگاه (Ping)</span>
</button>
<button
onClick={handleReconcile}
disabled={isReconciling}
className="bg-indigo-50 text-indigo-700 hover:bg-indigo-100 border border-indigo-200 text-xs font-bold px-3.5 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-sm disabled:opacity-50"
>
<RotateCcw className={`w-4 h-4 ${isReconciling ? 'animate-spin' : ''}`} />
<span>{isReconciling ? 'در حال استعلام...' : 'انطباق خودکار تراکنش‌ها'}</span>
</button>
</div>
</div>
{/* Gateway Health Monitor Banner */}
{health && (
<div className="bg-gradient-to-r from-slate-900 via-indigo-950 to-purple-950 p-4 rounded-2xl text-white shadow-md flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3 border border-indigo-800/40">
<div className="flex items-center gap-3">
<div className="relative flex items-center justify-center">
<span className={`w-3.5 h-3.5 rounded-full ${health.status === 'ONLINE' ? 'bg-emerald-500 animate-ping' : health.status === 'DEGRADED' ? 'bg-amber-500' : 'bg-rose-500'}`} />
<span className={`absolute w-2.5 h-2.5 rounded-full ${health.status === 'ONLINE' ? 'bg-emerald-400' : health.status === 'DEGRADED' ? 'bg-amber-400' : 'bg-rose-400'}`} />
</div>
<div>
<div className="flex items-center gap-2">
<span className="font-black text-sm">{health.gatewayName}</span>
<span className={`text-[10px] font-black px-2 py-0.5 rounded-full ${health.status === 'ONLINE' ? 'bg-emerald-500/20 text-emerald-300 border border-emerald-500/30' : 'bg-amber-500/20 text-amber-300 border border-amber-500/30'}`}>
{health.status}
</span>
</div>
<p className="text-[11px] text-gray-300 mt-0.5">{health.message}</p>
</div>
</div>
<div className="flex items-center gap-4 text-xs font-mono text-gray-300 mr-auto sm:mr-0">
<div className="bg-white/10 px-3 py-1.5 rounded-xl border border-white/10 flex items-center gap-1.5">
<Clock className="w-3.5 h-3.5 text-purple-300" />
<span>تاخیر: <strong>{health.latencyMs} ms</strong></span>
</div>
<div className="bg-white/10 px-3 py-1.5 rounded-xl border border-white/10 flex items-center gap-1.5">
<ShieldCheck className="w-3.5 h-3.5 text-emerald-300" />
<span>مرچنت: <strong>{health.activeMerchant}</strong></span>
</div>
</div>
</div>
)}
{/* Summary Stat Cards */}
{stats && (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
@@ -488,6 +569,7 @@ export default function Transactions() {
onClick={() => {
setSelectedTx(tx);
setLiveInquiryData(null);
setShowRawLogs(false);
}}
className="p-2 bg-gray-100 hover:bg-purple-50 text-gray-600 hover:text-purple-600 rounded-xl transition-all"
title="مشاهده جزئیات و عیب‌یابی"
@@ -598,6 +680,24 @@ export default function Transactions() {
{selectedTx.cardNumber || 'ثبت نشده'}
</span>
</div>
{selectedTx.ipAddress && (
<div>
<span className="text-gray-400 block font-medium">IP کلاینت:</span>
<span className="font-mono font-bold text-gray-700 dir-ltr inline-block">
{selectedTx.ipAddress}
</span>
</div>
)}
{selectedTx.userAgent && (
<div className="sm:col-span-2">
<span className="text-gray-400 block font-medium">User-Agent دستگاه:</span>
<span className="text-[10px] font-mono text-gray-600 line-clamp-1 dir-ltr inline-block">
{selectedTx.userAgent}
</span>
</div>
)}
</div>
{/* Error Message / Reason */}
@@ -661,12 +761,48 @@ export default function Transactions() {
)}
</div>
{/* Raw Request & Response Inspector Toggle */}
<div className="border border-gray-200 rounded-2xl overflow-hidden">
<button
type="button"
onClick={() => setShowRawLogs(!showRawLogs)}
className="w-full bg-gray-50 hover:bg-gray-100 px-4 py-3 text-xs font-bold text-gray-700 flex items-center justify-between transition-colors"
>
<span className="flex items-center gap-2">
<Code className="w-4 h-4 text-purple-600" />
مشاهده لاگ خام درخواست و پاسخ (Raw JSON Payload)
</span>
<span>{showRawLogs ? '▲ بستن' : '▼ باز کردن'}</span>
</button>
{showRawLogs && (
<div className="p-4 bg-slate-950 text-emerald-400 text-[11px] font-mono space-y-4 dir-ltr overflow-x-auto">
{selectedTx.rawRequest && (
<div>
<p className="text-purple-400 font-bold mb-1">// Raw Request:</p>
<pre className="p-2 bg-slate-900 rounded-lg">{JSON.stringify(selectedTx.rawRequest, null, 2)}</pre>
</div>
)}
{selectedTx.rawResponse && (
<div>
<p className="text-cyan-400 font-bold mb-1">// Raw Response:</p>
<pre className="p-2 bg-slate-900 rounded-lg">{JSON.stringify(selectedTx.rawResponse, null, 2)}</pre>
</div>
)}
{!selectedTx.rawRequest && !selectedTx.rawResponse && (
<p className="text-gray-500">// لاگ خامی برای این تراکنش ثبت نشده است.</p>
)}
</div>
)}
</div>
{/* Close Button */}
<div className="flex justify-end">
<button
onClick={() => {
setSelectedTx(null);
setLiveInquiryData(null);
setShowRawLogs(false);
}}
className="bg-gray-100 hover:bg-gray-200 text-gray-800 font-bold px-6 py-2.5 rounded-xl text-xs transition-colors"
>