refactor(payment): 渠道抽象成 Channel 接口 —— 接新渠道=加 adapter 不动骨架
PAYMENT_DESIGN §3 承诺的 internal/payment/channel.go 适配器接口此前不存在,微信硬编码在 manager/handler 里。补齐抽象: - channel.go:Channel 接口(Name/CreatePay/QueryOrder/VerifyCallback)+ 统一 PayIntent/PayResult + 渠道名常量。入参用基本类型不吃 *store.PaymentOrder,payment 包不反依赖 store。 - Wechat 实现 Channel(编译期断言 var _ Channel);QueryResult 归一为 PayResult;CreatePay 返回 PayIntent。 - Manager 从「持一个 *Wechat」改为渠道注册表:Get(name)/Available()/Status(name)/ReloadWechat; 按渠道名持有已装配实例,热重载不变。 - 回调路由收敛 /billing/callback/wechat → /billing/callback/:channel 按名路由(旧 notify URL 仍匹配); 查单/掉单补偿据 order.Channel 路由,不再写死微信。下单支持可选 channel(缺省 wechat)。 - 支付宝/Stripe 现在真·只差一个 adapter+注册。唯一未泛化:回调 ack 应答格式(现微信态,注释标明)。 - payment 包首个测试:Manager 注册表 4 用例(空/注册摘除/空配置/配置不全)。build/vet/test/lint 全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -21,10 +21,8 @@ func (h *Handler) BillingPacks(c *gin.Context) {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
channels := []string{store.ChannelRedeem}
|
||||
if h.pay.Current() != nil {
|
||||
channels = append(channels, store.ChannelWechat)
|
||||
}
|
||||
// 兑换码渠道始终可用(无需配置);真渠道由 Manager 报告哪些已启用。
|
||||
channels := append([]string{store.ChannelRedeem}, h.pay.Available()...)
|
||||
c.JSON(http.StatusOK, gin.H{"packs": packs, "channels": channels})
|
||||
}
|
||||
|
||||
@@ -33,21 +31,27 @@ func (h *Handler) BillingPacks(c *gin.Context) {
|
||||
// 价格可能已经改过,不让旧价格的单无限期可付。
|
||||
const orderTTL = 30 * time.Minute
|
||||
|
||||
// BillingCreateOrder: POST /api/v1/billing/orders {pack_id} —— 微信 Native 下单,返回 code_url。
|
||||
// 金额/积分由服务端按在售包锁定进订单行,前端只传包 id,不信任任何客户端金额。
|
||||
// BillingCreateOrder: POST /api/v1/billing/orders {pack_id, channel?} —— 真渠道下单,返回支付凭据。
|
||||
// channel 缺省 wechat(当前唯一真渠道);金额/积分由服务端按在售包锁定进订单行,前端只传包 id,
|
||||
// 不信任任何客户端金额。订单记下所用渠道,后续查单/回调/补偿据此路由。
|
||||
func (h *Handler) BillingCreateOrder(c *gin.Context) {
|
||||
wc := h.pay.Current()
|
||||
if wc == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "微信支付未配置,请用兑换码充值"})
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
PackID string `json:"pack_id"`
|
||||
PackID string `json:"pack_id"`
|
||||
Channel string `json:"channel"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil || strings.TrimSpace(b.PackID) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "pack_id 必填"})
|
||||
return
|
||||
}
|
||||
channel := strings.TrimSpace(b.Channel)
|
||||
if channel == "" {
|
||||
channel = store.ChannelWechat
|
||||
}
|
||||
ch := h.pay.Get(channel)
|
||||
if ch == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "渠道 " + channel + " 未启用,请换渠道或用兑换码充值"})
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
uid := userID(c)
|
||||
billing := h.db.ResolveBillingTenantID(ctx, uid, tenantID(c))
|
||||
@@ -63,20 +67,20 @@ func (h *Handler) BillingCreateOrder(c *gin.Context) {
|
||||
o := &store.PaymentOrder{
|
||||
TenantID: billing, UserID: uid, PackID: pk.ID,
|
||||
AmountFen: pk.PriceFen, CreditsMicro: pk.CreditsMicro,
|
||||
Channel: store.ChannelWechat, Status: store.OrderPending,
|
||||
Channel: channel, Status: store.OrderPending,
|
||||
}
|
||||
if err := h.db.CreateOrder(ctx, o); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
codeURL, err := wc.CreatePay(ctx, o.ID, "sundynix 积分充值 · "+pk.Name, pk.PriceFen)
|
||||
intent, err := ch.CreatePay(ctx, o.ID, "sundynix 积分充值 · "+pk.Name, pk.PriceFen)
|
||||
if err != nil {
|
||||
// 渠道下单失败的单直接作废,不留一堆永远付不了的 pending。
|
||||
_ = h.db.ExpireOrder(ctx, o.ID)
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "微信下单失败: " + err.Error()})
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": channel + "下单失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"order_id": o.ID, "code_url": codeURL, "amount_fen": o.AmountFen})
|
||||
c.JSON(http.StatusOK, gin.H{"order_id": o.ID, "code_url": intent.CodeURL, "amount_fen": o.AmountFen})
|
||||
}
|
||||
|
||||
// BillingOrderStatus: GET /api/v1/billing/orders/:id —— 前端轮询订单态。
|
||||
@@ -106,11 +110,11 @@ func (h *Handler) BillingOrderStatus(c *gin.Context) {
|
||||
// 渠道关单/超 TTL→过期。返回最新订单 + 是否金额不符(不符则不入账、留人工对账)。
|
||||
// 前端轮询与掉单补偿定时器共用这一份,避免两处「查单→落态」逻辑漂移。
|
||||
func (h *Handler) reconcileOrder(ctx context.Context, o *store.PaymentOrder) (*store.PaymentOrder, bool) {
|
||||
wc := h.pay.Current()
|
||||
if o.Status != store.OrderPending || wc == nil {
|
||||
ch := h.pay.Get(o.Channel) // 按订单实际所用渠道查单,不写死微信
|
||||
if o.Status != store.OrderPending || ch == nil {
|
||||
return o, false
|
||||
}
|
||||
if r, err := wc.QueryOrder(ctx, o.ID); err == nil {
|
||||
if r, err := ch.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 {
|
||||
@@ -130,15 +134,17 @@ func (h *Handler) reconcileOrder(ctx context.Context, o *store.PaymentOrder) (*s
|
||||
return o, false
|
||||
}
|
||||
|
||||
// WechatCallback: POST /api/v1/billing/callback/wechat —— 微信支付回调(公开路由,验签是唯一的门)。
|
||||
// 应答契约:入账成功/重复推送都回 200 {code:SUCCESS};验签失败 4xx;处理失败 5xx 让微信重试。
|
||||
func (h *Handler) WechatCallback(c *gin.Context) {
|
||||
wc := h.pay.Current()
|
||||
if wc == nil {
|
||||
// PaymentCallback: POST /api/v1/billing/callback/:channel —— 渠道支付回调(公开路由,验签是唯一的门)。
|
||||
// 按 :channel 路由到对应适配器验签。应答契约当前为微信形态({code:SUCCESS}/{code:FAIL}):
|
||||
// 入账成功/重复推送都回 200 SUCCESS;验签失败 4xx;处理失败 5xx 让渠道重试。
|
||||
// 注:其它渠道的 ack 格式不同(如支付宝要求纯文本 "success"),接入时按渠道分应答即可。
|
||||
func (h *Handler) PaymentCallback(c *gin.Context) {
|
||||
ch := h.pay.Get(c.Param("channel"))
|
||||
if ch == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"code": "FAIL", "message": "渠道未配置"})
|
||||
return
|
||||
}
|
||||
r, err := wc.VerifyCallback(c.Request)
|
||||
r, err := ch.VerifyCallback(c.Request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"code": "FAIL", "message": "验签失败"})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user