feat(payment): implement complete Zibal IPG, EBank, Wallets, Refunds and Trust Seals
Deploy Canina / deploy (push) Successful in 1m51s

This commit is contained in:
parsa aghaei committed 2026-08-22 13:40:42 +03:30
1 parent 7203915e15
commit a2b8afccc3
21 files changed
+143035 -8553

No files matched your search

@@ -1,10 +1,24 @@
import { useState, useEffect } from 'react';
import { CreditCard, Save, ShieldCheck, Wallet, ArrowLeftRight, CheckCircle2 } from 'lucide-react';
import {
CreditCard,
Save,
ShieldCheck,
Wallet,
ArrowLeftRight,
CheckCircle2,
Building2,
RefreshCw,
RotateCcw,
Users,
Search,
} from 'lucide-react';
import { toast } from 'react-hot-toast';
import api from '../services/api';
import Spinner from '../components/ui/Spinner';
export default function PaymentGatewaysPage() {
const [activeTab, setActiveTab] = useState<'gateways' | 'wallets' | 'ebank' | 'submerchants'>('gateways');
const [settings, setSettings] = useState({
PAY_GATEWAY_CARD_ENABLE: 'true',
PAY_GATEWAY_WALLET_ENABLE: 'true',
@@ -13,11 +27,37 @@ export default function PaymentGatewaysPage() {
ZIBAL_MERCHANT: 'zibal',
ZIBAL_SANDBOX: 'true',
FRONTEND_URL: 'https://canina.ir',
ZIBAL_EBANK_TOKEN: '',
ZIBAL_EBANK_ACCOUNT_ID: '',
});
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
// Wallets State
const [wallets, setWallets] = useState<any[]>([]);
const [loadingWallets, setLoadingWallets] = useState(false);
// EBank Accounts & Balance
const [ebankAccounts, setEBankAccounts] = useState<any[]>([]);
const [ebankBalance, setEBankBalance] = useState<any>(null);
const [loadingEBank, setLoadingEBank] = useState(false);
// SubMerchants
const [subMerchants, setSubMerchants] = useState<any[]>([]);
const [loadingSubMerchants, setLoadingSubMerchants] = useState(false);
const [newSubMerchant, setNewSubMerchant] = useState({ name: '', bankAccount: '' });
// Refund Modal State
const [refundData, setRefundData] = useState({
trackId: '',
amount: '',
tryReverse: false,
cardNumber: '',
description: '',
});
const [isRefunding, setIsRefunding] = useState(false);
useEffect(() => {
let isSubscribed = true;
api.get('/admin/settings')
@@ -33,6 +73,8 @@ export default function PaymentGatewaysPage() {
ZIBAL_MERCHANT: d.ZIBAL_MERCHANT || 'zibal',
ZIBAL_SANDBOX: d.ZIBAL_SANDBOX ?? 'true',
FRONTEND_URL: d.FRONTEND_URL || 'https://canina.ir',
ZIBAL_EBANK_TOKEN: d.ZIBAL_EBANK_TOKEN || '',
ZIBAL_EBANK_ACCOUNT_ID: d.ZIBAL_EBANK_ACCOUNT_ID || '',
});
}
})
@@ -52,7 +94,7 @@ export default function PaymentGatewaysPage() {
try {
setIsSaving(true);
await api.put('/admin/settings', settings);
toast.success('تنظیمات روش‌های پرداخت با موفقیت بروزرسانی شد.');
toast.success('تنظیمات درگاه و سرویس‌های زیبال با موفقیت ذخیره شد.');
} catch (err) {
console.error('Failed to update payment settings', err);
toast.error('خطا در ذخیره تنظیمات درگاه پرداخت');
@@ -61,209 +103,658 @@ export default function PaymentGatewaysPage() {
}
};
const fetchWallets = async () => {
try {
setLoadingWallets(true);
const res = await api.get('/payment/admin/wallets');
if (res.data?.data) {
setWallets(res.data.data);
}
} catch (e: any) {
toast.error(e?.response?.data?.message || 'خطا در دریافت موجودی کیف‌پول‌ها');
} finally {
setLoadingWallets(false);
}
};
const fetchEBank = async () => {
try {
setLoadingEBank(true);
const [accRes, balRes] = await Promise.allSettled([
api.get('/payment/admin/ebank/accounts'),
api.get('/payment/admin/ebank/balance'),
]);
if (accRes.status === 'fulfilled' && accRes.value.data?.data) {
setEBankAccounts(accRes.value.data.data);
}
if (balRes.status === 'fulfilled' && balRes.value.data) {
setEBankBalance(balRes.value.data);
}
} catch (e: any) {
toast.error('خطا در دریافت اطلاعات بانکداری شرکتی');
} finally {
setLoadingEBank(false);
}
};
const fetchSubMerchants = async () => {
try {
setLoadingSubMerchants(true);
const res = await api.get('/payment/admin/sub-merchants');
if (res.data?.data) {
setSubMerchants(res.data.data);
}
} catch (e: any) {
toast.error('خطا در دریافت لیست ذی‌نفعان');
} finally {
setLoadingSubMerchants(false);
}
};
const handleCreateSubMerchant = async (e: React.FormEvent) => {
e.preventDefault();
try {
await api.post('/payment/admin/sub-merchants', newSubMerchant);
toast.success('ذی‌نفع جدید با موفقیت ثبت شد.');
setNewSubMerchant({ name: '', bankAccount: '' });
fetchSubMerchants();
} catch (e: any) {
toast.error(e?.response?.data?.message || 'خطا در ثبت ذی‌نفع');
}
};
const handleRefundSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!refundData.trackId) {
toast.error('شناسه تراکنش (Track ID) الزامی است');
return;
}
try {
setIsRefunding(true);
const res = await api.post('/payment/admin/refund', {
trackId: refundData.trackId,
amount: refundData.amount ? Number(refundData.amount) * 10 : undefined, // to Rials
tryReverse: refundData.tryReverse,
cardNumber: refundData.cardNumber || undefined,
description: refundData.description || undefined,
});
if (res.data?.result === 1 || res.data?.status) {
toast.success(res.data?.message || 'درخواست استرداد وجه با موفقیت انجام شد.');
setRefundData({ trackId: '', amount: '', tryReverse: false, cardNumber: '', description: '' });
} else {
toast.error(res.data?.message || 'خطا در انجام استرداد وجه');
}
} catch (e: any) {
toast.error(e?.response?.data?.message || 'خطا در ارسال درخواست استرداد');
} finally {
setIsRefunding(false);
}
};
return (
<div className="space-y-6 font-vazir" dir="rtl">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h2 className="text-2xl font-black text-gray-900 flex items-center gap-2">
<CreditCard className="w-6 h-6 text-indigo-600" />
مدیریت روش‌های پرداخت و درگاه بانکی
پلتفرم جامع پرداختی و درگاه‌های بانکی زیبال
</h2>
<p className="text-gray-500 font-medium mt-1">
پیکربندی پایانه‌های پرداخت اینترنتی زیبال، کیف پول داخلی، کارت به کارت و پرداخت در محل
<p className="text-gray-500 font-medium mt-1 text-xs sm:text-sm">
مدیریت درگاه IPG، استرداد وجه (Refund/Reverse)، بانکداری شرکتی (EBank)، کیف‌پول و تسویه
</p>
</div>
</div>
{/* Tabs */}
<div className="flex border-b border-gray-200 gap-2 overflow-x-auto pb-1">
<button
type="button"
onClick={() => setActiveTab('gateways')}
className={`px-4 py-2.5 rounded-xl font-bold text-xs sm:text-sm transition-all flex items-center gap-2 cursor-pointer ${
activeTab === 'gateways'
? 'bg-indigo-600 text-white shadow-md shadow-indigo-100'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<CreditCard className="w-4 h-4" />
تنظیمات درگاه و پایانه
</button>
<button
type="button"
onClick={() => {
setActiveTab('wallets');
fetchWallets();
}}
className={`px-4 py-2.5 rounded-xl font-bold text-xs sm:text-sm transition-all flex items-center gap-2 cursor-pointer ${
activeTab === 'wallets'
? 'bg-indigo-600 text-white shadow-md shadow-indigo-100'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<Wallet className="w-4 h-4" />
کیف‌پول‌های زیبال و ریفاند
</button>
<button
type="button"
onClick={() => {
setActiveTab('ebank');
fetchEBank();
}}
className={`px-4 py-2.5 rounded-xl font-bold text-xs sm:text-sm transition-all flex items-center gap-2 cursor-pointer ${
activeTab === 'ebank'
? 'bg-indigo-600 text-white shadow-md shadow-indigo-100'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<Building2 className="w-4 h-4" />
بانکداری شرکتی (EBank)
</button>
<button
type="button"
onClick={() => {
setActiveTab('submerchants');
fetchSubMerchants();
}}
className={`px-4 py-2.5 rounded-xl font-bold text-xs sm:text-sm transition-all flex items-center gap-2 cursor-pointer ${
activeTab === 'submerchants'
? 'bg-indigo-600 text-white shadow-md shadow-indigo-100'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<Users className="w-4 h-4" />
تسهیم و ذی‌نفعان
</button>
</div>
{isLoading ? (
<div className="flex justify-center p-12">
<Spinner size="lg" className="text-indigo-600" />
</div>
) : (
<form onSubmit={handleSave} className="space-y-6">
{/* Methods Cards */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-6">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<ShieldCheck className="w-5 h-5 text-indigo-600" />
وضعیت فعال بودن روش‌های پرداخت در تسویه‌حساب
</h3>
<>
{activeTab === 'gateways' && (
<form onSubmit={handleSave} className="space-y-6">
{/* Methods Cards */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-6">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<ShieldCheck className="w-5 h-5 text-indigo-600" />
وضعیت فعال بودن روش‌های پرداخت در تسویه‌حساب
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Zibal IPG */}
<div className="flex items-center justify-between p-4 bg-indigo-50/50 rounded-2xl border border-indigo-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-indigo-600 text-white flex items-center justify-center font-black">
Z
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Zibal IPG */}
<div className="flex items-center justify-between p-4 bg-indigo-50/50 rounded-2xl border border-indigo-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-indigo-600 text-white flex items-center justify-center font-black">
Z
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">درگاه آنلاین شتاب (زیبال)</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت اینترنتی کلیه کارت‌های عضو شتاب</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_ONLINE_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_ONLINE_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-indigo-600"></div>
</label>
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">درگاه آنلاین شتاب (زیبال)</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت اینترنتی کلیه کارت‌های عضو شتاب</p>
{/* Wallet Internal */}
<div className="flex items-center justify-between p-4 bg-blue-50/50 rounded-2xl border border-blue-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-blue-600 text-white flex items-center justify-center font-black">
<Wallet className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">اعتبار کیف پول کنینا</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت لحظه‌ای از موجودی شارژشده کاربر</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_WALLET_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_WALLET_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"></div>
</label>
</div>
{/* Card to Card */}
<div className="flex items-center justify-between p-4 bg-slate-50 rounded-2xl border border-gray-200">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-slate-700 text-white flex items-center justify-center font-black">
<ArrowLeftRight className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">کارت به کارت (واریز مستقیم)</h4>
<p className="text-xs text-gray-500 mt-0.5">واریز به شماره شبا / کارت رسمی و ثبت فیش</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_CARD_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_CARD_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-slate-700"></div>
</label>
</div>
{/* Cash On Delivery */}
<div className="flex items-center justify-between p-4 bg-amber-50/50 rounded-2xl border border-amber-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-amber-600 text-white flex items-center justify-center font-black">
<CreditCard className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">پرداخت در محل (COD)</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت با پوز هنگام دریافت سفارش</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_COD_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_COD_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-amber-600"></div>
</label>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_ONLINE_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_ONLINE_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-indigo-600"></div>
</label>
</div>
{/* Wallet Internal */}
<div className="flex items-center justify-between p-4 bg-blue-50/50 rounded-2xl border border-blue-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-blue-600 text-white flex items-center justify-center font-black">
<Wallet className="w-5 h-5" />
</div>
{/* Zibal IPG Configuration Card */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-5">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<h3 className="text-base font-black text-gray-900 flex items-center gap-2">
<ShieldCheck className="w-5 h-5 text-indigo-600" />
پیکربندی پایانه پرداخت زیبال (Zibal IPG Config)
</h3>
{settings.ZIBAL_MERCHANT === 'zibal' && (
<span className="bg-amber-100 text-amber-800 text-xs px-3 py-1 rounded-xl font-bold flex items-center gap-1">
<CheckCircle2 className="w-3.5 h-3.5" />
حالت آزمایشی (Sandbox)
</span>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<h4 className="font-bold text-gray-900 text-sm">اعتبار کیف پول کنینا</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت لحظه‌ای از موجودی شارژشده کاربر</p>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
کد مرچنت زیبال (Merchant ID) *
</label>
<input
type="text"
required
value={settings.ZIBAL_MERCHANT}
onChange={(e) => setSettings({ ...settings, ZIBAL_MERCHANT: e.target.value })}
placeholder="zibal یا کد مرچنت دریافتی"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
جهت فعال‌سازی درگاه زنده، مرچنت آی‌دی رسمی زیبال را در این کادر قرار دهید.
</p>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
دامنه بازگشت پس از پرداخت (Frontend Return Domain) *
</label>
<input
type="text"
required
value={settings.FRONTEND_URL}
onChange={(e) => setSettings({ ...settings, FRONTEND_URL: e.target.value })}
placeholder="https://canina.ir"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
آدرس صفحه‌ای که نتیجه تراکنش به آن ارجاع داده می‌شود.
</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_WALLET_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_WALLET_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"></div>
</label>
</div>
{/* Card to Card */}
<div className="flex items-center justify-between p-4 bg-slate-50 rounded-2xl border border-gray-200">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-slate-700 text-white flex items-center justify-center font-black">
<ArrowLeftRight className="w-5 h-5" />
</div>
{/* Zibal API Bearer Token & Corporate Banking Keys */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-5">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<Building2 className="w-5 h-5 text-indigo-600" />
کلید دسترسی پلتفرم زیبال و بانکداری شرکتی (API Token)
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<h4 className="font-bold text-gray-900 text-sm">کارت به کارت (واریز مستقیم)</h4>
<p className="text-xs text-gray-500 mt-0.5">واریز به شماره شبا / کارت رسمی و ثبت فیش</p>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
توکن دسترسی زیبال (Access Token / Bearer)
</label>
<input
type="password"
value={settings.ZIBAL_EBANK_TOKEN}
onChange={(e) => setSettings({ ...settings, ZIBAL_EBANK_TOKEN: e.target.value })}
placeholder="توکن دریافت شده از بخش توسعه‌دهندگان زیبال"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
جهت استفاده از قابلیت‌های ریفاند خودکار، کیف‌پول و بانکداری شرکتی.
</p>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
شناسه پیش‌فرض حساب پذیرنده (Default Account ID)
</label>
<input
type="text"
value={settings.ZIBAL_EBANK_ACCOUNT_ID}
onChange={(e) => setSettings({ ...settings, ZIBAL_EBANK_ACCOUNT_ID: e.target.value })}
placeholder="شناسه حساب در پنل بانکداری شرکتی زیبال"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
شناسه حساب متصل جهت کسر و انتقال وجه پایا و ریفاندها.
</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_CARD_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_CARD_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-slate-700"></div>
</label>
</div>
{/* Cash On Delivery */}
<div className="flex items-center justify-between p-4 bg-amber-50/50 rounded-2xl border border-amber-100">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-amber-600 text-white flex items-center justify-center font-black">
<CreditCard className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">پرداخت در محل (COD)</h4>
<p className="text-xs text-gray-500 mt-0.5">پرداخت با پوز هنگام دریافت سفارش</p>
</div>
<div className="flex justify-end">
<button
type="submit"
disabled={isSaving}
className="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-8 rounded-xl transition-colors flex items-center gap-2 shadow-md shadow-indigo-200 cursor-pointer text-sm"
>
{isSaving ? <Spinner size="sm" /> : <Save className="w-5 h-5" />}
<span>ذخیره کلیه تنظیمات درگاه زیبال</span>
</button>
</div>
</form>
)}
{activeTab === 'wallets' && (
<div className="space-y-6">
{/* Wallets Overview */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<h3 className="text-base font-black text-gray-900 flex items-center gap-2">
<Wallet className="w-5 h-5 text-indigo-600" />
موجودی کیف‌پول‌های متصل به حساب زیبال
</h3>
<button
onClick={fetchWallets}
disabled={loadingWallets}
className="text-xs font-bold text-indigo-600 hover:text-indigo-800 flex items-center gap-1.5 cursor-pointer"
>
<RefreshCw className={`w-3.5 h-3.5 ${loadingWallets ? 'animate-spin' : ''}`} />
بروزرسانی موجودی
</button>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={settings.PAY_GATEWAY_COD_ENABLE === 'true'}
onChange={(e) =>
setSettings({
...settings,
PAY_GATEWAY_COD_ENABLE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-amber-600"></div>
</label>
{wallets.length === 0 ? (
<p className="text-xs text-gray-500 py-4 text-center">
کیف‌پولی یافت نشد یا توکن دسترسی زیبال تنظیم نشده است.
</p>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
{wallets.map((w: any) => (
<div key={w.id} className="p-4 rounded-xl bg-slate-50 border border-gray-200 space-y-2">
<div className="flex justify-between items-center">
<span className="font-bold text-sm text-gray-900">{w.name}</span>
<span className="text-[10px] bg-indigo-100 text-indigo-700 px-2 py-0.5 rounded font-mono">
ID: {w.id}
</span>
</div>
<div className="pt-2 border-t border-gray-100 flex justify-between text-xs font-bold">
<span className="text-gray-500">موجودی کل:</span>
<span className="text-gray-900 font-mono">{(Number(w.balance) / 10).toLocaleString()} تومان</span>
</div>
<div className="flex justify-between text-xs font-bold">
<span className="text-gray-500">قابل برداشت:</span>
<span className="text-emerald-600 font-mono">
{(Number(w.withdrawableBalance) / 10).toLocaleString()} تومان
</span>
</div>
</div>
))}
</div>
)}
</div>
{/* Refund Tool */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<RotateCcw className="w-5 h-5 text-indigo-600" />
استرداد وجه تراکنش (Refund / Reverse)
</h3>
<form onSubmit={handleRefundSubmit} className="space-y-4">
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">شناسه تراکنش (Track ID) *</label>
<input
type="text"
required
value={refundData.trackId}
onChange={(e) => setRefundData({ ...refundData, trackId: e.target.value })}
placeholder="مثال: 2808993485"
className="w-full border border-gray-200 rounded-xl p-2.5 text-xs font-mono outline-none focus:ring-2 focus:ring-indigo-500"
dir="ltr"
/>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">مبلغ به تومان (اختیاری - خالی برای کل مبلغ)</label>
<input
type="number"
value={refundData.amount}
onChange={(e) => setRefundData({ ...refundData, amount: e.target.value })}
placeholder="کل مبلغ تراکنش"
className="w-full border border-gray-200 rounded-xl p-2.5 text-xs font-mono outline-none focus:ring-2 focus:ring-indigo-500"
dir="ltr"
/>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">شماره کارت مقصد (اختیاری)</label>
<input
type="text"
value={refundData.cardNumber}
onChange={(e) => setRefundData({ ...refundData, cardNumber: e.target.value })}
placeholder="شماره کارت ۱۶ رقمی"
className="w-full border border-gray-200 rounded-xl p-2.5 text-xs font-mono outline-none focus:ring-2 focus:ring-indigo-500"
dir="ltr"
/>
</div>
</div>
<div className="flex items-center gap-2 pt-2">
<input
type="checkbox"
id="tryReverse"
checked={refundData.tryReverse}
onChange={(e) => setRefundData({ ...refundData, tryReverse: e.target.checked })}
className="rounded text-indigo-600 focus:ring-indigo-500 cursor-pointer"
/>
<label htmlFor="tryReverse" className="text-xs font-bold text-gray-700 cursor-pointer">
اولویت با Reverse باشد (برگشت آنی قبل از تسویه شاپرک)
</label>
</div>
<div className="flex justify-end pt-2">
<button
type="submit"
disabled={isRefunding}
className="bg-rose-600 hover:bg-rose-700 text-white font-bold py-2.5 px-6 rounded-xl transition-colors flex items-center gap-2 cursor-pointer text-xs"
>
{isRefunding ? <Spinner size="sm" /> : <RotateCcw className="w-4 h-4" />}
<span>ارسال درخواست استرداد به زیبال</span>
</button>
</div>
</form>
</div>
</div>
</div>
)}
{/* Zibal Configuration Card */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-5">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<h3 className="text-base font-black text-gray-900 flex items-center gap-2">
<ShieldCheck className="w-5 h-5 text-indigo-600" />
پیکربندی پایانه پرداخت زیبال (Zibal IPG Config)
</h3>
{settings.ZIBAL_MERCHANT === 'zibal' && (
<span className="bg-amber-100 text-amber-800 text-xs px-3 py-1 rounded-xl font-bold flex items-center gap-1">
<CheckCircle2 className="w-3.5 h-3.5" />
حالت آزمایشی (Sandbox)
</span>
)}
</div>
{activeTab === 'ebank' && (
<div className="space-y-6">
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<h3 className="text-base font-black text-gray-900 flex items-center gap-2">
<Building2 className="w-5 h-5 text-indigo-600" />
حساب‌های متصل به بانکداری شرکتی زیبال
</h3>
<button
onClick={fetchEBank}
disabled={loadingEBank}
className="text-xs font-bold text-indigo-600 hover:text-indigo-800 flex items-center gap-1.5 cursor-pointer"
>
<RefreshCw className={`w-3.5 h-3.5 ${loadingEBank ? 'animate-spin' : ''}`} />
استعلام و بروزرسانی
</button>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
کد مرچنت زیبال (Merchant ID) *
</label>
<input
type="text"
required
value={settings.ZIBAL_MERCHANT}
onChange={(e) => setSettings({ ...settings, ZIBAL_MERCHANT: e.target.value })}
placeholder="zibal یا کد مرچنت دریافتی"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
جهت فعال‌سازی درگاه زنده، مرچنت آی‌دی رسمی زیبال را در این کادر قرار دهید.
</p>
</div>
{ebankBalance && (
<div className="p-4 bg-emerald-50 border border-emerald-200 rounded-xl flex items-center justify-between">
<div>
<p className="text-xs text-emerald-800 font-bold">موجودی لحظه‌ای حساب مبدا:</p>
<p className="text-lg font-black text-emerald-950 font-mono mt-1">
{(Number(ebankBalance.balance || 0) / 10).toLocaleString()} تومان
</p>
</div>
<span className="text-[11px] text-emerald-700 bg-white/80 px-3 py-1 rounded-lg font-medium">
بروزرسانی: {ebankBalance.jalaliBalanceUpdatedAt || 'لحظه‌ای'}
</span>
</div>
)}
<div>
<label className="block text-xs font-bold text-gray-700 mb-1.5">
دامنه بازگشت پس از پرداخت (Frontend Return Domain) *
</label>
<input
type="text"
required
value={settings.FRONTEND_URL}
onChange={(e) => setSettings({ ...settings, FRONTEND_URL: e.target.value })}
placeholder="https://canina.ir"
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-indigo-500 text-xs font-mono font-bold"
dir="ltr"
/>
<p className="text-[11px] text-gray-400 mt-1">
آدرس صفحه‌ای که نتیجه تراکنش به آن ارجاع داده می‌شود.
</p>
{ebankAccounts.length === 0 ? (
<p className="text-xs text-gray-500 py-4 text-center">
حسابی یافت نشد یا دسترسی بانکداری شرکتی فعال نیست.
</p>
) : (
<div className="divide-y divide-gray-100">
{ebankAccounts.map((acc: any) => (
<div key={acc.accountId} className="py-3 flex items-center justify-between">
<div>
<p className="text-sm font-bold text-gray-900">{acc.accountName || 'حساب شرکتی'}</p>
<p className="text-xs text-gray-500 font-mono mt-0.5">{acc.accountIban}</p>
</div>
<div className="text-left font-mono text-xs">
<span className="bg-slate-100 text-slate-700 px-2.5 py-1 rounded-md">
ID: {acc.accountId}
</span>
</div>
</div>
))}
</div>
)}
</div>
</div>
</div>
)}
<div className="flex justify-end">
<button
type="submit"
disabled={isSaving}
className="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-8 rounded-xl transition-colors flex items-center gap-2 shadow-md shadow-indigo-200 cursor-pointer"
>
{isSaving ? <Spinner size="sm" /> : <Save className="w-5 h-5" />}
<span>ذخیره تغییرات روش‌های پرداخت</span>
</button>
</div>
</form>
{activeTab === 'submerchants' && (
<div className="space-y-6">
{/* Add SubMerchant */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<Users className="w-5 h-5 text-indigo-600" />
تعریف ذی‌نفع جدید جهت تسهیم خودکار
</h3>
<form onSubmit={handleCreateSubMerchant} className="grid grid-cols-1 md:grid-cols-3 gap-4 items-end">
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">نام کامل صاحب حساب / ذی‌نفع *</label>
<input
type="text"
required
value={newSubMerchant.name}
onChange={(e) => setNewSubMerchant({ ...newSubMerchant, name: e.target.value })}
placeholder="مطابق با نام صاحب شبا"
className="w-full border border-gray-200 rounded-xl p-2.5 text-xs outline-none focus:ring-2 focus:ring-indigo-500"
/>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">شماره شبا (۲۴ رقمی با IR) *</label>
<input
type="text"
required
value={newSubMerchant.bankAccount}
onChange={(e) => setNewSubMerchant({ ...newSubMerchant, bankAccount: e.target.value })}
placeholder="IR120620000000246388553648"
className="w-full border border-gray-200 rounded-xl p-2.5 text-xs font-mono outline-none focus:ring-2 focus:ring-indigo-500"
dir="ltr"
/>
</div>
<button
type="submit"
className="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2.5 px-6 rounded-xl transition-colors text-xs cursor-pointer h-[42px]"
>
ثبت ذی‌نفع در زیبال
</button>
</form>
</div>
{/* List */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<h3 className="text-base font-black text-gray-900">لیست ذی‌نفعان ثبت‌شده</h3>
<button
onClick={fetchSubMerchants}
disabled={loadingSubMerchants}
className="text-xs font-bold text-indigo-600 hover:text-indigo-800 flex items-center gap-1.5 cursor-pointer"
>
<RefreshCw className={`w-3.5 h-3.5 ${loadingSubMerchants ? 'animate-spin' : ''}`} />
بروزرسانی
</button>
</div>
{subMerchants.length === 0 ? (
<p className="text-xs text-gray-500 py-4 text-center">ذی‌نفعی ثبت نشده است.</p>
) : (
<div className="divide-y divide-gray-100">
{subMerchants.map((sm: any) => (
<div key={sm.id} className="py-3 flex items-center justify-between">
<div>
<p className="text-sm font-bold text-gray-900">{sm.name}</p>
<p className="text-xs text-gray-500 font-mono mt-0.5">{sm.bankAccount}</p>
</div>
<span className="text-xs bg-slate-100 text-slate-700 px-2.5 py-1 rounded font-mono">
ID: {sm.id}
</span>
</div>
))}
</div>
)}
</div>
</div>
)}
</>
)}
</div>
);