topup.go 5.0 KB

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