MCPcopy
hub / github.com/QuantumNous/new-api / SubscriptionRequestCreemPay

Function SubscriptionRequestCreemPay

controller/subscription_payment_creem.go:23–136  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

21}
22
23func SubscriptionRequestCreemPay(c *gin.Context) {
24 if !requirePaymentCompliance(c) {
25 return
26 }
27
28 var req SubscriptionCreemPayRequest
29
30 // Keep body for debugging consistency (like RequestCreemPay)
31 bodyBytes, err := io.ReadAll(c.Request.Body)
32 if err != nil {
33 logger.LogError(c.Request.Context(), fmt.Sprintf("Creem 订阅支付请求读取失败 error=%q", err.Error()))
34 c.JSON(http.StatusOK, gin.H{"message": "error", "data": "read query error"})
35 return
36 }
37 c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
38
39 if err := c.ShouldBindJSON(&req); err != nil || req.PlanId <= 0 {
40 c.JSON(http.StatusOK, gin.H{"message": "error", "data": "参数错误"})
41 return
42 }
43
44 plan, err := model.GetSubscriptionPlanById(req.PlanId)
45 if err != nil {
46 common.ApiError(c, err)
47 return
48 }
49 if !plan.Enabled {
50 common.ApiErrorMsg(c, "套餐未启用")
51 return
52 }
53 if plan.CreemProductId == "" {
54 common.ApiErrorMsg(c, "该套餐未配置 CreemProductId")
55 return
56 }
57 if setting.CreemWebhookSecret == "" && !setting.CreemTestMode {
58 common.ApiErrorMsg(c, "Creem Webhook 未配置")
59 return
60 }
61
62 userId := c.GetInt("id")
63 user, err := model.GetUserById(userId, false)
64 if err != nil {
65 common.ApiError(c, err)
66 return
67 }
68 if user == nil {
69 common.ApiErrorMsg(c, "用户不存在")
70 return
71 }
72
73 if plan.MaxPurchasePerUser > 0 {
74 count, err := model.CountUserSubscriptionsByPlan(userId, plan.Id)
75 if err != nil {
76 common.ApiError(c, err)
77 return
78 }
79 if count >= int64(plan.MaxPurchasePerUser) {
80 common.ApiErrorMsg(c, "已达到该套餐购买上限")

Callers

nothing calls this directly

Calls 14

InsertMethod · 0.95
LogErrorFunction · 0.92
GetSubscriptionPlanByIdFunction · 0.92
ApiErrorFunction · 0.92
ApiErrorMsgFunction · 0.92
GetUserByIdFunction · 0.92
Sha1Function · 0.92
GetGeneralSettingFunction · 0.92
requirePaymentComplianceFunction · 0.85
genCreemLinkFunction · 0.85
ReadAllMethod · 0.80

Tested by

no test coverage detected