| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- '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<void>;
- }
- 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 (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4 font-mono">
- <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-xl max-h-[90vh] flex flex-col overflow-hidden animate-in zoom-in-95 duration-200">
-
- {/* Header */}
- <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
- <div className="flex items-center gap-2 text-xs font-bold uppercase tracking-wider">
- <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
- <span>[ {isEditing ? 'EDIT GATEWAY' : 'NEW GATEWAY'} // 支付服务商 ]</span>
- </div>
- <button
- onClick={onClose}
- className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
- >
- ✕
- </button>
- </div>
- {/* Form Content */}
- <form onSubmit={handleSubmit} className="p-5 overflow-y-auto space-y-4 bg-white flex-1">
-
- {/* Preset Buttons */}
- {!isEditing && (
- <div className="bg-neutral-50 p-3 border-2 border-black shadow-[2px_2px_0px_#000] space-y-2">
- <span className="text-xs font-black text-black uppercase block">
- [ 快速预设模板 // 一键填充 ]:
- </span>
- <div className="grid grid-cols-2 sm:grid-cols-3 gap-1.5">
- {PAYMENT_PRESETS.map((preset) => {
- const isSelected = form.name === preset.name;
- return (
- <button
- key={preset.name}
- type="button"
- onClick={() => applyPreset(preset)}
- className={`p-1.5 border text-xs font-bold transition active:translate-x-0.5 active:translate-y-0.5 truncate text-left ${
- isSelected
- ? 'bg-amber-300 border-black text-black shadow-[1px_1px_0px_#000]'
- : 'bg-white border-neutral-300 hover:border-black text-black'
- }`}
- >
- {preset.name}
- </button>
- );
- })}
- </div>
- </div>
- )}
- {/* Form Fields */}
- <div className="grid grid-cols-1 sm:grid-cols-2 gap-3 text-xs">
- <div className="space-y-1">
- <label className="font-bold text-black uppercase">[ 01 // 内部标识名 (Code) ]:</label>
- <input
- type="text"
- required
- disabled={isEditing}
- placeholder="例如: wechat, stripe"
- value={form.name}
- onChange={(e) => 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]"
- />
- </div>
- <div className="space-y-1">
- <label className="font-bold text-black uppercase">[ 02 // 前台显示名称 ]:</label>
- <input
- type="text"
- required
- placeholder="例如: 微信支付, 国际信用卡"
- value={form.title}
- onChange={(e) => 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]"
- />
- </div>
- <div className="space-y-1">
- <label className="font-bold text-black uppercase">[ 03 // 结算币种 ]:</label>
- <input
- type="text"
- required
- placeholder="CNY, USD, EUR..."
- value={form.currency}
- onChange={(e) => 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]"
- />
- </div>
- <div className="space-y-1">
- <label className="font-bold text-black uppercase">[ 04 // 接入通道类型 ]:</label>
- <select
- value={form.channel}
- onChange={(e) => setForm({ ...form, channel: e.target.value })}
- className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-bold outline-none text-black shadow-[2px_2px_0px_#000]"
- >
- <option value="qr_static">静态二维码 (QR Static)</option>
- <option value="online_link">在线直连网关 (Online Link)</option>
- </select>
- </div>
- </div>
- <div className="space-y-1">
- <ImageUploadField
- label="[ 05 // 渠道图标(本地上传至 R2) ]:"
- value={form.icon || ''}
- onChange={(url) => setForm({ ...form, icon: url })}
- prefix="icons"
- aspectRatio="1:1"
- compact={true}
- helperText="选择本地 PNG, SVG, JPG 图标 (最大 1MB),留空默认使用内置矢量图标"
- />
- </div>
- <div className="pt-2 flex justify-end gap-3 border-t-2 border-dashed border-neutral-300">
- <button
- type="button"
- onClick={onClose}
- className="px-4 py-2 border-2 border-black bg-white hover:bg-neutral-100 text-black text-xs font-bold shadow-[2px_2px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- [ 取消 ]
- </button>
- <button
- type="submit"
- disabled={loading}
- className="px-6 py-2 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black text-xs font-black shadow-[3px_3px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 uppercase disabled:opacity-50"
- >
- {loading ? <Loader2 size={13} className="animate-spin" /> : null}
- <span>[ 保存渠道设置 ]</span>
- </button>
- </div>
- </form>
- </div>
- </div>
- );
- }
|