(c *gin.Context)
| 21 | } |
| 22 | |
| 23 | func 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, "已达到该套餐购买上限") |
nothing calls this directly
no test coverage detected