'use client'; import { useState, useEffect } from 'react'; import { Loader2, X, Sparkles, CreditCard, QrCode, Globe, Layers } from 'lucide-react'; import { Provider } from '@/types'; import { toast } from 'react-hot-toast'; import { DEFAULT_PAYMENT_ICONS, getPaymentProviderIcon } from './paymentIcons'; import { ImageUploadField } from '@/components/common/ImageUploadField'; interface ProviderModalProps { isOpen: boolean; onClose: () => void; provider?: Provider | null; onSubmit: (data: any) => Promise; } const PAYMENT_PRESETS = [ { name: 'wechat', title: '微信支付 (WeChat Pay)', channel: 'qr_static', currency: 'CNY', icon: DEFAULT_PAYMENT_ICONS.wechat, }, { name: 'alipay', title: '支付宝 (Alipay)', channel: 'qr_static', currency: 'CNY', icon: DEFAULT_PAYMENT_ICONS.alipay, }, { name: 'creem', title: 'Creem (AI Agent Payment)', channel: 'online_link', currency: 'USD', icon: DEFAULT_PAYMENT_ICONS.creem, }, { name: 'stripe', title: 'Stripe 国际信用卡', channel: 'online_link', currency: 'USD', icon: DEFAULT_PAYMENT_ICONS.stripe, }, { name: 'paypal', title: 'PayPal 快捷支付', channel: 'online_link', currency: 'USD', icon: DEFAULT_PAYMENT_ICONS.paypal, }, { name: 'usdt_trc20', title: 'USDT (TRC20 静态地址)', channel: 'qr_static', currency: 'USD', icon: DEFAULT_PAYMENT_ICONS.usdt_trc20, }, { name: 'bank_transfer', title: '银行转账 (对公/对私)', channel: 'qr_static', currency: 'CNY', icon: DEFAULT_PAYMENT_ICONS.bank_transfer, } ]; export default function ProviderModal({ isOpen, onClose, provider, onSubmit }: ProviderModalProps) { const [loading, setLoading] = useState(false); const [form, setForm] = useState({ name: '', title: '', icon: '', currency: 'CNY', channel: 'qr_static', enabled: 1 }); const isEditing = Boolean(provider?.id); useEffect(() => { if (isOpen) { if (provider) { setForm({ name: provider.name || '', title: provider.title || '', icon: getPaymentProviderIcon(provider.name, provider.icon), currency: provider.currency || 'CNY', channel: provider.channel || 'qr_static', enabled: Number(provider.enabled ?? 1) }); } else { setForm({ name: '', title: '', icon: '', currency: 'CNY', channel: 'qr_static', enabled: 1 }); } } }, [isOpen, provider]); const applyPreset = (preset: typeof PAYMENT_PRESETS[0]) => { setForm(prev => ({ ...prev, name: preset.name, title: preset.title, icon: preset.icon, channel: preset.channel, currency: preset.currency })); }; const handleNameChange = (val: string) => { const nextName = val.toLowerCase(); if (!form.icon || Object.values(DEFAULT_PAYMENT_ICONS).includes(form.icon)) { const matched = getPaymentProviderIcon(nextName); setForm(prev => ({ ...prev, name: nextName, icon: matched || prev.icon })); } else { setForm(prev => ({ ...prev, name: nextName })); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!form.name.trim() || !form.title.trim()) { toast.error('请填写内部标识和显示名称'); return; } const finalIcon = form.icon || getPaymentProviderIcon(form.name); setLoading(true); try { await onSubmit({ ...form, icon: finalIcon, id: provider?.id }); onClose(); } catch { // ignore } finally { setLoading(false); } }; if (!isOpen) return null; return (
{/* Header */}
[ {isEditing ? 'EDIT GATEWAY' : 'NEW GATEWAY'} // 支付服务商 ]
{/* Form Content */}
{/* Preset Buttons */} {!isEditing && (
[ 快速预设模板 // 一键填充 ]:
{PAYMENT_PRESETS.map((preset) => { const isSelected = form.name === preset.name; return ( ); })}
)} {/* Form Fields */}
handleNameChange(e.target.value)} className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-mono focus:bg-white outline-none disabled:bg-neutral-200 text-black shadow-[2px_2px_0px_#000]" />
setForm({ ...form, title: e.target.value })} className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-sans focus:bg-white outline-none text-black shadow-[2px_2px_0px_#000]" />
setForm({ ...form, currency: e.target.value.toUpperCase() })} className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-mono font-bold focus:bg-white outline-none text-black shadow-[2px_2px_0px_#000]" />
setForm({ ...form, icon: url })} prefix="icons" aspectRatio="1:1" compact={true} helperText="选择本地 PNG, SVG, JPG 图标 (最大 1MB),留空默认使用内置矢量图标" />
); }