feat: 对齐 Waffo 包年/永久结账与客户端席位 API

同步 OpsXShell 档位定价与 Waffo 商品映射,补齐 checkout/webhook/设备列表契约,便于桌面端购买与满席释放。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Logan.LiandCursor committed 2026-08-12 11:42:56 +08:00
1 parent 4ae318335c
commit 95405de119
14 files changed
+958 -155

No files matched your search

@@ -1,6 +1,7 @@
package license
import (
"backend/internal/dto"
"net/http"
"github.com/gin-gonic/gin"
@@ -8,13 +9,7 @@ import (
// Verify 客户端 License 验证 // Logan.Li
func (ctrl *LicenseController) Verify(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceInfo string `json:"device_info"`
ProductID uint `json:"product_id"`
}
var req dto.LicenseVerifyRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
@@ -25,12 +20,7 @@ func (ctrl *LicenseController) Verify(c *gin.Context) {
// ActivateDevice 客户端激活(机器绑定) // Logan.Li
func (ctrl *LicenseController) ActivateDevice(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
DeviceName string `json:"device_name"`
DeviceInfo string `json:"device_info"`
}
var req dto.LicenseActivateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
@@ -45,10 +35,7 @@ func (ctrl *LicenseController) ActivateDevice(c *gin.Context) {
// Deactivate 客户端停用 // Logan.Li
func (ctrl *LicenseController) Deactivate(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
}
var req dto.LicenseDeactivateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
@@ -62,10 +49,7 @@ func (ctrl *LicenseController) Deactivate(c *gin.Context) {
// Heartbeat 客户端心跳 // Logan.Li
func (ctrl *LicenseController) Heartbeat(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
}
var req dto.LicenseHeartbeatRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
@@ -77,3 +61,25 @@ func (ctrl *LicenseController) Heartbeat(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "心跳成功", "data": data})
}
// ListDevices 客户端查询已绑定设备(用于席位满时释放) // Logan.Li
func (ctrl *LicenseController) ListDevices(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
devices, maxDevices, err := ctrl.svc.ListDevicesByKey(req.LicenseKey)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
"devices": devices,
"max_devices": maxDevices,
"used": len(devices),
}})
}
@@ -4,8 +4,11 @@ import (
"backend/internal/dto"
orderservice "backend/internal/service/order"
"backend/utils"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -53,15 +56,7 @@ func (ctrl *OrderController) GetDetail(c *gin.Context) {
// CreateOrder 客户端下单(公开接口) // Logan.Li
func (ctrl *OrderController) CreateOrder(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
CustomerName string `json:"customer_name"`
ProductID uint `json:"product_id" binding:"required"`
PlanID uint `json:"plan_id" binding:"required"`
DeviceID string `json:"device_id"`
ReturnURL string `json:"return_url"`
CancelURL string `json:"cancel_url"`
}
var req dto.CreateOrderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
@@ -74,20 +69,39 @@ func (ctrl *OrderController) CreateOrder(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "订单创建成功", "data": resp})
}
// Checkout desktop-friendly checkout entry. // Logan.Li
func (ctrl *OrderController) Checkout(c *gin.Context) {
var req dto.CheckoutRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
resp, err := ctrl.svc.Checkout(req, utils.GetClientIP(c), c.GetHeader("User-Agent"))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 2001, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "订单创建成功", "data": resp})
}
// ClaimPurchase fetch license after payment. // Logan.Li
func (ctrl *OrderController) ClaimPurchase(c *gin.Context) {
var req dto.ClaimPurchaseRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
resp, err := ctrl.svc.ClaimPurchase(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 2002, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": resp})
}
// PaymentWebhook 支付回调(公开接口,供支付渠道调用) // Logan.Li
func (ctrl *OrderController) PaymentWebhook(c *gin.Context) {
var req struct {
EventType string `json:"event_type" binding:"required"`
OrderNo string `json:"order_no" binding:"required"`
GatewayOrderNo string `json:"gateway_order_no"`
Status string `json:"status" binding:"required"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
CustomerEmail string `json:"customer_email"`
Gateway string `json:"gateway" binding:"required"`
Signature string `json:"signature"`
Payload string `json:"payload"`
}
var req dto.PaymentWebhookRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "Invalid payload"})
return
@@ -99,16 +113,133 @@ func (ctrl *OrderController) PaymentWebhook(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "Webhook processed"})
}
// WaffoWebhook accepts loosely-shaped Waffo payment events and maps to PaymentWebhook. // Logan.Li
func (ctrl *OrderController) WaffoWebhook(c *gin.Context) {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无法读取请求体"})
return
}
// Optional: accept X-Waffo-Signature if present (no strict verification yet). // Logan.Li
_ = c.GetHeader("X-Waffo-Signature")
var payload map[string]interface{}
if len(body) > 0 {
if err := json.Unmarshal(body, &payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "Invalid JSON"})
return
}
} else {
payload = map[string]interface{}{}
}
orderNo := firstNonEmpty(
asString(payload["order_no"]),
asString(payload["orderNo"]),
asString(payload["order_id"]),
c.Query("order_no"),
c.Query("orderNo"),
)
if orderNo == "" {
if meta, ok := payload["metadata"].(map[string]interface{}); ok {
orderNo = firstNonEmpty(asString(meta["orderNo"]), asString(meta["order_no"]))
}
}
if orderNo == "" {
if data, ok := payload["data"].(map[string]interface{}); ok {
orderNo = firstNonEmpty(asString(data["order_no"]), asString(data["orderNo"]))
if orderNo == "" {
if meta, ok := data["metadata"].(map[string]interface{}); ok {
orderNo = firstNonEmpty(asString(meta["orderNo"]), asString(meta["order_no"]))
}
}
}
}
if orderNo == "" {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "missing order_no"})
return
}
eventType := firstNonEmpty(
asString(payload["event_type"]),
asString(payload["eventType"]),
asString(payload["type"]),
asString(payload["event"]),
"payment",
)
statusRaw := strings.ToLower(firstNonEmpty(
asString(payload["status"]),
asString(payload["payment_status"]),
asString(payload["paymentStatus"]),
))
gatewayOrderNo := firstNonEmpty(
asString(payload["gateway_order_no"]),
asString(payload["gatewayOrderNo"]),
asString(payload["transaction_id"]),
asString(payload["transactionId"]),
asString(payload["id"]),
)
mappedStatus := "paid"
switch {
case strings.Contains(statusRaw, "fail"), strings.Contains(statusRaw, "cancel"),
strings.Contains(eventType, "fail"), strings.Contains(eventType, "cancel"):
mappedStatus = "failed"
case strings.Contains(statusRaw, "refund"), strings.Contains(eventType, "refund"):
mappedStatus = "refunded"
case statusRaw == "" || strings.Contains(statusRaw, "paid") || strings.Contains(statusRaw, "complete") ||
strings.Contains(statusRaw, "success") || strings.Contains(eventType, "complete") ||
strings.Contains(eventType, "paid") || strings.Contains(eventType, "success"):
mappedStatus = "paid"
default:
// Treat unknown completed-like events as paid for now. // Logan.Li
mappedStatus = "paid"
}
req := dto.PaymentWebhookRequest{
EventType: eventType,
OrderNo: orderNo,
GatewayOrderNo: gatewayOrderNo,
Status: mappedStatus,
Gateway: "waffo",
Payload: string(body),
Signature: c.GetHeader("X-Waffo-Signature"),
}
if err := ctrl.svc.PaymentWebhook(req, utils.GetClientIP(c)); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "Webhook processed"})
}
func asString(v interface{}) string {
switch t := v.(type) {
case string:
return strings.TrimSpace(t)
case float64:
if t == float64(int64(t)) {
return strconv.FormatInt(int64(t), 10)
}
return strconv.FormatFloat(t, 'f', -1, 64)
case json.Number:
return t.String()
default:
return ""
}
}
func firstNonEmpty(values ...string) string {
for _, v := range values {
if strings.TrimSpace(v) != "" {
return strings.TrimSpace(v)
}
}
return ""
}
// ManualGrant 手动发放 License(管理后台) // Logan.Li
func (ctrl *OrderController) ManualGrant(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
CustomerName string `json:"customer_name"`
ProductID uint `json:"product_id" binding:"required"`
PlanID uint `json:"plan_id" binding:"required"`
DeviceID string `json:"device_id"`
Notes string `json:"notes"`
}
var req dto.ManualGrantRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
@@ -125,10 +256,7 @@ func (ctrl *OrderController) ManualGrant(c *gin.Context) {
// CheckDuplicatePurchase 防重复购买检测(公开接口) // Logan.Li
func (ctrl *OrderController) CheckDuplicatePurchase(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
ProductID uint `json:"product_id" binding:"required"`
}
var req dto.DuplicatePurchaseCheckRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
@@ -151,10 +279,7 @@ func (ctrl *OrderController) CheckDuplicatePurchase(c *gin.Context) {
// CheckPurchaseStatus 查询购买状态(公开接口) // Logan.Li
func (ctrl *OrderController) CheckPurchaseStatus(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
ProductID uint `json:"product_id" binding:"required"`
}
var req dto.CheckPurchaseStatusRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
@@ -1,6 +1,8 @@
package product
import (
"backend/internal/dto"
planservice "backend/internal/service/plan"
productservice "backend/internal/service/product"
"backend/utils"
"fmt"
@@ -13,13 +15,18 @@ import (
// ProductController 产品控制器 // Logan.Li
type ProductController struct {
svc *productservice.ProductService
db *gorm.DB
svc *productservice.ProductService
planSvc *planservice.PlanService
db *gorm.DB
}
// NewProductController 创建控制器实例 // Logan.Li
func NewProductController(db *gorm.DB) *ProductController {
return &ProductController{svc: productservice.NewProductService(db), db: db}
return &ProductController{
svc: productservice.NewProductService(db),
planSvc: planservice.NewPlanService(db),
db: db,
}
}
// List 产品列表 // Logan.Li
@@ -60,18 +67,28 @@ func (ctrl *ProductController) Get(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"code": 0, "data": product})
}
// GetByCode 按产品编码返回产品及上架计划(公开接口) // Logan.Li
func (ctrl *ProductController) GetByCode(c *gin.Context) {
code := c.Param("code")
product, err := ctrl.svc.GetByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 404, "message": err.Error()})
return
}
plans, err := ctrl.planSvc.GetByProduct(product.ID)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": dto.ProductWithPlansResponse{
Product: *product,
Plans: plans,
}})
}
// Create 创建产品 // Logan.Li
func (ctrl *ProductController) Create(c *gin.Context) {
var req struct {
Name string `json:"name" binding:"required"`
Code string `json:"code" binding:"required"`
Version string `json:"version"`
Description string `json:"description"`
Features []string `json:"features"`
Price float64 `json:"price"`
PriceModel string `json:"price_model"`
Status *int `json:"status"`
}
var req dto.ProductCreate
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
@@ -88,15 +105,7 @@ func (ctrl *ProductController) Create(c *gin.Context) {
// Update 更新产品 // Logan.Li
func (ctrl *ProductController) Update(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Features []string `json:"features"`
Price *float64 `json:"price"`
PriceModel string `json:"price_model"`
Status *int `json:"status"`
}
var req dto.ProductUpdate
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return