pay.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package zhongshui
  2. import (
  3. "encoding/json"
  4. "math"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "bet24.com/servers/payment/shop"
  10. "bet24.com/log"
  11. "bet24.com/public"
  12. "bet24.com/servers/payment/config"
  13. "bet24.com/servers/payment/db"
  14. "github.com/gin-gonic/gin"
  15. coreClient "bet24.com/servers/coreservice/client"
  16. )
  17. // 下单
  18. func PayOrder(c *gin.Context) {
  19. obj := db.NewOrder(db.SP_ZShui_ORDER)
  20. if err := c.ShouldBind(&obj.In); err != nil {
  21. log.Debug("%s query params err %v", "zhongshui.PayOrder", err)
  22. c.String(http.StatusOK, "")
  23. return
  24. }
  25. obj.In.IpAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  26. // 币种为空,根据ip获取
  27. //if obj.In.Currency == "" {
  28. // currency := shop.GetCurrencyRateByIp(obj.In.UserID, obj.In.IpAddress)
  29. // obj.In.Currency = currency
  30. //}
  31. // 获取产品信息
  32. item := shop.GetProduct(obj.In.ProductID)
  33. if item == nil {
  34. log.Error("%s query GetProduct productId=%s currency=%s is nil", "zhongshui.PayOrder", obj.In.ProductID, obj.In.Currency)
  35. c.String(http.StatusOK, "")
  36. return
  37. }
  38. //// 获取当前汇率信息
  39. //info := shop.GetExchangeRate(obj.In.Currency)
  40. //if info == nil {
  41. // log.Error("%s query GetExchangeRate obj.In.Currency=%s is nil", "zhongshui.PayOrder", obj.In.Currency)
  42. // c.String(http.StatusOK, "")
  43. // return
  44. //}
  45. //
  46. //// 计算价格
  47. //calPrice := info.Rate * item.Price
  48. //
  49. //// 检查价格是否篡改
  50. //if calPrice != obj.In.Price {
  51. // log.Error("%s obj.In.Price=%v info.Rate=%v calPrice=%v is invalid", "zhongshui.PayOrder", obj.In.Price, info.Rate, calPrice)
  52. // c.String(http.StatusOK, "")
  53. // return
  54. //}
  55. // 检查价格是否篡改
  56. if item.Price != obj.In.Price {
  57. log.Error("%s obj.In.Price=%v calPrice=%v is invalid", "zhongshui.PayOrder", obj.In.Price, item.Price)
  58. c.String(http.StatusOK, "")
  59. return
  60. }
  61. obj.DoAction(nil)
  62. if obj.Out.OrderID == "" {
  63. log.Debug("%s GenOrder fail obj.In=%+v", "zhongshui.PayOrder", obj.In)
  64. c.String(http.StatusOK, "")
  65. return
  66. }
  67. //请求payOrder的代码
  68. req := pay_req{
  69. GymchtId: config.Server.ZShuiPay.GymchtId,
  70. TradeSn: obj.Out.OrderID,
  71. OrderAmount: int(math.Ceil(obj.In.Price)),
  72. GoodsName: obj.In.ProductID,
  73. TradeSource: "webstaging",
  74. Realname: obj.In.Name,
  75. UserMobile: obj.In.Tel,
  76. UserEmail: obj.In.Email,
  77. Callback_url: config.Server.ZShuiPay.Url_pay_callback,
  78. NotifyUrl: config.Server.ZShuiPay.Url_pay_notify,
  79. }
  80. params := url.Values{}
  81. params.Set("gymchtId", req.GymchtId)
  82. params.Set("tradeSn", req.TradeSn)
  83. params.Set("orderAmount", strconv.Itoa(req.OrderAmount))
  84. params.Set("goodsName", req.GoodsName)
  85. params.Set("tradeSource", req.TradeSource)
  86. params.Set("realname", req.Realname)
  87. params.Set("userMobile", req.UserMobile)
  88. params.Set("userEmail", req.UserEmail)
  89. params.Set("callback_url", req.Callback_url)
  90. params.Set("notifyUrl", req.NotifyUrl)
  91. // 生成签名
  92. checkContent := createEncryptStr(params) + "&key=" + config.Server.ZShuiPay.GymKey
  93. // log.Debug("order.checkContent ==> %s", checkContent)
  94. signature := strings.ToUpper(public.GetMd5String(checkContent))
  95. params.Set("sign", signature)
  96. // POST请求
  97. respBody := public.HttpPostForm(config.Server.ZShuiPay.Url_pay_order, params)
  98. log.Debug("zhongshui.payOrder req ==> %+v resp ==> %+v", params, respBody)
  99. var resp pay_resp
  100. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  101. log.Error("zhongshui.payOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  102. return
  103. }
  104. // log.Debug("zhongshui.payOrder resp ==> %+v", resp)
  105. // 请求响应码,00000表示成功,其他失败
  106. if resp.ResultCode != "00000" {
  107. log.Error("zhongshui.payOrder post return resp fail ==> %+v", resp)
  108. return
  109. }
  110. paramsResp := url.Values{}
  111. paramsResp.Set("resultCode", resp.ResultCode)
  112. paramsResp.Set("message", resp.Message)
  113. paramsResp.Set("gymchtId", config.Server.ZShuiPay.GymchtId)
  114. paramsResp.Set("code_url", resp.Code_url)
  115. // 生成签名
  116. checkContent_resp := createEncryptStr(paramsResp) + "&key=" + config.Server.ZShuiPay.GymKey
  117. // log.Debug("order.checkContent_resp ==> %s", checkContent_resp)
  118. signature_resp := strings.ToUpper(public.GetMd5String(checkContent_resp))
  119. // 校验签名
  120. if resp.Sign != signature_resp {
  121. log.Error("zhongshui.payOrder 签名失败 post return resp ==> %+v", resp)
  122. return
  123. }
  124. log.Debug("zhongshui.payOrder 签名成功")
  125. // 判断支付链接是否为空
  126. if resp.Code_url == "" {
  127. log.Error("zhongshui.payOrder response Code_url is empty %+v", resp)
  128. return
  129. }
  130. // log.Debug("paymentUrl ==> %s", resp.Code_url)
  131. // 跳转到支付页面,以便持卡人完成支付过程
  132. c.Redirect(http.StatusMovedPermanently, resp.Code_url)
  133. return
  134. }
  135. // 回调通知
  136. func PayNotify(c *gin.Context) {
  137. var resp payNotify
  138. if err := c.ShouldBind(&resp); err != nil {
  139. log.Debug("%s query params err %v", "zhongshui.PayNotify", err)
  140. c.String(http.StatusOK, "")
  141. return
  142. }
  143. log.Debug("zhongshui.PayNotify resp ==> %+v", resp)
  144. params := url.Values{}
  145. params.Set("gymchtId", config.Server.ZShuiPay.GymchtId)
  146. params.Set("transaction_id", resp.Transaction_id)
  147. params.Set("tradeSn", resp.TradeSn)
  148. params.Set("pay_result", resp.Pay_result)
  149. params.Set("pay_info", resp.Pay_info)
  150. params.Set("orderAmount", strconv.Itoa(resp.OrderAmount))
  151. params.Set("bankType", resp.BankType)
  152. params.Set("timeEnd", resp.TimeEnd)
  153. params.Set("t0Flag", resp.T0Flag)
  154. // 生成签名
  155. checkContent := createEncryptStr(params) + "&key=" + config.Server.ZShuiPay.GymKey
  156. // log.Debug("order.OrderNotify checkContent ==> %s", checkContent)
  157. signature := strings.ToUpper(public.GetMd5String(checkContent))
  158. // 校验签名
  159. if resp.Sign != signature {
  160. log.Error("zhongshui.PayNotify 签名失败 ==> %+v", resp)
  161. return
  162. }
  163. log.Debug("zhongshui.PayNotify 签名成功")
  164. // 0-成功,其他失败
  165. if resp.Pay_result != "0" {
  166. log.Error("zhongshui.PayNotify resp ==> %+v 失败", resp)
  167. return
  168. }
  169. // 数据库操作
  170. obj := db.NewNotify(db.SP_ZShui_NOTIFY)
  171. obj.In.OrderID = resp.TradeSn
  172. obj.In.TradeID = resp.Transaction_id
  173. //obj.In.Price = resp.OrderAmount
  174. obj.DoAction(nil)
  175. //操作成功,给道具
  176. if obj.Out.RetCode == 1 {
  177. //充值
  178. resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
  179. log.Debug("%s 充值成功 %+v", "zhongshui.PayNotify", resp)
  180. }
  181. c.String(http.StatusOK, "success")
  182. return
  183. }
  184. // 支付完成跳转处理
  185. func PayCallback(c *gin.Context) {
  186. log.Debug("zhongshui.payCallback:%v", c)
  187. // c.Redirect(http.StatusMovedPermanently, config.Server.Cropay.Url_resultPay)
  188. }