subscription_payment_stripe.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/logger"
  9. "github.com/QuantumNous/new-api/model"
  10. "github.com/QuantumNous/new-api/setting"
  11. "github.com/QuantumNous/new-api/setting/system_setting"
  12. "github.com/gin-gonic/gin"
  13. "github.com/stripe/stripe-go/v81"
  14. "github.com/stripe/stripe-go/v81/checkout/session"
  15. "github.com/thanhpk/randstr"
  16. )
  17. type SubscriptionStripePayRequest struct {
  18. PlanId int `json:"plan_id"`
  19. }
  20. func SubscriptionRequestStripePay(c *gin.Context) {
  21. var req SubscriptionStripePayRequest
  22. if err := c.ShouldBindJSON(&req); err != nil || req.PlanId <= 0 {
  23. common.ApiErrorMsg(c, "参数错误")
  24. return
  25. }
  26. plan, err := model.GetSubscriptionPlanById(req.PlanId)
  27. if err != nil {
  28. common.ApiError(c, err)
  29. return
  30. }
  31. if !plan.Enabled {
  32. common.ApiErrorMsg(c, "套餐未启用")
  33. return
  34. }
  35. if plan.StripePriceId == "" {
  36. common.ApiErrorMsg(c, "该套餐未配置 StripePriceId")
  37. return
  38. }
  39. if !strings.HasPrefix(setting.StripeApiSecret, "sk_") && !strings.HasPrefix(setting.StripeApiSecret, "rk_") {
  40. common.ApiErrorMsg(c, "Stripe 未配置或密钥无效")
  41. return
  42. }
  43. if setting.StripeWebhookSecret == "" {
  44. common.ApiErrorMsg(c, "Stripe Webhook 未配置")
  45. return
  46. }
  47. userId := c.GetInt("id")
  48. user, err := model.GetUserById(userId, false)
  49. if err != nil {
  50. common.ApiError(c, err)
  51. return
  52. }
  53. if user == nil {
  54. common.ApiErrorMsg(c, "用户不存在")
  55. return
  56. }
  57. if plan.MaxPurchasePerUser > 0 {
  58. count, err := model.CountUserSubscriptionsByPlan(userId, plan.Id)
  59. if err != nil {
  60. common.ApiError(c, err)
  61. return
  62. }
  63. if count >= int64(plan.MaxPurchasePerUser) {
  64. common.ApiErrorMsg(c, "已达到该套餐购买上限")
  65. return
  66. }
  67. }
  68. reference := fmt.Sprintf("sub-stripe-ref-%d-%d-%s", user.Id, time.Now().UnixMilli(), randstr.String(4))
  69. referenceId := "sub_ref_" + common.Sha1([]byte(reference))
  70. payLink, err := genStripeSubscriptionLink(referenceId, user.StripeCustomer, user.Email, plan.StripePriceId)
  71. if err != nil {
  72. logger.LogError(c.Request.Context(), fmt.Sprintf("Stripe 订阅支付链接创建失败 trade_no=%s plan_id=%d error=%q", referenceId, plan.Id, err.Error()))
  73. c.JSON(http.StatusOK, gin.H{"message": "error", "data": "拉起支付失败"})
  74. return
  75. }
  76. order := &model.SubscriptionOrder{
  77. UserId: userId,
  78. PlanId: plan.Id,
  79. Money: plan.PriceAmount,
  80. TradeNo: referenceId,
  81. PaymentMethod: model.PaymentMethodStripe,
  82. PaymentProvider: model.PaymentProviderStripe,
  83. CreateTime: time.Now().Unix(),
  84. Status: common.TopUpStatusPending,
  85. }
  86. if err := order.Insert(); err != nil {
  87. c.JSON(http.StatusOK, gin.H{"message": "error", "data": "创建订单失败"})
  88. return
  89. }
  90. c.JSON(http.StatusOK, gin.H{
  91. "message": "success",
  92. "data": gin.H{
  93. "pay_link": payLink,
  94. },
  95. })
  96. }
  97. func genStripeSubscriptionLink(referenceId string, customerId string, email string, priceId string) (string, error) {
  98. stripe.Key = setting.StripeApiSecret
  99. params := &stripe.CheckoutSessionParams{
  100. ClientReferenceID: stripe.String(referenceId),
  101. SuccessURL: stripe.String(system_setting.ServerAddress + "/console/topup"),
  102. CancelURL: stripe.String(system_setting.ServerAddress + "/console/topup"),
  103. LineItems: []*stripe.CheckoutSessionLineItemParams{
  104. {
  105. Price: stripe.String(priceId),
  106. Quantity: stripe.Int64(1),
  107. },
  108. },
  109. Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
  110. }
  111. if "" == customerId {
  112. if "" != email {
  113. params.CustomerEmail = stripe.String(email)
  114. }
  115. params.CustomerCreation = stripe.String(string(stripe.CheckoutSessionCustomerCreationAlways))
  116. } else {
  117. params.Customer = stripe.String(customerId)
  118. }
  119. result, err := session.New(params)
  120. if err != nil {
  121. return "", err
  122. }
  123. return result.URL, nil
  124. }