feat(billing): 支付 P5.2 —— 微信支付 Native 渠道(扫码充值)
wechatpay-go v0.2.21。凭据全 env 注入(WECHAT_MCHID/MCH_CERT_SERIAL/
MCH_PRIVATE_KEY/APIV3_KEY/APPID/NOTIFY_URL),缺一渠道即隐藏——半配置/假凭据
只打日志不拖垮 gateway(用假私钥实测过降级)。
- internal/payment:Native 下单出 code_url、APIv3 回调验签解密、主动查单,
三者统一收敛为 QueryResult。
- 下单 POST /billing/orders {pack_id}(≥member+审计):金额/积分按在售包服务端
锁定进订单行,不信任客户端;渠道下单失败即作废,不留付不了的 pending。
- 到账两条路汇入同一个 MarkOrderPaid 幂等闸(CAS+唯一索引双闸,同 P5.1):
①公开回调路由(验签是唯一的门;金额与订单不符不入账);②前端轮询的
GET /billing/orders/:id 在 pending 时顺路主动查单——本地/内网收不到公网
回调也能确认到账,回调只是生产更快的通道。pending 超 30 分钟置 expired。
- Web 面:在售包卡片(渠道亮才出现)→扫码弹窗(qrcode 画 code_url,二维码底色
固定纯白——暗色主题下低对比码扫不出来)→2.5s 轮询→到账 toast+刷余额。
验证:go/tsc/vitest 全绿;无凭据+假凭据两种降级 live 四连
(channels 只剩 redeem/下单 400 引导兑换码/回调 503/兑换码闭环不受影响)。
⚠️ 真通道(prepay→扫码→回调/查单→入账)需真实商户号,未 live——用户配好
env 后用小额包实测,建议先配 ¥0.01 测试包走一单。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sundynix/sundynix-gateway/internal/store"
|
||||
@@ -12,14 +13,140 @@ import (
|
||||
// 入账目标一律是「计费租户」(ResolveBillingTenantID)——和消耗记账同一本账,
|
||||
// 谁的池子扣钱就往谁的池子充,别让用户充进一个花不到的池。
|
||||
|
||||
// BillingPacks: GET /api/v1/billing/packs —— 在售积分包(微信渠道 P5.2 上线前仅展示)。
|
||||
// BillingPacks: GET /api/v1/billing/packs —— 在售积分包 + 可用渠道(wechat 配了 env 才亮)。
|
||||
func (h *Handler) BillingPacks(c *gin.Context) {
|
||||
packs, err := h.db.ActivePacks(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"packs": packs, "channels": []string{store.ChannelRedeem}})
|
||||
channels := []string{store.ChannelRedeem}
|
||||
if h.wechat != nil {
|
||||
channels = append(channels, store.ChannelWechat)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"packs": packs, "channels": channels})
|
||||
}
|
||||
|
||||
// orderTTL 待支付订单的有效期:过期后前端轮询会把它置 expired,不再确认到账。
|
||||
// 微信 Native 的 code_url 本身约 2 小时有效,这里收紧到 30 分钟——挂太久的单
|
||||
// 价格可能已经改过,不让旧价格的单无限期可付。
|
||||
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 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "微信支付未配置,请用兑换码充值"})
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
PackID string `json:"pack_id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil || strings.TrimSpace(b.PackID) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "pack_id 必填"})
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
uid := userID(c)
|
||||
billing := h.db.ResolveBillingTenantID(ctx, uid, tenantID(c))
|
||||
if billing == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无计费租户上下文"})
|
||||
return
|
||||
}
|
||||
pk, err := h.db.GetPack(ctx, b.PackID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "积分包不存在或已下架"})
|
||||
return
|
||||
}
|
||||
o := &store.PaymentOrder{
|
||||
TenantID: billing, UserID: uid, PackID: pk.ID,
|
||||
AmountFen: pk.PriceFen, CreditsMicro: pk.CreditsMicro,
|
||||
Channel: store.ChannelWechat, Status: store.OrderPending,
|
||||
}
|
||||
if err := h.db.CreateOrder(ctx, o); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
codeURL, err := h.wechat.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()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"order_id": o.ID, "code_url": codeURL, "amount_fen": o.AmountFen})
|
||||
}
|
||||
|
||||
// BillingOrderStatus: GET /api/v1/billing/orders/:id —— 前端轮询订单态。
|
||||
// pending 时顺路主动查单确认(本地/内网收不到公网回调也能到账——回调只是生产更快的通道,
|
||||
// 两条路汇入同一个 MarkOrderPaid 幂等闸);超过 TTL 置 expired。
|
||||
func (h *Handler) BillingOrderStatus(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
o, err := h.db.GetOrder(ctx, c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "订单不存在"})
|
||||
return
|
||||
}
|
||||
// 只允许看自己计费租户的单(订单表未挂租户插件,这里显式校验)。
|
||||
if o.TenantID != h.db.ResolveBillingTenantID(ctx, userID(c), tenantID(c)) {
|
||||
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 {
|
||||
switch {
|
||||
case r.Paid && r.AmountFen == o.AmountFen:
|
||||
if _, err := h.db.MarkOrderPaid(ctx, o.ID, r.ChannelTxn); err == nil {
|
||||
o, _ = h.db.GetOrder(ctx, o.ID)
|
||||
}
|
||||
case r.Paid: // 金额对不上:不入账,人工对账(比错账便宜)
|
||||
c.JSON(http.StatusOK, gin.H{"order": o, "warn": "支付金额与订单不符,已挂起待人工核对"})
|
||||
return
|
||||
case r.Closed:
|
||||
_ = h.db.ExpireOrder(ctx, o.ID)
|
||||
o, _ = h.db.GetOrder(ctx, o.ID)
|
||||
}
|
||||
}
|
||||
if o.Status == store.OrderPending && time.Since(o.CreatedAt) > orderTTL {
|
||||
_ = h.db.ExpireOrder(ctx, o.ID)
|
||||
o, _ = h.db.GetOrder(ctx, o.ID)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"order": o})
|
||||
}
|
||||
|
||||
// WechatCallback: POST /api/v1/billing/callback/wechat —— 微信支付回调(公开路由,验签是唯一的门)。
|
||||
// 应答契约:入账成功/重复推送都回 200 {code:SUCCESS};验签失败 4xx;处理失败 5xx 让微信重试。
|
||||
func (h *Handler) WechatCallback(c *gin.Context) {
|
||||
if h.wechat == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"code": "FAIL", "message": "渠道未配置"})
|
||||
return
|
||||
}
|
||||
r, err := h.wechat.VerifyCallback(c.Request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"code": "FAIL", "message": "验签失败"})
|
||||
return
|
||||
}
|
||||
if !r.Paid {
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS"}) // 非成功态通知:确认收到即可
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
o, err := h.db.GetOrder(ctx, r.OrderID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS"}) // 不认识的单:可能是别的环境,别让微信无限重试
|
||||
return
|
||||
}
|
||||
if r.AmountFen != o.AmountFen {
|
||||
// 金额不符:不入账、不让重试(重试也不会变对),落审计人工处理。
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS"})
|
||||
return
|
||||
}
|
||||
if _, err := h.db.MarkOrderPaid(ctx, o.ID, r.ChannelTxn); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": "FAIL", "message": "入账失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS"})
|
||||
}
|
||||
|
||||
// BillingRedeem: POST /api/v1/billing/redeem {code} —— 核销兑换码,积分入计费租户。
|
||||
|
||||
@@ -18,21 +18,29 @@ import (
|
||||
"github.com/sundynix/sundynix-gateway/internal/blob"
|
||||
"github.com/sundynix/sundynix-gateway/internal/dsl"
|
||||
"github.com/sundynix/sundynix-gateway/internal/nats"
|
||||
"github.com/sundynix/sundynix-gateway/internal/payment"
|
||||
"github.com/sundynix/sundynix-gateway/internal/store"
|
||||
"github.com/sundynix/sundynix-shared/contract"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
db *store.Postgres
|
||||
cache *store.Redis
|
||||
bus *nats.Bus
|
||||
blob *blob.Store
|
||||
db *store.Postgres
|
||||
cache *store.Redis
|
||||
bus *nats.Bus
|
||||
blob *blob.Store
|
||||
wechat *payment.Wechat // 微信支付渠道;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
|
||||
}
|
||||
|
||||
// preflight 是「会烧钱的执行」提交前的统一关卡:当日 token 预算 → 计费租户 → 积分硬拦截。
|
||||
// 返回计费租户;ok=false 表示已写过响应,调用方直接 return。
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user