feat(billing): 支付 P5.1 —— 订单骨架 + 兑换码渠道全闭环
按 PAYMENT_DESIGN.md 施工。零资质渠道先把「订单→入账→对账」的骨架跑真, 微信 Native(P5.2)进来只是多一个 adapter。 - store/payment.go:CreditPack(admin 可配包)/PaymentOrder(支付侧事实源,兑换 也写单,全部充值一个查法)/RedeemCode(SDX-XXXX-XXXX-XXXX,crypto/rand,剔除 易混字符)。三模型都不标 isTenantScoped——订单归计费租户,与活跃租户可能 分叉,插件自动注入会写错归属(RecentRuns 同款教训),显式赋值+显式过滤。 - Redeem 单事务:码 CAS 占用(unused→used 只成功一次,幂等主闸)→建已支付 订单→账本分录+物化余额。credit_ledger 加 (kind,ref) 部分唯一索引兜底 ——GrantCredits 此前对 ref 零约束,回调 at-least-once 就是重复入账事故。 - 路由:/billing/packs|orders(查,全员) + /billing/redeem(≥member+审计); admin /redeem-codes(生成/列表) + /packs(配包)。 - Web 面账单页:兑换码输入(viewer 不摆输入框,真闸在后端)+最近充值订单流。 - 测试:码形态/200 样本无撞码/参数边界;go+tsc+vitest 全绿。 live 13 项:生成→核销余额精确+100→重core 400(主闸)→DB 直插重复 ref 被唯一 索引打回(兜底闸实证)→viewer 403→balance==SUM(ledger) 不变量→审计留痕→ 浏览器 UI 兑换 100→200+订单流展示。 已知余项:admin 生成码暂只有 API(admin 页 UI 下一刀);微信 adapter=P5.2。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sundynix/sundynix-gateway/internal/store"
|
||||
)
|
||||
|
||||
// 充值(P5.1:兑换码渠道;设计见 PAYMENT_DESIGN.md)。
|
||||
// 入账目标一律是「计费租户」(ResolveBillingTenantID)——和消耗记账同一本账,
|
||||
// 谁的池子扣钱就往谁的池子充,别让用户充进一个花不到的池。
|
||||
|
||||
// BillingPacks: GET /api/v1/billing/packs —— 在售积分包(微信渠道 P5.2 上线前仅展示)。
|
||||
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}})
|
||||
}
|
||||
|
||||
// BillingRedeem: POST /api/v1/billing/redeem {code} —— 核销兑换码,积分入计费租户。
|
||||
func (h *Handler) BillingRedeem(c *gin.Context) {
|
||||
var b struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil || strings.TrimSpace(b.Code) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "code 必填"})
|
||||
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
|
||||
}
|
||||
order, err := h.db.Redeem(ctx, b.Code, billing, uid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"order": order,
|
||||
"balance_micro": h.db.TenantBalance(ctx, billing),
|
||||
})
|
||||
}
|
||||
|
||||
// BillingOrders: GET /api/v1/billing/orders —— 计费租户最近充值记录(账单页展示)。
|
||||
func (h *Handler) BillingOrders(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
billing := h.db.ResolveBillingTenantID(ctx, userID(c), tenantID(c))
|
||||
rows, err := h.db.TenantOrders(ctx, billing, 20)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"orders": rows})
|
||||
}
|
||||
|
||||
// AdminGenRedeemCodes: POST /api/v1/admin/redeem-codes {credits, count, memo} —— 批量生成兑换码。
|
||||
// credits 单位:积分(面向人,非 micro)。
|
||||
func (h *Handler) AdminGenRedeemCodes(c *gin.Context) {
|
||||
var b struct {
|
||||
Credits float64 `json:"credits"`
|
||||
Count int `json:"count"`
|
||||
Memo string `json:"memo"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil || b.Credits <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "credits 必填且为正"})
|
||||
return
|
||||
}
|
||||
if b.Count <= 0 {
|
||||
b.Count = 1
|
||||
}
|
||||
codes, err := h.db.GenerateRedeemCodes(c.Request.Context(), b.Count, int64(b.Credits*1e6), b.Memo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"codes": codes})
|
||||
}
|
||||
|
||||
// AdminRedeemCodes: GET /api/v1/admin/redeem-codes —— 兑换码列表(含核销状态)。
|
||||
func (h *Handler) AdminRedeemCodes(c *gin.Context) {
|
||||
rows, err := h.db.ListRedeemCodes(c.Request.Context(), 200)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"codes": rows})
|
||||
}
|
||||
|
||||
// AdminSavePack: PUT /api/v1/admin/packs {id?, name, credits, price_fen, active, sort} —— 配积分包。
|
||||
func (h *Handler) AdminSavePack(c *gin.Context) {
|
||||
var b struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Credits float64 `json:"credits"` // 积分(面向人)
|
||||
PriceFen int64 `json:"price_fen"`
|
||||
Active bool `json:"active"`
|
||||
Sort int `json:"sort"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&b); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
||||
return
|
||||
}
|
||||
pk := &store.CreditPack{Name: strings.TrimSpace(b.Name), CreditsMicro: int64(b.Credits * 1e6), PriceFen: b.PriceFen, Active: b.Active, Sort: b.Sort}
|
||||
pk.ID = b.ID
|
||||
if err := h.db.SavePack(c.Request.Context(), pk); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"pack": pk})
|
||||
}
|
||||
|
||||
// AdminPacks: GET /api/v1/admin/packs —— 全部积分包(含下架)。
|
||||
func (h *Handler) AdminPacks(c *gin.Context) {
|
||||
rows, err := h.db.ListPacks(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"packs": rows})
|
||||
}
|
||||
Reference in New Issue
Block a user