withdraw.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package dingpei
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "bet24.com/log"
  11. "bet24.com/public"
  12. notification "bet24.com/servers/micros/notification/proto"
  13. "bet24.com/servers/payment/config"
  14. "bet24.com/servers/payment/db"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // 提现请求
  18. func WithdrawOrder(c *gin.Context) {
  19. obj := db.NewDingpeiWithdrawReq()
  20. if err := c.ShouldBind(&obj.In); err != nil {
  21. log.Debug("%s query params err %v", "DingpeiPay.WithdrawOrder", err)
  22. c.String(http.StatusOK, "fail")
  23. return
  24. }
  25. obj.In.IPAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  26. obj.DoAction()
  27. if obj.Out.OrderID == "" {
  28. log.Debug("%s GenOrder OrderId is empty fail obj.In=%+v obj.Out=%+v", "DingpeiPay.WithdrawOrder", obj.In, obj.Out)
  29. c.String(http.StatusOK, "fail")
  30. return
  31. }
  32. // 下单失败
  33. if obj.Out.RetCode != 1 {
  34. log.Debug("%s GenOrder fail obj.In=%+v obj.Out=%+v", "DingpeiPay.WithdrawOrder", obj.In, obj.Out)
  35. c.String(http.StatusOK, "fail")
  36. return
  37. }
  38. // 下单成功,待审核
  39. if obj.Out.GetStatus != 0 {
  40. log.Debug("%s GenOrder success obj.In=%+v obj.Out=%+v", "DingpeiPay.WithdrawOrder", obj.In, obj.Out)
  41. c.String(http.StatusOK, "success")
  42. return
  43. }
  44. // 请求时,没有传手机号
  45. if obj.In.Mobile == "" {
  46. obj.In.Mobile = obj.Out.Tel
  47. }
  48. // 请求withdrawOrder的代码
  49. req := withdraw_req{
  50. MerchantNo: config.Server.DingpeiPay.MerchantNo,
  51. MerchantOrderNo: obj.Out.OrderID,
  52. Amount: fmt.Sprintf("%d.00", obj.In.Amount),
  53. BankCode: obj.In.BankCode,
  54. BankAccountNumber: obj.In.BankCard,
  55. BankAccountName: obj.In.RealName,
  56. BankBranch: "",
  57. CnapsNo: "",
  58. Province: 0,
  59. City: "",
  60. NotifyUrl: config.Server.DingpeiPay.Url_withdraw_notify,
  61. ClientIp: obj.In.IPAddress,
  62. Sign: "",
  63. }
  64. params := url.Values{}
  65. params.Set("merchantNo", req.MerchantNo)
  66. params.Set("merchantOrderNo", req.MerchantOrderNo)
  67. params.Set("amount", req.Amount)
  68. params.Set("bankCode", req.BankCode)
  69. params.Set("bankAccountNumber", req.BankAccountNumber)
  70. params.Set("bankAccountName", req.BankAccountName)
  71. params.Set("notifyUrl", req.NotifyUrl)
  72. params.Set("clientIp", req.ClientIp)
  73. // 生成签名
  74. checkContent := createEncryptStr(params) + "&key=" + config.Server.DingpeiPay.MD5Key
  75. // log.Debug("DingpeiPay.WithdrawOrder.checkContent ==> %s", checkContent)
  76. signature := strings.ToUpper(public.GetMd5String(checkContent))
  77. req.Sign = signature
  78. params.Set("sign", req.Sign)
  79. // POST请求
  80. respBody := public.HttpPostForm(config.Server.DingpeiPay.Url_withdraw_order, params)
  81. log.Debug("DingpeiPay.WithdrawOrder req ==> %+v resp ==> %+v", params, respBody)
  82. var resp withdraw_resp
  83. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  84. log.Error("DingpeiPay.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  85. c.String(http.StatusOK, "fail")
  86. return
  87. }
  88. log.Debug("DingpeiPay.WithdrawOrder resp ==> %+v", resp)
  89. // 请求响应码 0:成功,1:失败
  90. if resp.Code != 0 {
  91. log.Error("DingpeiPay.withdrawRequest post return resp fail ==> %+v", resp)
  92. c.String(http.StatusOK, "fail")
  93. return
  94. }
  95. go notification.AddNotification(obj.In.UserID, notification.Notification_Gold, "")
  96. c.String(http.StatusOK, "success")
  97. }
  98. // 提现通知
  99. func WithdrawNotify(c *gin.Context) {
  100. data, _ := io.ReadAll(c.Request.Body)
  101. log.Debug("DingpeiPay.WithdrawNotify ==> ctx.Request.body: %v", string(data))
  102. var req withdrawNotify
  103. if err := json.Unmarshal(data, &req); err != nil {
  104. log.Debug("%s query params err %v", "DingpeiPay.WithdrawNotify", err)
  105. c.String(http.StatusOK, "")
  106. return
  107. }
  108. // log.Debug("DingpeiPay.WithdrawNotify ==> %+v", req)
  109. // 订单状态:0:待处理 1:代付成功 2:代付失败
  110. if req.Status == 2 {
  111. log.Error("DingpeiPay.WithdrawNotify req ==> %+v", req)
  112. c.String(http.StatusOK, "")
  113. return
  114. }
  115. params := url.Values{}
  116. params.Set("systemOrderNo", req.SystemOrderNo)
  117. params.Set("merchantOrderNo", req.MerchantOrderNo)
  118. params.Set("amount", req.Amount)
  119. params.Set("balance", req.Balance)
  120. params.Set("status", strconv.Itoa(req.Status))
  121. params.Set("errorMsg", req.ErrorMsg)
  122. // 生成签名
  123. checkContent := createEncryptStr(params) + "&key=" + config.Server.DingpeiPay.MD5Key
  124. // log.Debug("DingpeiPay.WithdrawNotify.checkContent ==> %s", checkContent)
  125. signature := strings.ToUpper(public.GetMd5String(checkContent))
  126. // 校验签名
  127. if req.Sign != signature {
  128. log.Error("DingpeiPay.WithdrawNotify 签名失败 post req ==> %+v signature ==> %s", req, signature)
  129. return
  130. }
  131. log.Debug("DingpeiPay.WithdrawNotify 签名成功")
  132. // 订单状态:0:待处理 1:代付成功 2:代付失败
  133. if req.Status == 2 {
  134. log.Error("DingpeiPay.WithdrawNotify response fail ==> %+v", req)
  135. return
  136. }
  137. balance, err := strconv.Atoi(strings.ReplaceAll(req.Balance, ".", ""))
  138. if err != nil {
  139. log.Error("DingpeiPay.WithdrawNotify balance err %v", err)
  140. return
  141. }
  142. balance = balance / 100
  143. obj := db.NewDingpeiWithdrawNotify()
  144. obj.In.OrderID = req.MerchantOrderNo
  145. obj.In.DfTransactionId = req.SystemOrderNo
  146. obj.In.Status = req.Status
  147. obj.In.DfDesc = ""
  148. obj.In.Balance = balance
  149. obj.DoAction()
  150. log.Debug("DingpeiPay.WithdrawNotify obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  151. c.String(http.StatusOK, "success")
  152. return
  153. }