mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-18 10:17:26 +00:00
Apply consistent code formatting across the entire codebase using gofmt and lint:fix tools. This ensures adherence to Go community standards and improves code readability and maintainability. Changes include: - Run gofmt on all .go files to standardize formatting - Apply lint:fix to automatically resolve linting issues - Fix code style inconsistencies and formatting violations No functional changes were made in this commit.
100 lines
2.5 KiB
Go
100 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,
|
|
},
|
|
})
|
|
}
|