withdraw.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package zhongshui
  2. import (
  3. "encoding/json"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "time"
  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. uuid "github.com/satori/go.uuid"
  17. )
  18. // 提现请求
  19. func WithdrawOrder(c *gin.Context) {
  20. obj := db.NewZShuiWithdrawReq()
  21. if err := c.ShouldBind(&obj.In); err != nil {
  22. log.Debug("%s query params err %v", "zhongshui.WithdrawOrder", err)
  23. c.String(http.StatusOK, "fail")
  24. return
  25. }
  26. obj.In.IPAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  27. obj.DoAction()
  28. if obj.Out.OrderID == "" {
  29. log.Debug("%s GenOrder fail obj.In=%+v obj.Out=%+v", "zhongshui.WithdrawOrder", obj.In, obj.Out)
  30. c.String(http.StatusOK, "fail")
  31. return
  32. }
  33. // 请求时,没有传手机号
  34. if obj.In.Mobile == "" {
  35. obj.In.Mobile = obj.Out.Tel
  36. }
  37. id, _ := uuid.NewV4()
  38. // 请求withdrawOrder的代码
  39. req := withdraw_req{
  40. GymchtId: config.Server.ZShuiPay.GymchtId,
  41. DfSn: obj.Out.OrderID,
  42. ReceiptAmount: obj.In.Amount,
  43. CurType: "1",
  44. PayType: "1",
  45. PaymentModes: "IMPS",
  46. ReceiptName: obj.In.RealName,
  47. ReceiptPan: obj.In.BankCard,
  48. ReceiptBankNm: obj.In.BankName,
  49. SettleNo: "PYTM000001",
  50. BeneficiaryVPA: "VPA",
  51. BeneficiaryPhoneNo: obj.In.Mobile, // PS
  52. BeneficiaryEmail: obj.In.Email,
  53. BeneficiaryAddress: obj.In.Address,
  54. Mobile: obj.In.Mobile,
  55. AcctType: "0",
  56. Nonce: id.String(),
  57. NotifyUrl: config.Server.ZShuiPay.Url_withdraw_notify,
  58. }
  59. params := url.Values{}
  60. params.Set("gymchtId", req.GymchtId)
  61. params.Set("dfSn", req.DfSn)
  62. params.Set("receiptAmount", strconv.Itoa(req.ReceiptAmount))
  63. params.Set("curType", req.CurType)
  64. params.Set("payType", req.PayType)
  65. params.Set("paymentModes", req.PaymentModes)
  66. params.Set("receiptName", req.ReceiptName)
  67. params.Set("receiptPan", req.ReceiptPan)
  68. params.Set("receiptBankNm", req.ReceiptBankNm)
  69. params.Set("settleNo", req.SettleNo)
  70. params.Set("beneficiaryVPA", req.BeneficiaryVPA)
  71. params.Set("beneficiaryPhoneNo", req.BeneficiaryPhoneNo)
  72. params.Set("beneficiaryEmail", req.BeneficiaryEmail)
  73. params.Set("beneficiaryAddress", req.BeneficiaryAddress)
  74. params.Set("mobile", req.Mobile)
  75. params.Set("acctType", req.AcctType)
  76. params.Set("nonce", req.Nonce)
  77. params.Set("notifyUrl", req.NotifyUrl)
  78. // 生成签名
  79. checkContent := createEncryptStr(params) + "&key=" + config.Server.ZShuiPay.GymKey
  80. // log.Debug("zhongshui.WithdrawOrder.checkContent ==> %s", checkContent)
  81. signature := strings.ToUpper(public.GetMd5String(checkContent))
  82. params.Set("sign", signature)
  83. respBody := public.HttpPostForm(config.Server.ZShuiPay.Url_withdraw_order, params)
  84. log.Debug("zhongshui.WithdrawOrder req ==> %+v resp ==> %+v", params, respBody)
  85. var resp withdraw_resp
  86. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  87. log.Error("zhongshui.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  88. c.String(http.StatusOK, "fail")
  89. return
  90. }
  91. // log.Debug("zhongshui.WithdrawOrder resp ==> %+v", resp)
  92. // 请求响应码,00000表示成功,其他失败
  93. if resp.ResultCode != "00000" {
  94. log.Error("zhongshui.withdrawRequest post return resp fail ==> %+v", resp)
  95. c.String(http.StatusOK, "fail")
  96. return
  97. }
  98. paramsResp := url.Values{}
  99. paramsResp.Set("resultCode", resp.ResultCode)
  100. paramsResp.Set("message", resp.Message)
  101. paramsResp.Set("gymchtId", req.GymchtId)
  102. paramsResp.Set("dfSn", resp.DfSn)
  103. paramsResp.Set("dfTransactionId", resp.DfTransactionId)
  104. paramsResp.Set("dfState", resp.DfState)
  105. paramsResp.Set("dfDesc", resp.DfDesc)
  106. paramsResp.Set("timeEnd", resp.TimeEnd)
  107. paramsResp.Set("nonce", resp.Nonce)
  108. // 生成签名
  109. checkContent_resp := createEncryptStr(paramsResp) + "&key=" + config.Server.ZShuiPay.GymKey
  110. // log.Debug("zhongshui.WithdrawOrder checkContent_resp ==> %s", checkContent_resp)
  111. signature_resp := strings.ToUpper(public.GetMd5String(checkContent_resp))
  112. // 校验签名
  113. if resp.Sign != signature_resp {
  114. log.Error("zhongshui.WithdrawOrder 签名失败 post return resp ==> %+v", resp)
  115. c.String(http.StatusOK, "fail")
  116. return
  117. }
  118. log.Debug("zhongshui.WithdrawOrder 签名成功")
  119. go notification.AddNotification(obj.In.UserID, notification.Notification_Gold, "")
  120. // 更新银行信息
  121. objBank := db.NewBankInfoUp()
  122. objBank.In.UserID = obj.In.UserID
  123. objBank.In.RealName = req.ReceiptName
  124. objBank.In.BankCard = req.ReceiptPan
  125. objBank.In.BankName = req.ReceiptBankNm
  126. objBank.In.Mobile = req.Mobile
  127. objBank.In.EMail = req.BeneficiaryEmail
  128. objBank.In.Address = req.BeneficiaryAddress
  129. objBank.DoAction()
  130. // log.Debug("zhongshui.WithdrawNotify need save bankInfo")
  131. c.String(http.StatusOK, "success")
  132. return
  133. }
  134. // 提现通知
  135. func WithdrawNotify(c *gin.Context) {
  136. data, _ := io.ReadAll(c.Request.Body)
  137. log.Debug("zhongshui.WithdrawNotify ==> ctx.Request.body: %v", string(data))
  138. var req withdrawNotify
  139. err := json.Unmarshal(data, &req)
  140. if err != nil {
  141. log.Debug("%s query params err %v", "zhongshui.WithdrawNotify", err)
  142. c.String(http.StatusOK, "")
  143. return
  144. }
  145. // log.Debug("zhongshui.WithdrawNotify ==> %+v", req)
  146. // 请求响应码,00000表示成功
  147. if req.ResultCode != "00000" {
  148. log.Error("zhongshui.WithdrawNotify req ==> %+v", req)
  149. c.String(http.StatusOK, "")
  150. return
  151. }
  152. params := url.Values{}
  153. params.Set("resultCode", req.ResultCode)
  154. params.Set("message", req.Message)
  155. params.Set("gymchtId", req.GymchtId)
  156. params.Set("dfTransactionId", req.DfTransactionId)
  157. params.Set("dfSn", req.DfSn)
  158. params.Set("dfState", req.DfState)
  159. params.Set("receiptAmount", strconv.Itoa(req.ReceiptAmount))
  160. params.Set("dfDesc", req.DfDesc)
  161. params.Set("receiptName", req.ReceiptName)
  162. params.Set("receiptPan", req.ReceiptPan)
  163. params.Set("receiptBankNm", req.ReceiptBankNm)
  164. params.Set("mobile", req.Mobile)
  165. params.Set("timeEnd", req.TimeEnd)
  166. params.Set("memo", req.Memo)
  167. params.Set("nonce", req.Nonce)
  168. // 生成签名
  169. checkContent := createEncryptStr(params) + "&key=" + config.Server.ZShuiPay.GymKey
  170. // log.Debug("zhongshui.WithdrawNotify.checkContent ==> %s", checkContent)
  171. signature := strings.ToUpper(public.GetMd5String(checkContent))
  172. // 校验签名
  173. if req.Sign != signature {
  174. log.Error("zhongshui.WithdrawNotify 签名失败 post req ==> %+v signature ==> %s", req, signature)
  175. return
  176. }
  177. log.Debug("zhongshui.WithdrawNotify 签名成功")
  178. // 代付状态 00-成功 01-处理中 02-处理失败
  179. if req.DfState != "00" && req.DfState != "02" {
  180. log.Error("zhongshui.WithdrawNotify response fail ==> %+v", req)
  181. return
  182. }
  183. // 查询账户余额
  184. balance := queryAccount()
  185. obj := db.NewZShuiWithdrawNotify()
  186. obj.In.OrderID = req.DfSn
  187. obj.In.BankCard = req.ReceiptPan
  188. obj.In.DfTransactionId = req.DfTransactionId
  189. obj.In.DfState = req.DfState
  190. obj.In.DfDesc = req.DfDesc
  191. obj.In.Balance = balance
  192. obj.DoAction()
  193. log.Debug("zhongshui.WithdrawNotify obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  194. // 提现失败,退款
  195. if req.DfState == "02" {
  196. go notification.AddNotification(obj.Out.UserID, notification.Notification_Gold, "")
  197. }
  198. c.String(http.StatusOK, "success")
  199. return
  200. }
  201. // 账户余额
  202. func queryAccount() int {
  203. id, _ := uuid.NewV4()
  204. req := queryAccount_req{
  205. GymchtId: config.Server.ZShuiPay.GymchtId,
  206. QryTime: time.Now().Format("20060102150405"),
  207. QryType: "1",
  208. Nonce: id.String(),
  209. }
  210. params := url.Values{}
  211. params.Set("gymchtId", req.GymchtId)
  212. params.Set("qryTime", req.QryTime)
  213. params.Set("qryType", req.QryType)
  214. params.Set("nonce", req.Nonce)
  215. // 生成签名
  216. checkContent := createEncryptStr(params) + "&key=" + config.Server.ZShuiPay.GymKey
  217. // log.Debug("zhongshui.queryAccount.checkContent ==> %s", checkContent)
  218. signature := strings.ToUpper(public.GetMd5String(checkContent))
  219. params.Set("sign", signature)
  220. respBody := public.HttpPostForm(config.Server.ZShuiPay.Url_withdraw_queryAccount, params)
  221. log.Debug("zhongshui.queryAccount req ==> %+v resp ==> %+v", params, respBody)
  222. var resp queryAccount_resp
  223. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  224. log.Error("zhongshui.queryAccount json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  225. return -1
  226. }
  227. // log.Debug("zhongshui.queryAccount resp ==> %+v", resp)
  228. // 请求响应码,00000表示成功,其他失败
  229. if resp.ResultCode != "00000" {
  230. log.Error("zhongshui.withdrawRequest post return resp fail ==> %+v", resp)
  231. return -1
  232. }
  233. return resp.Balance
  234. }