topup.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var client, _ = epay.NewClientWithUrl(&epay.Config{
  24. PartnerID: "1096",
  25. Key: "n08V9LpE8JffA3NPP893689u8p39NV9J",
  26. }, "https://api.lempay.org")
  27. func GetAmount(id int, count float64, topUpCode string) float64 {
  28. amount := count * 1.5
  29. if topUpCode != "" {
  30. if topUpCode == "nekoapi" {
  31. if id == 89 {
  32. amount = count * 1
  33. } else if id == 98 || id == 105 || id == 107 {
  34. amount = count * 1.2
  35. } else if id == 1 {
  36. amount = count * 1
  37. }
  38. }
  39. }
  40. return amount
  41. }
  42. func RequestEpay(c *gin.Context) {
  43. var req EpayRequest
  44. err := c.ShouldBindJSON(&req)
  45. if err != nil {
  46. c.JSON(200, gin.H{"message": err.Error(), "data": 10})
  47. return
  48. }
  49. id := c.GetInt("id")
  50. amount := GetAmount(id, float64(req.Amount), req.TopUpCode)
  51. if id != 1 {
  52. if req.Amount < 10 {
  53. c.JSON(200, gin.H{"message": "最小充值10元", "data": amount, "count": 10})
  54. return
  55. }
  56. }
  57. if req.PaymentMethod == "zfb" {
  58. if amount > 400 {
  59. c.JSON(200, gin.H{"message": "支付宝最大充值400元", "data": amount, "count": 400})
  60. return
  61. }
  62. req.PaymentMethod = "alipay"
  63. }
  64. if req.PaymentMethod == "wx" {
  65. if amount > 600 {
  66. c.JSON(200, gin.H{"message": "微信最大充值600元", "data": amount, "count": 600})
  67. return
  68. }
  69. req.PaymentMethod = "wxpay"
  70. }
  71. returnUrl, _ := url.Parse("https://nekoapi.com/log")
  72. notifyUrl, _ := url.Parse("https://nekoapi.com/api/user/epay/notify")
  73. tradeNo := strconv.FormatInt(time.Now().Unix(), 10)
  74. uri, params, err := client.Purchase(&epay.PurchaseArgs{
  75. Type: epay.PurchaseType(req.PaymentMethod),
  76. ServiceTradeNo: "A" + tradeNo,
  77. Name: "B" + tradeNo,
  78. Money: strconv.FormatFloat(amount*0.99, 'f', 2, 64),
  79. Device: epay.PC,
  80. NotifyUrl: notifyUrl,
  81. ReturnUrl: returnUrl,
  82. })
  83. if err != nil {
  84. c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})
  85. return
  86. }
  87. topUp := &model.TopUp{
  88. UserId: id,
  89. Amount: req.Amount,
  90. Money: int(amount),
  91. TradeNo: "A" + tradeNo,
  92. CreateTime: time.Now().Unix(),
  93. Status: "pending",
  94. }
  95. err = topUp.Insert()
  96. if err != nil {
  97. c.JSON(200, gin.H{"message": "error", "data": "创建订单失败"})
  98. return
  99. }
  100. c.JSON(200, gin.H{"message": "success", "data": params, "url": uri})
  101. }
  102. func EpayNotify(c *gin.Context) {
  103. params := lo.Reduce(lo.Keys(c.Request.URL.Query()), func(r map[string]string, t string, i int) map[string]string {
  104. r[t] = c.Request.URL.Query().Get(t)
  105. return r
  106. }, map[string]string{})
  107. verifyInfo, err := client.Verify(params)
  108. if err == nil && verifyInfo.VerifyStatus {
  109. _, err := c.Writer.Write([]byte("success"))
  110. if err != nil {
  111. log.Println("易支付回调写入失败")
  112. }
  113. } else {
  114. _, err := c.Writer.Write([]byte("fail"))
  115. if err != nil {
  116. log.Println("易支付回调写入失败")
  117. }
  118. }
  119. if verifyInfo.TradeStatus == epay.StatusTradeSuccess {
  120. log.Println(verifyInfo)
  121. topUp := model.GetTopUpByTradeNo(verifyInfo.ServiceTradeNo)
  122. if topUp.Status == "pending" {
  123. topUp.Status = "success"
  124. err := topUp.Update()
  125. if err != nil {
  126. log.Printf("易支付回调更新订单失败: %v", topUp)
  127. return
  128. }
  129. //user, _ := model.GetUserById(topUp.UserId, false)
  130. //user.Quota += topUp.Amount * 500000
  131. err = model.IncreaseUserQuota(topUp.UserId, topUp.Amount*500000)
  132. if err != nil {
  133. log.Printf("易支付回调更新用户失败: %v", topUp)
  134. return
  135. }
  136. log.Printf("易支付回调更新用户成功 %v", topUp)
  137. model.RecordLog(topUp.UserId, model.LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v", common.LogQuota(topUp.Amount*500000)))
  138. }
  139. } else {
  140. log.Printf("易支付异常回调: %v", verifyInfo)
  141. }
  142. }
  143. func RequestAmount(c *gin.Context) {
  144. var req AmountRequest
  145. err := c.ShouldBindJSON(&req)
  146. if err != nil {
  147. c.JSON(200, gin.H{"message": err.Error(), "data": 10})
  148. return
  149. }
  150. id := c.GetInt("id")
  151. if id != 1 {
  152. if req.Amount < 10 {
  153. c.JSON(200, gin.H{"message": "最小充值10刀", "data": GetAmount(id, 10, req.TopUpCode), "count": 10})
  154. return
  155. }
  156. if req.Amount > 400 {
  157. c.JSON(200, gin.H{"message": "最大充值400刀", "data": GetAmount(id, 400, req.TopUpCode), "count": 400})
  158. return
  159. }
  160. }
  161. c.JSON(200, gin.H{"message": "success", "data": GetAmount(id, float64(req.Amount), req.TopUpCode)})
  162. }