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:
@@ -21,7 +21,7 @@ func (h *Handler) BillingPacks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
channels := []string{store.ChannelRedeem}
|
||||
if h.wechat != nil {
|
||||
if h.pay.Current() != nil {
|
||||
channels = append(channels, store.ChannelWechat)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"packs": packs, "channels": channels})
|
||||
@@ -35,7 +35,8 @@ const orderTTL = 30 * time.Minute
|
||||
// BillingCreateOrder: POST /api/v1/billing/orders {pack_id} —— 微信 Native 下单,返回 code_url。
|
||||
// 金额/积分由服务端按在售包锁定进订单行,前端只传包 id,不信任任何客户端金额。
|
||||
func (h *Handler) BillingCreateOrder(c *gin.Context) {
|
||||
if h.wechat == nil {
|
||||
wc := h.pay.Current()
|
||||
if wc == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "微信支付未配置,请用兑换码充值"})
|
||||
return
|
||||
}
|
||||
@@ -67,7 +68,7 @@ func (h *Handler) BillingCreateOrder(c *gin.Context) {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
codeURL, err := h.wechat.CreatePay(ctx, o.ID, "sundynix 积分充值 · "+pk.Name, pk.PriceFen)
|
||||
codeURL, err := wc.CreatePay(ctx, o.ID, "sundynix 积分充值 · "+pk.Name, pk.PriceFen)
|
||||
if err != nil {
|
||||
// 渠道下单失败的单直接作废,不留一堆永远付不了的 pending。
|
||||
_ = h.db.ExpireOrder(ctx, o.ID)
|
||||
@@ -92,8 +93,8 @@ func (h *Handler) BillingOrderStatus(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "订单不存在"})
|
||||
return
|
||||
}
|
||||
if o.Status == store.OrderPending && h.wechat != nil {
|
||||
if r, err := h.wechat.QueryOrder(ctx, o.ID); err == nil {
|
||||
if wc := h.pay.Current(); o.Status == store.OrderPending && wc != nil {
|
||||
if r, err := wc.QueryOrder(ctx, o.ID); err == nil {
|
||||
switch {
|
||||
case r.Paid && r.AmountFen == o.AmountFen:
|
||||
if _, err := h.db.MarkOrderPaid(ctx, o.ID, r.ChannelTxn); err == nil {
|
||||
@@ -118,11 +119,12 @@ func (h *Handler) BillingOrderStatus(c *gin.Context) {
|
||||
// WechatCallback: POST /api/v1/billing/callback/wechat —— 微信支付回调(公开路由,验签是唯一的门)。
|
||||
// 应答契约:入账成功/重复推送都回 200 {code:SUCCESS};验签失败 4xx;处理失败 5xx 让微信重试。
|
||||
func (h *Handler) WechatCallback(c *gin.Context) {
|
||||
if h.wechat == nil {
|
||||
wc := h.pay.Current()
|
||||
if wc == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"code": "FAIL", "message": "渠道未配置"})
|
||||
return
|
||||
}
|
||||
r, err := h.wechat.VerifyCallback(c.Request)
|
||||
r, err := wc.VerifyCallback(c.Request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"code": "FAIL", "message": "验签失败"})
|
||||
return
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sundynix/sundynix-gateway/internal/payment"
|
||||
"github.com/sundynix/sundynix-shared/secrets"
|
||||
)
|
||||
|
||||
// 微信支付配置控制面:配置进 DB(sundynix_setting)、改完热生效不重启;
|
||||
// APIv3 密钥入库前 AES-GCM 加密(与模型 API Key 同一把 SUNDYNIX_SECRET_KEY),
|
||||
// 私钥文件留在服务器磁盘,库里只存路径。env 仍作兜底(DB 优先 → env → 隐藏)。
|
||||
|
||||
// SettingWechatPay 是 settings KV 里的键,值为 payment.Config 的 JSON(apiv3_key 为密文)。
|
||||
const SettingWechatPay = "payment_wechat"
|
||||
|
||||
// loadWechatConfig 读支付配置:DB 优先(解密 apiv3),空则回退 env。
|
||||
func (h *Handler) loadWechatConfig(ctx context.Context) payment.Config {
|
||||
raw := h.db.GetSetting(ctx, SettingWechatPay)
|
||||
if raw == "" {
|
||||
return payment.ConfigFromEnv()
|
||||
}
|
||||
var c payment.Config
|
||||
if err := json.Unmarshal([]byte(raw), &c); err != nil {
|
||||
return payment.ConfigFromEnv()
|
||||
}
|
||||
if c.APIv3Key != "" {
|
||||
if plain, err := secrets.Decrypt(c.APIv3Key); err == nil {
|
||||
c.APIv3Key = plain
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// InitWechat 启动时装配微信渠道(DB 优先 → env)。失败只降级隐藏,不阻断启动。
|
||||
func (h *Handler) InitWechat(ctx context.Context) {
|
||||
_ = h.pay.Reload(ctx, h.loadWechatConfig(ctx))
|
||||
}
|
||||
|
||||
// AdminGetWechatPay: GET /api/v1/admin/payment/wechat —— 当前配置(密钥不回显)+ 渠道状态。
|
||||
func (h *Handler) AdminGetWechatPay(c *gin.Context) {
|
||||
cfg := h.loadWechatConfig(c.Request.Context())
|
||||
enabled, reason := h.pay.Status()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"config": gin.H{
|
||||
"mchid": cfg.MchID,
|
||||
"cert_serial": cfg.CertSerial,
|
||||
"private_key_path": cfg.PrivateKeyPath,
|
||||
"appid": cfg.AppID,
|
||||
"notify_url": cfg.NotifyURL,
|
||||
"has_apiv3_key": cfg.APIv3Key != "", // 密钥只报有无,明文永不回显
|
||||
},
|
||||
"enabled": enabled,
|
||||
"reason": reason,
|
||||
})
|
||||
}
|
||||
|
||||
// AdminSaveWechatPay: PUT /api/v1/admin/payment/wechat —— 保存配置并热重载渠道。
|
||||
// apiv3_key 留空 = 沿用已存的(只写不回显的编辑语义,同模型 Key)。
|
||||
func (h *Handler) AdminSaveWechatPay(c *gin.Context) {
|
||||
var b struct {
|
||||
MchID string `json:"mchid"`
|
||||
CertSerial string `json:"cert_serial"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
APIv3Key string `json:"apiv3_key"`
|
||||
AppID string `json:"appid"`
|
||||
NotifyURL string `json:"notify_url"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
apiv3 := strings.TrimSpace(b.APIv3Key)
|
||||
if apiv3 == "" {
|
||||
apiv3 = h.loadWechatConfig(ctx).APIv3Key // 留空沿用旧密钥(此处为明文,稍后统一加密入库)
|
||||
}
|
||||
cfg := payment.Config{
|
||||
MchID: strings.TrimSpace(b.MchID),
|
||||
CertSerial: strings.TrimSpace(b.CertSerial),
|
||||
PrivateKeyPath: strings.TrimSpace(b.PrivateKeyPath),
|
||||
APIv3Key: apiv3,
|
||||
AppID: strings.TrimSpace(b.AppID),
|
||||
NotifyURL: strings.TrimSpace(b.NotifyURL),
|
||||
}
|
||||
stored := cfg
|
||||
if stored.APIv3Key != "" {
|
||||
enc, err := secrets.Encrypt(stored.APIv3Key)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "密钥加密失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
stored.APIv3Key = enc
|
||||
}
|
||||
raw, _ := json.Marshal(stored)
|
||||
if err := h.db.SetSetting(ctx, SettingWechatPay, string(raw)); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
// 热重载:配置有毛病只会让渠道隐藏并给出原因,不影响其它功能。
|
||||
_ = h.pay.Reload(ctx, cfg)
|
||||
enabled, reason := h.pay.Status()
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "enabled": enabled, "reason": reason})
|
||||
}
|
||||
@@ -24,21 +24,15 @@ import (
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
db *store.Postgres
|
||||
cache *store.Redis
|
||||
bus *nats.Bus
|
||||
blob *blob.Store
|
||||
wechat *payment.Wechat // 微信支付渠道;nil=未配置(渠道隐藏)
|
||||
db *store.Postgres
|
||||
cache *store.Redis
|
||||
bus *nats.Bus
|
||||
blob *blob.Store
|
||||
pay *payment.Manager // 微信支付渠道管理器(DB 配置热重载;Current()==nil 即渠道隐藏)
|
||||
}
|
||||
|
||||
func New(db *store.Postgres, cache *store.Redis, bus *nats.Bus, blob *blob.Store) *Handler {
|
||||
return &Handler{db: db, cache: cache, bus: bus, blob: blob}
|
||||
}
|
||||
|
||||
// WithWechat 注入微信支付渠道(nil 安全:保持隐藏)。
|
||||
func (h *Handler) WithWechat(w *payment.Wechat) *Handler {
|
||||
h.wechat = w
|
||||
return h
|
||||
return &Handler{db: db, cache: cache, bus: bus, blob: blob, pay: payment.NewManager()}
|
||||
}
|
||||
|
||||
// preflight 是「会烧钱的执行」提交前的统一关卡:当日 token 预算 → 计费租户 → 积分硬拦截。
|
||||
|
||||
Reference in New Issue
Block a user