mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 08:36:22 +00:00
Complete subscription orders by creating a matching top-up record and writing billing logs Add Epay return handler to verify and finalize browser callbacks Require Stripe/Creem webhook configuration before starting subscription payments Show subscription purchases in topup history with clearer labels/methods Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields Simplify subscription self API payload to active/all lists only
101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/QuantumNous/new-api/setting"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/thanhpk/randstr"
|
|
)
|
|
|
|
type SubscriptionCreemPayRequest struct {
|
|
PlanId int `json:"plan_id"`
|
|
}
|
|
|
|
func SubscriptionRequestCreemPay(c *gin.Context) {
|
|
var req SubscriptionCreemPayRequest
|
|
|
|
// Keep body for debugging consistency (like RequestCreemPay)
|
|
bodyBytes, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
log.Printf("read subscription creem pay req body err: %v", err)
|
|
c.JSON(200, gin.H{"message": "error", "data": "read query error"})
|
|
return
|
|
}
|
|
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.PlanId <= 0 {
|
|
c.JSON(200, gin.H{"message": "error", "data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
plan, err := model.GetSubscriptionPlanById(req.PlanId)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
if !plan.Enabled {
|
|
common.ApiErrorMsg(c, "套餐未启用")
|
|
return
|
|
}
|
|
if plan.CreemProductId == "" {
|
|
common.ApiErrorMsg(c, "该套餐未配置 CreemProductId")
|
|
return
|
|
}
|
|
if setting.CreemWebhookSecret == "" && !setting.CreemTestMode {
|
|
common.ApiErrorMsg(c, "Creem Webhook 未配置")
|
|
return
|
|
}
|
|
|
|
userId := c.GetInt("id")
|
|
user, _ := model.GetUserById(userId, false)
|
|
|
|
reference := "sub-creem-ref-" + randstr.String(6)
|
|
referenceId := "sub_ref_" + common.Sha1([]byte(reference+time.Now().String()+user.Username))
|
|
|
|
// create pending order first
|
|
order := &model.SubscriptionOrder{
|
|
UserId: userId,
|
|
PlanId: plan.Id,
|
|
Money: plan.PriceAmount,
|
|
TradeNo: referenceId,
|
|
PaymentMethod: PaymentMethodCreem,
|
|
CreateTime: time.Now().Unix(),
|
|
Status: common.TopUpStatusPending,
|
|
}
|
|
if err := order.Insert(); err != nil {
|
|
c.JSON(200, gin.H{"message": "error", "data": "创建订单失败"})
|
|
return
|
|
}
|
|
|
|
// Reuse Creem checkout generator by building a lightweight product reference.
|
|
product := &CreemProduct{
|
|
ProductId: plan.CreemProductId,
|
|
Name: plan.Title,
|
|
Price: plan.PriceAmount,
|
|
Currency: plan.Currency,
|
|
Quota: 0,
|
|
}
|
|
|
|
checkoutUrl, err := genCreemLink(referenceId, product, user.Email, user.Username)
|
|
if err != nil {
|
|
log.Printf("获取Creem支付链接失败: %v", err)
|
|
c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
"message": "success",
|
|
"data": gin.H{
|
|
"checkout_url": checkoutUrl,
|
|
"order_id": referenceId,
|
|
},
|
|
})
|
|
}
|
|
|