withdraw.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package flash
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "bet24.com/log"
  13. "bet24.com/public"
  14. "bet24.com/servers/payment/config"
  15. "bet24.com/servers/payment/db"
  16. "github.com/gin-gonic/gin"
  17. )
  18. // 提现请求
  19. func WithdrawOrder(c *gin.Context) {
  20. obj := db.NewFlashWithdrawReq()
  21. if err := c.ShouldBind(&obj.In); err != nil {
  22. log.Debug("%s query params err %v", "flash.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", "flash.WithdrawOrder", obj.In, obj.Out)
  30. c.String(http.StatusOK, "fail")
  31. return
  32. }
  33. if obj.Out.GetStatus != 0 {
  34. log.Debug("%s GenOrder Audit obj.In=%+v obj.Out=%+v", "flash.WithdrawOrder", obj.In, obj.Out)
  35. c.String(http.StatusOK, "fail")
  36. return
  37. }
  38. // 请求时,没有传手机号
  39. if obj.In.Mobile == "" {
  40. obj.In.Mobile = obj.Out.Tel
  41. }
  42. // 请求withdrawOrder的代码
  43. req := withdraw_req{
  44. MerchantNo: config.Server.FlashPay.MerchantCode,
  45. MerchantOrderNo: obj.Out.OrderID,
  46. Description: "withdraw",
  47. PayAmount: float64(obj.In.Amount),
  48. BankCode: obj.In.BankCode,
  49. Mobile: obj.In.Mobile,
  50. BankNumber: obj.In.BankCard,
  51. AccountHoldName: obj.In.RealName,
  52. NotifyUrl: config.Server.FlashPay.Url_withdraw_notify,
  53. // ClientNo: "",
  54. }
  55. payAmount := strconv.FormatFloat(req.PayAmount, 'f', -1, 64)
  56. params := url.Values{}
  57. params.Set("merchantNo", req.MerchantNo)
  58. params.Set("merchantOrderNo", req.MerchantOrderNo)
  59. params.Set("description", req.Description)
  60. params.Set("payAmount", payAmount)
  61. params.Set("bankCode", req.BankCode)
  62. params.Set("mobile", req.Mobile)
  63. params.Set("bankNumber", req.BankNumber)
  64. params.Set("accountHoldName", req.AccountHoldName)
  65. params.Set("notifyUrl", req.NotifyUrl)
  66. // 生成签名
  67. checkContent := createEncryptStr(params)
  68. // log.Debug("flash.WithdrawOrder.checkContent ==> %s", checkContent)
  69. // 商户请求我们接口时使用商户私钥对请求参数 进行加密
  70. mchPriKey := fmt.Sprintf(`-----BEGIN PRIVATE KEY-----
  71. %s
  72. -----END PRIVATE KEY-----
  73. `, config.Server.FlashPay.MCH_PRIVATE_KEY)
  74. if err := public.RSA.SetPrivateKey(mchPriKey); err != nil {
  75. log.Error("flashpay.WithdrawOrder set private key :%v ==> %+v", err, params)
  76. return
  77. }
  78. // 私钥加密
  79. prienctypt, err := public.RSA.PriKeyENCTYPT([]byte(checkContent))
  80. if err != nil {
  81. log.Error("flashpay.WithdrawOrder RSA.PriKeyENCTYPT err %v", err)
  82. return
  83. }
  84. req.Sign = base64.StdEncoding.EncodeToString(prienctypt)
  85. // log.Debug("flashpay.payOrder req ==> %+v ", params)
  86. buf, _ := json.Marshal(req)
  87. respBody := public.HttpPostByJson(config.Server.FlashPay.Url_withdraw_order, string(buf))
  88. log.Debug("flashpay.WithdrawOrder req ==> %+v resp ==> %+v", string(buf), respBody)
  89. var resp withdraw_resp
  90. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  91. log.Error("flashpay.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  92. c.String(http.StatusOK, "fail")
  93. return
  94. }
  95. log.Debug("flashpay.WithdrawOrder resp ==> %+v", resp)
  96. if resp.Status != "200" {
  97. log.Error("flashpay.withdrawRequest post return resp fail ==> %+v", resp)
  98. }
  99. // 请求响应码,00000表示成功,其他失败
  100. if resp.Data.OrderStatus == WITHDRAW_STATUS_FAILED {
  101. log.Error("flashpay.withdrawRequest post return resp fail ==> %+v", resp)
  102. c.String(http.StatusOK, "fail")
  103. return
  104. }
  105. c.String(http.StatusOK, "success")
  106. return
  107. }
  108. // 提现通知
  109. func WithdrawNotify(c *gin.Context) {
  110. bodyData, _ := io.ReadAll(c.Request.Body)
  111. // log.Debug("flash.WithdrawNotify ==> ctx.Request.body: %v", string(bodyData))
  112. var resp withdrawNotify
  113. err := json.Unmarshal(bodyData, &resp)
  114. if err != nil {
  115. log.Debug("%s query params err %v", "flashpay.WithdrawNotify", err)
  116. c.String(http.StatusOK, "")
  117. return
  118. }
  119. log.Debug("flashpay.WithdrawNotify resp ==> %+v", resp)
  120. params := url.Values{}
  121. params.Set("amount", resp.Amount)
  122. params.Set("merchantNo", resp.MerchantNo)
  123. params.Set("merchantOrderNo", resp.MerchantOrderNo)
  124. params.Set("platOrderNo", resp.PlatOrderNo)
  125. params.Set("orderStatus", resp.OrderStatus)
  126. params.Set("orderMessage", resp.OrderMessage)
  127. // 生成签名
  128. checkContent := createEncryptStr(params)
  129. log.Debug("flashpay.WithdrawNotify checkContent ==> %s", checkContent)
  130. // 商户使用 商户后台显示的平台公钥 进行解密
  131. platPubKey := fmt.Sprintf(`-----BEGIN PUBLIC KEY-----
  132. %s
  133. -----END PUBLIC KEY-----
  134. `, config.Server.FlashPay.PLAT_PUBLIC_KEY)
  135. if err := public.RSA.SetPublicKey(platPubKey); err != nil {
  136. log.Error("flashpay.WithdrawNotify set public key :%v ==> %+v", err, params)
  137. return
  138. }
  139. data, err := base64.RawURLEncoding.DecodeString(resp.Sign)
  140. if err != nil {
  141. log.Error("flashpay.WithdrawNotify base64.StdEncoding.DecodeString err %v", err)
  142. return
  143. }
  144. // 公钥解密
  145. pubdecrypt, err := public.RSA.PubKeyDECRYPT(data)
  146. if err != nil {
  147. log.Error("flashpay.WithdrawNotify RSA.PubKeyDECRYPT err %v", err)
  148. return
  149. }
  150. // 校验信息
  151. if checkContent != string(pubdecrypt) {
  152. log.Error("flashpay.WithdrawNotify 签名失败 ==> %+v", resp)
  153. return
  154. }
  155. log.Debug("flashpay.WithdrawNotify 签名成功")
  156. // 除了SUCCESS是成功,FAILED失败,其他状态请调用方标记为处理中,等待回调或者查询接口返回最终状态
  157. if resp.OrderStatus != WITHDRAW_STATUS_SUCCESS {
  158. log.Error("flashpay.WithdrawNotify req ==> %+v", resp)
  159. c.String(http.StatusOK, "")
  160. return
  161. }
  162. // 查询账户余额
  163. balance := queryAccount()
  164. // 除了SUCCESS是成功,FAILED失败,其他状态请调用方标记为处理中,等待回调或者查询接口返回最终状态
  165. // 状态(0=Failure 1=Success 2=Pending(Success))
  166. status := 0
  167. switch resp.OrderStatus {
  168. case WITHDRAW_STATUS_SUCCESS:
  169. status = 1
  170. case WITHDRAW_STATUS_FAILED:
  171. status = 0
  172. default:
  173. status = 1
  174. }
  175. obj := db.NewFlashWithdrawNotify()
  176. obj.In.OrderID = resp.MerchantOrderNo
  177. obj.In.DfTransactionId = resp.PlatOrderNo
  178. obj.In.Status = status
  179. obj.In.DfDesc = fmt.Sprintf("%s", strings.ReplaceAll(resp.OrderMessage, "'", ""))
  180. obj.In.Balance = balance
  181. obj.DoAction()
  182. log.Debug("flashpay.WithdrawNotify obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
  183. c.String(http.StatusOK, "success")
  184. return
  185. }
  186. // 账户余额
  187. func queryAccount() int {
  188. req := queryAccount_req{
  189. MerchantNo: config.Server.FlashPay.MerchantCode,
  190. Timestamp: time.Now().Format(TIME_FORMAT),
  191. }
  192. params := url.Values{}
  193. params.Set("merchantNo", req.MerchantNo)
  194. params.Set("timestamp", req.Timestamp)
  195. // 生成签名
  196. checkContent := createEncryptStr(params)
  197. log.Debug("flashpay.queryAccount.checkContent ==> %s", checkContent)
  198. // 商户请求我们接口时使用商户私钥对请求参数 进行加密
  199. mchPriKey := fmt.Sprintf(`-----BEGIN PRIVATE KEY-----
  200. %s
  201. -----END PRIVATE KEY-----
  202. `, config.Server.FlashPay.MCH_PRIVATE_KEY)
  203. if err := public.RSA.SetPrivateKey(mchPriKey); err != nil {
  204. log.Error("flashpay.queryAccount set private key :%v ==> %+v", err, params)
  205. return -1
  206. }
  207. // 私钥加密
  208. prienctypt, err := public.RSA.PriKeyENCTYPT([]byte(checkContent))
  209. if err != nil {
  210. log.Error("flash.queryAccount RSA.PriKeyENCTYPT err %v", err)
  211. return -1
  212. }
  213. req.Sign = base64.StdEncoding.EncodeToString(prienctypt)
  214. // log.Debug("flashpay.queryAccount req ==> %+v ", params)
  215. buf, _ := json.Marshal(req)
  216. respBody := public.HttpPostByJson(config.Server.FlashPay.Url_queryAccount, string(buf))
  217. log.Debug("flashpay.queryAccount req ==> %+v resp ==> %+v", params, respBody)
  218. var resp queryAccount_resp
  219. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  220. log.Error("flashpay.queryAccount json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  221. return -1
  222. }
  223. log.Debug("flashpay.queryAccount resp ==> %+v", resp)
  224. // 请求响应码
  225. if resp.Status != "200" {
  226. log.Error("flashpay.queryAccount post return resp fail ==> %+v", resp)
  227. return -1
  228. }
  229. balance := int(resp.Data.AvailableAmount)
  230. return balance
  231. }