mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 13:09:59 +00:00
Implement a new subscription-based billing model alongside existing metered/per-request billing: Backend: - Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.) - Implement CRUD APIs for subscription plan management (admin only) - Add user subscription queries with support for multiple active/expired subscriptions - Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases - Implement pre-consume and post-consume billing logic for subscription quota tracking - Add billing preference settings (subscription_first, wallet_first, etc.) - Enhance usage logs with subscription deduction details Frontend - Admin: - Add subscription management page with table view and drawer-based edit form - Match UI/UX style with existing admin pages (redemption codes, users) - Support enabling/disabling plans, configuring payment IDs, and model quotas - Add user subscription binding modal in user management Frontend - Wallet: - Add subscription plans card with current subscription status display - Show all subscriptions (active and expired) with remaining days/usage percentage - Display purchasable plans with pricing cards following SaaS best practices - Extract purchase modal to separate component matching payment confirm modal style - Add skeleton loading states with active animation - Implement billing preference selector in card header - Handle payment gateway availability based on admin configuration Frontend - Usage Logs: - Display subscription deduction details in log entries - Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining) - Add subscription deduction tag for subscription-covered requests
304 lines
8.0 KiB
Go
304 lines
8.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ---- Shared types ----
|
|
|
|
type SubscriptionPlanDTO struct {
|
|
Plan model.SubscriptionPlan `json:"plan"`
|
|
Items []model.SubscriptionPlanItem `json:"items"`
|
|
}
|
|
|
|
type BillingPreferenceRequest struct {
|
|
BillingPreference string `json:"billing_preference"`
|
|
}
|
|
|
|
func normalizeBillingPreference(pref string) string {
|
|
switch strings.TrimSpace(pref) {
|
|
case "subscription_first", "wallet_first", "subscription_only", "wallet_only":
|
|
return strings.TrimSpace(pref)
|
|
default:
|
|
return "subscription_first"
|
|
}
|
|
}
|
|
|
|
// ---- User APIs ----
|
|
|
|
func GetSubscriptionPlans(c *gin.Context) {
|
|
var plans []model.SubscriptionPlan
|
|
if err := model.DB.Where("enabled = ?", true).Order("sort_order desc, id desc").Find(&plans).Error; err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
result := make([]SubscriptionPlanDTO, 0, len(plans))
|
|
for _, p := range plans {
|
|
items, _ := model.GetSubscriptionPlanItems(p.Id)
|
|
result = append(result, SubscriptionPlanDTO{
|
|
Plan: p,
|
|
Items: items,
|
|
})
|
|
}
|
|
common.ApiSuccess(c, result)
|
|
}
|
|
|
|
func GetSubscriptionSelf(c *gin.Context) {
|
|
userId := c.GetInt("id")
|
|
settingMap, _ := model.GetUserSetting(userId, false)
|
|
pref := normalizeBillingPreference(settingMap.BillingPreference)
|
|
|
|
// Get all subscriptions (including expired)
|
|
allSubscriptions, err := model.GetAllUserSubscriptions(userId)
|
|
if err != nil {
|
|
allSubscriptions = []model.SubscriptionSummary{}
|
|
}
|
|
|
|
// Get active subscriptions for backward compatibility
|
|
activeSubscriptions, err := model.GetAllActiveUserSubscriptions(userId)
|
|
if err != nil {
|
|
activeSubscriptions = []model.SubscriptionSummary{}
|
|
}
|
|
|
|
// For backward compatibility, also return the first active subscription as "subscription"
|
|
var summary *model.SubscriptionSummary
|
|
if len(activeSubscriptions) > 0 {
|
|
summary = &activeSubscriptions[0]
|
|
}
|
|
|
|
common.ApiSuccess(c, gin.H{
|
|
"billing_preference": pref,
|
|
"subscription": summary, // backward compatibility (first active)
|
|
"subscriptions": activeSubscriptions, // all active subscriptions
|
|
"all_subscriptions": allSubscriptions, // all subscriptions including expired
|
|
})
|
|
}
|
|
|
|
func UpdateSubscriptionPreference(c *gin.Context) {
|
|
userId := c.GetInt("id")
|
|
var req BillingPreferenceRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.ApiErrorMsg(c, "参数错误")
|
|
return
|
|
}
|
|
pref := normalizeBillingPreference(req.BillingPreference)
|
|
|
|
user, err := model.GetUserById(userId, true)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
current := user.GetSetting()
|
|
current.BillingPreference = pref
|
|
user.SetSetting(current)
|
|
if err := user.Update(false); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, gin.H{"billing_preference": pref})
|
|
}
|
|
|
|
// ---- Admin APIs ----
|
|
|
|
func AdminListSubscriptionPlans(c *gin.Context) {
|
|
var plans []model.SubscriptionPlan
|
|
if err := model.DB.Order("sort_order desc, id desc").Find(&plans).Error; err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
result := make([]SubscriptionPlanDTO, 0, len(plans))
|
|
for _, p := range plans {
|
|
items, _ := model.GetSubscriptionPlanItems(p.Id)
|
|
result = append(result, SubscriptionPlanDTO{
|
|
Plan: p,
|
|
Items: items,
|
|
})
|
|
}
|
|
common.ApiSuccess(c, result)
|
|
}
|
|
|
|
type AdminUpsertSubscriptionPlanRequest struct {
|
|
Plan model.SubscriptionPlan `json:"plan"`
|
|
Items []model.SubscriptionPlanItem `json:"items"`
|
|
}
|
|
|
|
func AdminCreateSubscriptionPlan(c *gin.Context) {
|
|
var req AdminUpsertSubscriptionPlanRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.ApiErrorMsg(c, "参数错误")
|
|
return
|
|
}
|
|
req.Plan.Id = 0
|
|
if strings.TrimSpace(req.Plan.Title) == "" {
|
|
common.ApiErrorMsg(c, "套餐标题不能为空")
|
|
return
|
|
}
|
|
if req.Plan.Currency == "" {
|
|
req.Plan.Currency = "USD"
|
|
}
|
|
if req.Plan.DurationUnit == "" {
|
|
req.Plan.DurationUnit = model.SubscriptionDurationMonth
|
|
}
|
|
if req.Plan.DurationValue <= 0 && req.Plan.DurationUnit != model.SubscriptionDurationCustom {
|
|
req.Plan.DurationValue = 1
|
|
}
|
|
|
|
if len(req.Items) == 0 {
|
|
common.ApiErrorMsg(c, "套餐至少需要配置一个模型权益")
|
|
return
|
|
}
|
|
|
|
db := model.DB
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&req.Plan).Error; err != nil {
|
|
return err
|
|
}
|
|
items := make([]model.SubscriptionPlanItem, 0, len(req.Items))
|
|
for _, it := range req.Items {
|
|
if strings.TrimSpace(it.ModelName) == "" {
|
|
continue
|
|
}
|
|
if it.AmountTotal <= 0 {
|
|
continue
|
|
}
|
|
it.Id = 0
|
|
it.PlanId = req.Plan.Id
|
|
items = append(items, it)
|
|
}
|
|
if len(items) == 0 {
|
|
return errors.New("无有效的模型权益配置")
|
|
}
|
|
return tx.Create(&items).Error
|
|
})
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, req.Plan)
|
|
}
|
|
|
|
func AdminUpdateSubscriptionPlan(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if id <= 0 {
|
|
common.ApiErrorMsg(c, "无效的ID")
|
|
return
|
|
}
|
|
var req AdminUpsertSubscriptionPlanRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.ApiErrorMsg(c, "参数错误")
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Plan.Title) == "" {
|
|
common.ApiErrorMsg(c, "套餐标题不能为空")
|
|
return
|
|
}
|
|
req.Plan.Id = id
|
|
if req.Plan.Currency == "" {
|
|
req.Plan.Currency = "USD"
|
|
}
|
|
if req.Plan.DurationUnit == "" {
|
|
req.Plan.DurationUnit = model.SubscriptionDurationMonth
|
|
}
|
|
if req.Plan.DurationValue <= 0 && req.Plan.DurationUnit != model.SubscriptionDurationCustom {
|
|
req.Plan.DurationValue = 1
|
|
}
|
|
|
|
if len(req.Items) == 0 {
|
|
common.ApiErrorMsg(c, "套餐至少需要配置一个模型权益")
|
|
return
|
|
}
|
|
|
|
err := model.DB.Transaction(func(tx *gorm.DB) error {
|
|
// update plan (allow zero values updates with map)
|
|
updateMap := map[string]interface{}{
|
|
"title": req.Plan.Title,
|
|
"subtitle": req.Plan.Subtitle,
|
|
"price_amount": req.Plan.PriceAmount,
|
|
"currency": req.Plan.Currency,
|
|
"duration_unit": req.Plan.DurationUnit,
|
|
"duration_value": req.Plan.DurationValue,
|
|
"custom_seconds": req.Plan.CustomSeconds,
|
|
"enabled": req.Plan.Enabled,
|
|
"sort_order": req.Plan.SortOrder,
|
|
"stripe_price_id": req.Plan.StripePriceId,
|
|
"creem_product_id": req.Plan.CreemProductId,
|
|
"updated_at": common.GetTimestamp(),
|
|
}
|
|
if err := tx.Model(&model.SubscriptionPlan{}).Where("id = ?", id).Updates(updateMap).Error; err != nil {
|
|
return err
|
|
}
|
|
// replace items
|
|
if err := tx.Where("plan_id = ?", id).Delete(&model.SubscriptionPlanItem{}).Error; err != nil {
|
|
return err
|
|
}
|
|
items := make([]model.SubscriptionPlanItem, 0, len(req.Items))
|
|
for _, it := range req.Items {
|
|
if strings.TrimSpace(it.ModelName) == "" {
|
|
continue
|
|
}
|
|
if it.AmountTotal <= 0 {
|
|
continue
|
|
}
|
|
it.Id = 0
|
|
it.PlanId = id
|
|
items = append(items, it)
|
|
}
|
|
if len(items) == 0 {
|
|
return errors.New("无有效的模型权益配置")
|
|
}
|
|
return tx.Create(&items).Error
|
|
})
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, nil)
|
|
}
|
|
|
|
func AdminDeleteSubscriptionPlan(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if id <= 0 {
|
|
common.ApiErrorMsg(c, "无效的ID")
|
|
return
|
|
}
|
|
// best practice: disable instead of hard delete to avoid breaking past orders
|
|
if err := model.DB.Model(&model.SubscriptionPlan{}).Where("id = ?", id).Update("enabled", false).Error; err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, nil)
|
|
}
|
|
|
|
type AdminBindSubscriptionRequest struct {
|
|
UserId int `json:"user_id"`
|
|
PlanId int `json:"plan_id"`
|
|
}
|
|
|
|
func AdminBindSubscription(c *gin.Context) {
|
|
var req AdminBindSubscriptionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.UserId <= 0 || req.PlanId <= 0 {
|
|
common.ApiErrorMsg(c, "参数错误")
|
|
return
|
|
}
|
|
if err := model.AdminBindSubscription(req.UserId, req.PlanId, ""); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, nil)
|
|
}
|
|
|
|
// ---- Helper: serialize provider payload safely ----
|
|
|
|
func jsonString(v any) string {
|
|
b, _ := json.Marshal(v)
|
|
return string(b)
|
|
}
|