topup.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 * float64(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. id := c.GetInt("id")
  49. amount := GetAmount(float64(req.Amount))
  50. if req.PaymentMethod == "zfb" {
  51. req.PaymentMethod = "alipay"
  52. }
  53. if req.PaymentMethod == "wx" {
  54. req.PaymentMethod = "wxpay"
  55. }
  56. returnUrl, _ := url.Parse(common.ServerAddress + "/log")
  57. notifyUrl, _ := url.Parse(common.ServerAddress + "/api/user/epay/notify")
  58. tradeNo := strconv.FormatInt(time.Now().Unix(), 10)
  59. payMoney := amount
  60. client := GetEpayClient()
  61. if client == nil {
  62. c.JSON(200, gin.H{"message": "error", "data": "当前管理员未配置支付信息"})
  63. return
  64. }
  65. uri, params, err := client.Purchase(&epay.PurchaseArgs{
  66. Type: epay.PurchaseType(req.PaymentMethod),
  67. ServiceTradeNo: "A" + tradeNo,
  68. Name: "B" + tradeNo,
  69. Money: strconv.FormatFloat(payMoney, 'f', 2, 64),
  70. Device: epay.PC,
  71. NotifyUrl: notifyUrl,
  72. ReturnUrl: returnUrl,
  73. })
  74. if err != nil {
  75. c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})
  76. return
  77. }
  78. topUp := &model.TopUp{
  79. UserId: id,
  80. Amount: req.Amount,
  81. Money: int(amount),
  82. TradeNo: "A" + tradeNo,
  83. CreateTime: time.Now().Unix(),
  84. Status: "pending",
  85. }
  86. err = topUp.Insert()
  87. if err != nil {
  88. c.JSON(200, gin.H{"message": "error", "data": "创建订单失败"})
  89. return
  90. }
  91. c.JSON(200, gin.H{"message": "success", "data": params, "url": uri})
  92. }
  93. func EpayNotify(c *gin.Context) {
  94. params := lo.Reduce(lo.Keys(c.Request.URL.Query()), func(r map[string]string, t string, i int) map[string]string {
  95. r[t] = c.Request.URL.Query().Get(t)
  96. return r
  97. }, map[string]string{})
  98. client := GetEpayClient()
  99. if client == nil {
  100. log.Println("易支付回调失败 未找到配置信息")
  101. _, err := c.Writer.Write([]byte("fail"))
  102. if err != nil {
  103. log.Println("易支付回调写入失败")
  104. }
  105. }
  106. verifyInfo, err := client.Verify(params)
  107. if err == nil && verifyInfo.VerifyStatus {
  108. _, err := c.Writer.Write([]byte("success"))
  109. if err != nil {
  110. log.Println("易支付回调写入失败")
  111. }
  112. } else {
  113. _, err := c.Writer.Write([]byte("fail"))
  114. if err != nil {
  115. log.Println("易支付回调写入失败")
  116. }
  117. }
  118. if verifyInfo.TradeStatus == epay.StatusTradeSuccess {
  119. log.Println(verifyInfo)
  120. topUp := model.GetTopUpByTradeNo(verifyInfo.ServiceTradeNo)
  121. if topUp.Status == "pending" {
  122. topUp.Status = "success"
  123. err := topUp.Update()
  124. if err != nil {
  125. log.Printf("易支付回调更新订单失败: %v", topUp)
  126. return
  127. }
  128. //user, _ := model.GetUserById(topUp.UserId, false)
  129. //user.Quota += topUp.Amount * 500000
  130. err = model.IncreaseUserQuota(topUp.UserId, topUp.Amount*500000)
  131. if err != nil {
  132. log.Printf("易支付回调更新用户失败: %v", topUp)
  133. return
  134. }
  135. log.Printf("易支付回调更新用户成功 %v", topUp)
  136. model.RecordLog(topUp.UserId, model.LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v", common.LogQuota(topUp.Amount*500000)))
  137. }
  138. } else {
  139. log.Printf("易支付异常回调: %v", verifyInfo)
  140. }
  141. }
  142. func RequestAmount(c *gin.Context) {
  143. var req AmountRequest
  144. err := c.ShouldBindJSON(&req)
  145. if err != nil {
  146. c.JSON(200, gin.H{"message": "error", "data": "参数错误"})
  147. return
  148. }
  149. c.JSON(200, gin.H{"message": "success", "data": GetAmount(float64(req.Amount))})
  150. }