feat(billing): 微信支付配置进 DB —— admin 控制面保存即热生效
用户定的形态:配置存数据库、密钥文件放服务器磁盘(库里只存路径)。 env 降级为兜底(DB 优先 → env → 隐藏,与 tokens_per_credit 同一约定)。 - payment 包重构:Config(6 字段)+ Manager(RWMutex 热重载,学 prompt 控制面 改完即生效不重启);未启用原因人话化(未配置/缺哪些字段/初始化失败具体错)。 - APIv3 密钥入库前 AES-GCM 加密(shared/secrets,与模型 API Key 同一把 SUNDYNIX_SECRET_KEY);GET 只回 has_apiv3_key 不回显;PUT 留空=沿用旧密钥 (只写不回显语义,同模型 Key)。 - admin GET/PUT /admin/payment/wechat;业务路径全部改经 Manager.Current() 取快照(BillingPacks/下单/查单/回调)。 - admin 计费页「微信支付配置」卡片:状态徽章(已启用/未启用+原因)+六字段 +保存并热生效。 live:无配置→「未配置」;存假配置→热重载报「私钥加载失败:decode err」; 去掉 appid→「配置不全,缺: appid」;密钥留空沿用(has_apiv3_key 保持 true); psql 复核库内密文 enc:1: 前缀、不含明文子串。go/tsc/41 vitest 全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -226,6 +226,38 @@ export async function listRedeemCodes(): Promise<RedeemCodeRow[]> {
|
||||
return d.codes ?? [];
|
||||
}
|
||||
|
||||
// ---- 微信支付配置(DB 存储、热生效;APIv3 密钥密文入库、不回显)----
|
||||
export interface WechatPayConfig {
|
||||
mchid: string;
|
||||
cert_serial: string;
|
||||
private_key_path: string;
|
||||
appid: string;
|
||||
notify_url: string;
|
||||
has_apiv3_key: boolean;
|
||||
}
|
||||
|
||||
export async function getWechatPay(): Promise<{ config: WechatPayConfig; enabled: boolean; reason: string }> {
|
||||
const res = guard(await fetch(`${ADMIN}/payment/wechat`, { headers: authHeaders() }));
|
||||
const d = (await res.json().catch(() => ({}))) as { config?: WechatPayConfig; enabled?: boolean; reason?: string; error?: string };
|
||||
if (!res.ok) throw new Error(d.error ?? `load failed: ${res.status}`);
|
||||
return { config: d.config!, enabled: !!d.enabled, reason: d.reason ?? "" };
|
||||
}
|
||||
|
||||
// saveWechatPay:apiv3_key 传空串 = 沿用已保存的密钥。返回热重载后的渠道状态。
|
||||
export async function saveWechatPay(body: {
|
||||
mchid: string;
|
||||
cert_serial: string;
|
||||
private_key_path: string;
|
||||
apiv3_key: string;
|
||||
appid: string;
|
||||
notify_url: string;
|
||||
}): Promise<{ enabled: boolean; reason: string }> {
|
||||
const res = guard(await fetch(`${ADMIN}/payment/wechat`, { method: "PUT", headers: authHeaders(true), body: JSON.stringify(body) }));
|
||||
const d = (await res.json().catch(() => ({}))) as { enabled?: boolean; reason?: string; error?: string };
|
||||
if (!res.ok) throw new Error(d.error ?? `save failed: ${res.status}`);
|
||||
return { enabled: !!d.enabled, reason: d.reason ?? "" };
|
||||
}
|
||||
|
||||
// gatewayOnline 用公开的 /healthz 探活(不受鉴权影响)。
|
||||
export async function gatewayOnline(): Promise<boolean> {
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { adminPacks, savePack, genRedeemCodes, listRedeemCodes, type CreditPack, type RedeemCodeRow } from "../api";
|
||||
import { adminPacks, savePack, genRedeemCodes, listRedeemCodes, getWechatPay, saveWechatPay, type CreditPack, type RedeemCodeRow, type WechatPayConfig } from "../api";
|
||||
|
||||
// 充值渠道(P5.1,设计见 PAYMENT_DESIGN.md):
|
||||
// - 积分包:钱→积分的第一层汇率(第二层 积分→token 在上方「计费规则」里,两层解耦)。
|
||||
@@ -14,12 +14,114 @@ export function TopupChannels() {
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2 pt-1">
|
||||
<h3 className="text-sm font-semibold text-gray-700">充值渠道</h3>
|
||||
<span className="text-[11px] text-gray-400">兑换码即刻可用;微信扫码支付(P5.2)待商户号</span>
|
||||
<span className="text-[11px] text-gray-400">兑换码即刻可用;微信扫码支付在下方配好商户号即点亮</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<RedeemBlock />
|
||||
<PacksBlock />
|
||||
</div>
|
||||
<WechatConfigBlock />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ---- 微信支付配置:DB 存储、保存即热生效(不重启 gateway)。
|
||||
// APIv3 密钥只写不回显(密文入库,与模型 API Key 同一把密钥加密);
|
||||
// 商户证书私钥文件放服务器磁盘,这里只填路径。
|
||||
function WechatConfigBlock() {
|
||||
const empty: WechatPayConfig = { mchid: "", cert_serial: "", private_key_path: "", appid: "", notify_url: "", has_apiv3_key: false };
|
||||
const [cfg, setCfg] = useState<WechatPayConfig>(empty);
|
||||
const [apiv3, setApiv3] = useState(""); // 留空=沿用已存
|
||||
const [enabled, setEnabled] = useState(false);
|
||||
const [reason, setReason] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [err, setErr] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
getWechatPay()
|
||||
.then((r) => {
|
||||
setCfg(r.config);
|
||||
setEnabled(r.enabled);
|
||||
setReason(r.reason);
|
||||
})
|
||||
.catch((e) => setErr((e as Error).message));
|
||||
}, []);
|
||||
|
||||
const save = async () => {
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
setErr("");
|
||||
try {
|
||||
const r = await saveWechatPay({
|
||||
mchid: cfg.mchid,
|
||||
cert_serial: cfg.cert_serial,
|
||||
private_key_path: cfg.private_key_path,
|
||||
apiv3_key: apiv3, // 空串=后端沿用旧密钥
|
||||
appid: cfg.appid,
|
||||
notify_url: cfg.notify_url,
|
||||
});
|
||||
setEnabled(r.enabled);
|
||||
setReason(r.reason);
|
||||
setApiv3("");
|
||||
if (apiv3) setCfg((c) => ({ ...c, has_apiv3_key: true }));
|
||||
} catch (e) {
|
||||
setErr((e as Error).message);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const field = (label: string, key: keyof WechatPayConfig, placeholder: string, cls = "") => (
|
||||
<label className={`text-xs text-gray-500 ${cls}`}>
|
||||
{label}
|
||||
<input
|
||||
value={String(cfg[key] ?? "")}
|
||||
onChange={(e) => setCfg((c) => ({ ...c, [key]: e.target.value }))}
|
||||
placeholder={placeholder}
|
||||
className="mt-1 block w-full rounded-lg border border-gray-200 px-2.5 py-1.5 font-mono text-sm text-gray-800 focus:border-violet-400 focus:outline-none"
|
||||
/>
|
||||
</label>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-gray-100 bg-white p-5 shadow-sm">
|
||||
<div className="mb-3 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<h4 className="text-sm font-semibold text-gray-700">微信支付配置</h4>
|
||||
{enabled ? (
|
||||
<span className="rounded bg-emerald-50 px-2 py-0.5 text-[10px] font-medium text-emerald-600">渠道已启用</span>
|
||||
) : (
|
||||
<span className="rounded bg-gray-100 px-2 py-0.5 text-[10px] text-gray-500" title={reason}>未启用{reason ? ` · ${reason}` : ""}</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-[11px] text-gray-400">保存即热生效;私钥文件放服务器磁盘,此处只填路径</span>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
{field("商户号 (mchid)", "mchid", "190000****")}
|
||||
{field("API 证书序列号", "cert_serial", "5157F09E…")}
|
||||
{field("appid(公众号/小程序)", "appid", "wx88888888")}
|
||||
{field("商户私钥文件路径(服务器磁盘)", "private_key_path", "/etc/sundynix/wechat/apiclient_key.pem", "md:col-span-2")}
|
||||
<label className="text-xs text-gray-500">
|
||||
APIv3 密钥{cfg.has_apiv3_key && <span className="ml-1 text-emerald-600">已保存</span>}
|
||||
<input
|
||||
type="password"
|
||||
value={apiv3}
|
||||
onChange={(e) => setApiv3(e.target.value)}
|
||||
placeholder={cfg.has_apiv3_key ? "留空则沿用已保存的" : "32 字节"}
|
||||
className="mt-1 block w-full rounded-lg border border-gray-200 px-2.5 py-1.5 font-mono text-sm text-gray-800 focus:border-violet-400 focus:outline-none"
|
||||
/>
|
||||
</label>
|
||||
{field("支付回调地址(公网 https)", "notify_url", "https://api.example.com/api/v1/billing/callback/wechat", "md:col-span-3")}
|
||||
</div>
|
||||
{err && <p className="mt-2 text-xs text-rose-500">{err}</p>}
|
||||
<div className="mt-3 flex items-center gap-3">
|
||||
<button onClick={() => void save()} disabled={busy}
|
||||
className="rounded-lg bg-violet-600 px-3.5 py-1.5 text-sm text-white hover:bg-violet-700 disabled:opacity-40">
|
||||
{busy ? "保存中…" : "保存并热生效"}
|
||||
</button>
|
||||
<span className="text-[11px] text-gray-400">收不到公网回调也能到账(用户端轮询会主动查单),回调只是更快的通道。</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user