withdraw.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package lets
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "bet24.com/log"
  10. "bet24.com/public"
  11. notification "bet24.com/servers/micros/notification/proto"
  12. "bet24.com/servers/payment/config"
  13. "bet24.com/servers/payment/db"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // 提现请求
  17. func WithdrawOrder(c *gin.Context) {
  18. obj := db.NewLetsWithdrawReq()
  19. if err := c.ShouldBind(&obj.In); err != nil {
  20. log.Debug("%s query params err %v", "lets.WithdrawOrder", err)
  21. c.String(http.StatusOK, "fail")
  22. return
  23. }
  24. obj.In.IPAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  25. obj.DoAction()
  26. if obj.Out.OrderID == "" {
  27. log.Debug("%s GenOrder fail obj.In=%+v obj.Out=%+v", "lets.WithdrawOrder", obj.In, obj.Out)
  28. c.String(http.StatusOK, "fail")
  29. return
  30. }
  31. // 请求时,没有传手机号
  32. if obj.In.Mobile == "" {
  33. obj.In.Mobile = obj.Out.Tel
  34. }
  35. // 请求withdrawOrder的代码
  36. req := withdraw_req{
  37. Type: "api",
  38. MchId: config.Server.LetsPay.MchId,
  39. MchTransNo: obj.Out.OrderID,
  40. Amount: obj.In.Amount,
  41. NotifyUrl: config.Server.LetsPay.Url_witdraw_notify,
  42. AccountName: obj.In.RealName,
  43. AccountNo: obj.In.BankCard,
  44. BankCode: obj.In.BankName,
  45. RemarkInfo: fmt.Sprintf("email:%s/phone:%s", obj.In.Email, obj.In.Mobile),
  46. }
  47. params := url.Values{}
  48. params.Set("type", req.Type)
  49. params.Set("mchId", req.MchId)
  50. params.Set("mchTransNo", req.MchTransNo)
  51. params.Set("amount", fmt.Sprintf("%d.00", req.Amount))
  52. params.Set("notifyUrl", req.NotifyUrl)
  53. params.Set("accountName", req.AccountName)
  54. params.Set("accountNo", req.AccountNo)
  55. params.Set("bankCode", req.BankCode)
  56. params.Set("remarkInfo", req.RemarkInfo)
  57. // 生成签名
  58. checkContent := createEncryptStr(params) + "&key=" + config.Server.LetsPay.MD5Key
  59. // log.Debug("lets.WithdrawOrder.checkContent ==> %s", checkContent)
  60. signature := strings.ToUpper(public.GetMd5String(checkContent))
  61. params.Set("sign", signature)
  62. respBody := public.HttpPostForm(config.Server.LetsPay.Url_withdraw_order, params)
  63. log.Debug("lets.WithdrawOrder req ==> %+v resp ==> %+v", params, respBody)
  64. var resp withdraw_resp
  65. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  66. log.Error("lets.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  67. c.String(http.StatusOK, "fail")
  68. return
  69. }
  70. log.Debug("lets.WithdrawOrder resp ==> %+v", resp)
  71. // 请求响应码
  72. if resp.RetCode != "SUCCESS" {
  73. log.Error("lets.withdrawRequest post return resp fail ==> %+v", resp)
  74. c.String(http.StatusOK, "fail")
  75. return
  76. }
  77. go notification.AddNotification(obj.In.UserID, notification.Notification_Gold, "")
  78. // 更新银行信息
  79. objBank := db.NewBankInfoUp()
  80. objBank.In.UserID = obj.In.UserID
  81. objBank.In.RealName = req.AccountName
  82. objBank.In.BankCard = req.AccountNo
  83. objBank.In.BankName = req.BankCode
  84. objBank.In.Mobile = obj.In.Mobile
  85. objBank.In.EMail = obj.In.Email
  86. objBank.In.Address = obj.In.Address
  87. objBank.DoAction()
  88. log.Debug("lets.WithdrawNotify need save bankInfo")
  89. c.String(http.StatusOK, "success")
  90. return
  91. }
  92. // 提现通知
  93. func WithdrawNotify(c *gin.Context) {
  94. var req withdrawNotify
  95. if err := c.ShouldBind(&req); err != nil {
  96. log.Debug("%s query params err %v", "lets.WithdrawNotify", err)
  97. c.String(http.StatusOK, "")
  98. return
  99. }
  100. log.Debug("lets.WithdrawNotify ==> %+v", req)
  101. // 请求响应码
  102. if req.Status != "2" {
  103. log.Error("lets.WithdrawNotify req ==> %+v", req)
  104. c.String(http.StatusOK, "")
  105. return
  106. }
  107. amount := strconv.FormatFloat(req.Amount, 'f', 2, 64)
  108. params := url.Values{}
  109. params.Set("mchId", config.Server.LetsPay.MchId)
  110. params.Set("mchTransNo", req.MchTransNo)
  111. params.Set("amount", amount)
  112. params.Set("status", req.Status)
  113. params.Set("transSuccTime", req.TransSuccTime)
  114. // 生成签名
  115. checkContent := createEncryptStr(params) + "&key=" + config.Server.LetsPay.MD5Key
  116. // log.Debug("lets.WithdrawNotify.checkContent ==> %s", checkContent)
  117. signature := strings.ToUpper(public.GetMd5String(checkContent))
  118. // 校验签名
  119. if req.Sign != signature {
  120. log.Error("lets.WithdrawNotify 签名失败 post req ==> %+v signature ==> %s", req, signature)
  121. return
  122. }
  123. log.Debug("lets.WithdrawNotify 签名成功")
  124. // 代付状态 1 处理中,2 成功,3 失败
  125. if req.Status != "2" && req.Status != "3" {
  126. log.Error("lets.WithdrawNotify response fail ==> %+v", req)
  127. return
  128. }
  129. // 查询账户余额
  130. // balance := queryAccount()
  131. obj := db.NewLetsWithdrawNotify()
  132. obj.In.OrderID = req.MchTransNo
  133. obj.In.BankCard = ""
  134. obj.In.DfTransactionId = req.TransSuccTime
  135. obj.In.DfState = req.Status
  136. obj.In.DfDesc = ""
  137. obj.In.Balance = 0
  138. obj.DoAction()
  139. log.Debug("lets.WithdrawNotify obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  140. // 提现失败,退款
  141. if req.Status == "3" {
  142. go notification.AddNotification(obj.Out.UserID, notification.Notification_Gold, "")
  143. }
  144. c.String(http.StatusOK, "success")
  145. return
  146. }
  147. // 账户余额
  148. func queryAccount() int {
  149. req := queryAccount_req{
  150. MchId: config.Server.LetsPay.MchId,
  151. }
  152. params := url.Values{}
  153. params.Set("mchId", req.MchId)
  154. // 生成签名
  155. checkContent := createEncryptStr(params) + "&key=" + config.Server.LetsPay.MD5Key
  156. // log.Debug("lets.queryAccount.checkContent ==> %s", checkContent)
  157. signature := strings.ToUpper(public.GetMd5String(checkContent))
  158. params.Set("sign", signature)
  159. respBody := public.HttpPostForm(config.Server.LetsPay.Url_queryAccount, params)
  160. log.Debug("lets.queryAccount req ==> %+v resp ==> %+v", params, respBody)
  161. var resp queryAccount_resp
  162. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  163. log.Error("lets.queryAccount json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  164. return -1
  165. }
  166. log.Debug("lets.queryAccount resp ==> %+v", resp)
  167. amount := strconv.FormatFloat(resp.Balance, 'f', 2, 64)
  168. balance, err := strconv.Atoi(strings.ReplaceAll(amount, ".00", ""))
  169. if err != nil {
  170. log.Error("lets.queryAccount price err %v", err)
  171. return -1
  172. }
  173. return balance
  174. }