withdraw.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package crush
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  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.NewCrushWithdrawReq()
  19. if err := c.ShouldBind(&obj.In); err != nil {
  20. log.Debug("%s query params err %v", "crush.WithdrawOrder", err)
  21. c.String(http.StatusOK, "fail")
  22. return
  23. }
  24. // 判断提现金额足购
  25. /*if !coreClient.TryWithdraw(obj.In.UserID, obj.In.Amount, true) {
  26. c.String(http.StatusOK, "Insufficient amount")
  27. return
  28. }*/
  29. obj.In.IPAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  30. obj.DoAction()
  31. if obj.Out.OrderID == "" {
  32. log.Debug("%s GenOrder fail obj.In=%+v obj.Out=%+v", "crush.WithdrawOrder", obj.In, obj.Out)
  33. c.String(http.StatusOK, "fail")
  34. return
  35. }
  36. if obj.Out.GetStatus != 0 {
  37. log.Debug("%s GenOrder Audit obj.In=%+v obj.Out=%+v", "crush.WithdrawOrder", obj.In, obj.Out)
  38. c.String(http.StatusOK, "fail")
  39. return
  40. }
  41. // 请求时,没有传手机号
  42. if obj.In.Mobile == "" {
  43. obj.In.Mobile = obj.Out.Tel
  44. }
  45. // 请求withdrawOrder的代码
  46. req := withdraw_req{
  47. MerchantOrderId: obj.Out.OrderID,
  48. OrderAmount: fmt.Sprintf("%d.00", obj.In.Amount),
  49. UserName: obj.In.RealName,
  50. BankName: obj.In.BankName,
  51. AccountNumber: obj.In.BankCard,
  52. MobileNumber: obj.In.Mobile,
  53. IdNumber: "",
  54. Cci: "",
  55. NotifyUrl: config.Server.CrushPay.Url_withdraw_Notify,
  56. }
  57. // 生成签名
  58. checkContent, err := json.Marshal(req)
  59. if err != nil {
  60. log.Error("crush.WithdrawOrder checkContent json marshal fail %v", err)
  61. return
  62. }
  63. buf, err := public.AesEncrypt(checkContent, []byte(config.Server.CrushPay.MerchantKey))
  64. if err != nil {
  65. log.Error("crush.WithdrawOrder AESEncrypt err %v", err)
  66. return
  67. }
  68. sign := strings.ToUpper(hex.EncodeToString(buf))
  69. // 生成请求的 body
  70. bodyBuf, _ := json.Marshal(struct {
  71. Data string `json:"data"`
  72. }{
  73. Data: sign,
  74. })
  75. body := string(bodyBuf)
  76. log.Debug(body)
  77. respBody := httpPostByJson(config.Server.CrushPay.Url_withdraw_order, body, config.Server.CrushPay.MerchantAppId)
  78. log.Debug("crush.WithdrawOrder req ==> %+v resp ==> %+v", string(checkContent), respBody)
  79. var resp withdraw_resp
  80. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  81. log.Error("crush.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", req, respBody, err)
  82. c.String(http.StatusOK, "fail")
  83. return
  84. }
  85. log.Debug("crush.WithdrawOrder resp ==> %+v", resp)
  86. // 接口响应码,’APPLY_SUCCESS’代表成功
  87. if !resp.Success {
  88. log.Error("crush.withdrawRequest post return resp fail ==> %+v", resp)
  89. // 余额不足,退款
  90. // if resp.Data.Status != "PENDING" {
  91. // obj := db.NewPayerMaxWithdrawNotify()
  92. // obj.In.OrderID = req.Data.OutTradeNo
  93. // obj.In.DfTransactionId = resp.Data.TradeNo
  94. // obj.In.Status = 0
  95. // obj.In.DfDesc = fmt.Sprintf("%s", resp.Data.Status)
  96. // obj.In.Balance = 0
  97. // obj.DoAction()
  98. //
  99. // log.Debug("payermax.WithdrawOrder obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  100. // }
  101. c.String(http.StatusOK, "fail")
  102. return
  103. }
  104. go func() {
  105. //coreClient.TryWithdraw(obj.In.UserID, obj.In.Amount, false)
  106. // 通知
  107. notification.AddNotification(obj.In.UserID, notification.Notification_Gold, "")
  108. }()
  109. // 更新银行信息
  110. // objBank := db.NewBankInfoUp()
  111. // objBank.In.UserID = obj.In.UserID
  112. // objBank.In.RealName = req.Name
  113. // objBank.In.BankCard = req.Number
  114. // objBank.In.BankName = req.BankCode
  115. // objBank.In.Mobile = obj.In.Mobile
  116. // objBank.In.EMail = obj.In.Email
  117. // objBank.In.Address = obj.In.Address
  118. // objBank.DoAction()
  119. // log.Debug("payermax.WithdrawNotify need save bankInfo")
  120. c.String(http.StatusOK, "SUCCESS")
  121. return
  122. }
  123. // 提现通知
  124. func WithdrawNotify(c *gin.Context) {
  125. var Req struct {
  126. Data string `json:"data" form:"data"`
  127. }
  128. if err := c.ShouldBind(&Req); err != nil {
  129. log.Debug("%s query params err %v", "crush.WithdrawNotify", err)
  130. c.String(http.StatusOK, "")
  131. return
  132. }
  133. log.Debug("crush.WithdrawNotify resp ==> %+v", Req)
  134. data, err := hex.DecodeString(Req.Data)
  135. if err != nil {
  136. log.Debug("crush.WithdrawNotify hex.DecodeString %s ==> err %v", Req.Data, err)
  137. c.String(http.StatusOK, "")
  138. return
  139. }
  140. buf, err := public.AesDecrypt(data, []byte(config.Server.CrushPay.MerchantKey))
  141. if err != nil {
  142. log.Debug("crush.WithdrawNotify AESDecrypt err %v", err)
  143. c.String(http.StatusOK, "")
  144. return
  145. }
  146. log.Debug("crush.WithdrawNotify 解析成功 ==> %s", string(buf))
  147. var resp payNotify
  148. if err := json.Unmarshal(buf, &resp); err != nil {
  149. log.Debug("%s query params err %v", "crush.WithdrawNotify", err)
  150. c.String(http.StatusOK, "")
  151. return
  152. }
  153. log.Debug("crush.WithdrawNotify resp ==> %+v", resp)
  154. // 返回码,’APPLY_SUCCESS’代表成功
  155. if resp.Status != "SUCCESS" {
  156. log.Error("crush.WithdrawNotify resp ==> %+v 失败", resp)
  157. c.String(http.StatusOK, "SUCCESS")
  158. return
  159. }
  160. payAmount, err := strconv.Atoi(strings.ReplaceAll(resp.PayAmount, ".", ""))
  161. if err != nil {
  162. log.Error("crush.WithdrawNotify balance err %v", err)
  163. return
  164. }
  165. payAmount = payAmount / 100
  166. // 交易状态,SUCCESS成功, FAILD失败 ,PENDING进行中,BOUNCEBACK退票
  167. // 0=Failure 1=Success 2=Pending(Success) 3=BOUNCEBACK退票
  168. status := 0
  169. switch resp.Status {
  170. case "SUCCESS": // 1=成功
  171. status = 1
  172. default:
  173. status = 0
  174. }
  175. obj := db.NewCrushWithdrawNotify()
  176. obj.In.OrderID = resp.MerchantOrderId
  177. obj.In.DfTransactionId = resp.OrderId
  178. obj.In.Status = status
  179. obj.In.DfDesc = ""
  180. obj.In.Balance = 0
  181. obj.DoAction()
  182. log.Debug("crush.WithdrawNotify obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  183. // 提现失败,退款
  184. // if resp.Status == WITHDRAW_FAIL {
  185. // go coreClient.AddNotification(obj.Out.UserID, notification.Notification_Gold, "")
  186. // }
  187. c.String(http.StatusOK, "SUCCESS")
  188. return
  189. }