topup.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/samber/lo"
  6. epay "github.com/star-horizon/go-epay"
  7. "log"
  8. "net/url"
  9. "one-api/common"
  10. "one-api/model"
  11. "strconv"
  12. "time"
  13. )
  14. type EpayRequest struct {
  15. Amount int `json:"amount"`
  16. PaymentMethod string `json:"payment_method"`
  17. TopUpCode string `json:"top_up_code"`
  18. }
  19. type AmountRequest struct {
  20. Amount int `json:"amount"`
  21. TopUpCode string `json:"top_up_code"`
  22. }
  23. func GetEpayClient() *epay.Client {
  24. if common.PayAddress == "" || common.EpayId == "" || common.EpayKey == "" {
  25. return nil
  26. }
  27. withUrl, err := epay.NewClientWithUrl(&epay.Config{
  28. PartnerID: common.EpayId,
  29. Key: common.EpayKey,
  30. }, common.PayAddress)
  31. if err != nil {
  32. return nil
  33. }
  34. return withUrl
  35. }
  36. func GetAmount(count float64) float64 {
  37. // 别问为什么用float64,问就是这么点钱没必要
  38. amount := count * common.Price
  39. return amount
  40. }
  41. func RequestEpay(c *gin.Context) {
  42. var req EpayRequest
  43. err := c.ShouldBindJSON(&req)
  44. if err != nil {
  45. c.JSON(200, gin.H{"message": err.Error(), "data": 10})
  46. return
  47. }
  48. if req.Amount < 1 {
  49. c.JSON(200, gin.H{"message": "充值金额不能小于1", "data": 10})
  50. return
  51. }
  52. id := c.GetInt("id")
  53. amount := GetAmount(float64(req.Amount))
  54. if req.PaymentMethod == "zfb" {
  55. req.PaymentMethod = "alipay"
  56. }
  57. if req.PaymentMethod == "wx" {
  58. req.PaymentMethod = "wxpay"
  59. }
  60. returnUrl, _ := url.Parse(common.ServerAddress + "/log")
  61. notifyUrl, _ := url.Parse(common.ServerAddress + "/api/user/epay/notify")
  62. tradeNo := strconv.FormatInt(time.Now().Unix(), 10)
  63. payMoney := amount
  64. client := GetEpayClient()
  65. if client == nil {
  66. c.JSON(200, gin.H{"message": "error", "data": "当前管理员未配置支付信息"})
  67. return
  68. }
  69. uri, params, err := client.Purchase(&epay.PurchaseArgs{
  70. Type: epay.PurchaseType(req.PaymentMethod),
  71. ServiceTradeNo: "A" + tradeNo,
  72. Name: "B" + tradeNo,
  73. Money: strconv.FormatFloat(payMoney, 'f', 2, 64),
  74. Device: epay.PC,
  75. NotifyUrl: notifyUrl,
  76. ReturnUrl: returnUrl,
  77. })
  78. if err != nil {
  79. c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})
  80. return
  81. }
  82. topUp := &model.TopUp{
  83. UserId: id,
  84. Amount: req.Amount,
  85. Money: int(amount),
  86. TradeNo: "A" + tradeNo,
  87. CreateTime: time.Now().Unix(),
  88. Status: "pending",
  89. }
  90. err = topUp.Insert()
  91. if err != nil {
  92. c.JSON(200, gin.H{"message": "error", "data": "创建订单失败"})
  93. return
  94. }
  95. c.JSON(200, gin.H{"message": "success", "data": params, "url": uri})
  96. }
  97. func EpayNotify(c *gin.Context) {
  98. params := lo.Reduce(lo.Keys(c.Request.URL.Query()), func(r map[string]string, t string, i int) map[string]string {
  99. r[t] = c.Request.URL.Query().Get(t)
  100. return r
  101. }, map[string]string{})
  102. client := GetEpayClient()
  103. if client == nil {
  104. log.Println("易支付回调失败 未找到配置信息")
  105. _, err := c.Writer.Write([]byte("fail"))
  106. if err != nil {
  107. log.Println("易支付回调写入失败")
  108. }
  109. }
  110. verifyInfo, err := client.Verify(params)
  111. if err == nil && verifyInfo.VerifyStatus {
  112. _, err := c.Writer.Write([]byte("success"))
  113. if err != nil {
  114. log.Println("易支付回调写入失败")
  115. }
  116. } else {
  117. _, err := c.Writer.Write([]byte("fail"))
  118. if err != nil {
  119. log.Println("易支付回调写入失败")
  120. }
  121. }
  122. if verifyInfo.TradeStatus == epay.StatusTradeSuccess {
  123. log.Println(verifyInfo)
  124. topUp := model.GetTopUpByTradeNo(verifyInfo.ServiceTradeNo)
  125. if topUp.Status == "pending" {
  126. topUp.Status = "success"
  127. err := topUp.Update()
  128. if err != nil {
  129. log.Printf("易支付回调更新订单失败: %v", topUp)
  130. return
  131. }
  132. //user, _ := model.GetUserById(topUp.UserId, false)
  133. //user.Quota += topUp.Amount * 500000
  134. err = model.IncreaseUserQuota(topUp.UserId, topUp.Amount*500000)
  135. if err != nil {
  136. log.Printf("易支付回调更新用户失败: %v", topUp)
  137. return
  138. }
  139. log.Printf("易支付回调更新用户成功 %v", topUp)
  140. model.RecordLog(topUp.UserId, model.LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v", common.LogQuota(topUp.Amount*500000)))
  141. }
  142. } else {
  143. log.Printf("易支付异常回调: %v", verifyInfo)
  144. }
  145. }
  146. func RequestAmount(c *gin.Context) {
  147. var req AmountRequest
  148. err := c.ShouldBindJSON(&req)
  149. if err != nil {
  150. c.JSON(200, gin.H{"message": "error", "data": "参数错误"})
  151. return
  152. }
  153. if req.Amount < 1 {
  154. c.JSON(200, gin.H{"message": "error", "data": "充值金额不能小于1"})
  155. return
  156. }
  157. c.JSON(200, gin.H{"message": "success", "data": GetAmount(float64(req.Amount))})
  158. }