pay.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package liu
  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. coreClient "bet24.com/servers/coreservice/client"
  13. "bet24.com/servers/payment/config"
  14. "bet24.com/servers/payment/db"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // 下单
  18. func PayOrder(c *gin.Context) {
  19. obj := db.NewOrder(db.SP_Liu_ORDER)
  20. if err := c.ShouldBind(&obj.In); err != nil {
  21. log.Debug("%s query params err %v", "liu.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", "liu.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", "liu.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", "liu.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", "liu.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", "liu.PayOrder", obj.In)
  64. c.String(http.StatusOK, "")
  65. return
  66. }
  67. // 请求payOrder的代码
  68. req := pay_req{
  69. MerchantCode: config.Server.LiuPay.MerchantCode,
  70. Ccy_no: "IDR",
  71. OrderNo: obj.Out.OrderID,
  72. Menty: int(math.Ceil(obj.In.Price)),
  73. TypeCode: "yinnixunihubniva",
  74. Wares: obj.In.ProductID,
  75. NotifyUrl: config.Server.LiuPay.Url_pay_notify,
  76. PageUrl: config.Server.LiuPay.Url_pay_page,
  77. }
  78. params := url.Values{}
  79. params.Set("merchantCode", req.MerchantCode)
  80. params.Set("ccy_no", req.Ccy_no)
  81. params.Set("orderNo", req.OrderNo)
  82. params.Set("menty", strconv.Itoa(req.Menty))
  83. params.Set("typeCode", req.TypeCode)
  84. params.Set("wares", req.Wares)
  85. params.Set("notifyUrl", req.NotifyUrl)
  86. params.Set("pageUrl", req.PageUrl)
  87. // 生成签名
  88. checkContent := createEncryptStr(params) + "&key=" + config.Server.LiuPay.MD5Key
  89. // log.Debug("liu.payOrder checkContent ==> %s", checkContent)
  90. signature := strings.ToUpper(public.GetMd5String(checkContent))
  91. req.Signature = signature
  92. // POST请求
  93. buf, _ := json.Marshal(req)
  94. respBody := public.HttpPostByJson(config.Server.LiuPay.Url_pay_order, string(buf))
  95. log.Debug("liu.payOrder req ==> %+v resp ==> %+v", string(buf), respBody)
  96. var resp pay_resp
  97. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  98. log.Error("liu.payOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  99. return
  100. }
  101. log.Debug("liu.payOrder resp ==> %+v", resp)
  102. // 请求响应码
  103. if resp.Err_code != "000002" || resp.Status != "SUCCESS" || resp.Order_data == "" {
  104. log.Error("liu.payOrder post return resp fail ==> %+v", resp)
  105. return
  106. }
  107. // 跳转到支付页面,以便持卡人完成支付过程
  108. c.Redirect(http.StatusMovedPermanently, resp.Order_data)
  109. return
  110. }
  111. // 回调通知
  112. func PayNotify(c *gin.Context) {
  113. var resp payNotify
  114. if err := c.ShouldBind(&resp); err != nil {
  115. log.Debug("%s query params err %v", "liu.PayNotify", err)
  116. c.String(http.StatusOK, "")
  117. return
  118. }
  119. log.Debug("liu.PayNotify resp ==> %+v", resp)
  120. if resp.Err_code != "000000" {
  121. log.Error("liu.PayNotify resp ==> %+v 失败", resp)
  122. return
  123. }
  124. params := url.Values{}
  125. params.Set("typeCode", resp.TypeCode)
  126. params.Set("err_code", resp.Err_code)
  127. params.Set("err_msg", resp.Err_msg)
  128. params.Set("merchantCode", config.Server.LiuPay.MerchantCode)
  129. params.Set("orderNo", resp.OrderNo)
  130. params.Set("amount", strconv.Itoa(resp.Amount))
  131. params.Set("terraceNo", resp.TerraceNo)
  132. params.Set("orderTime", resp.OrderTime)
  133. params.Set("payAmount", resp.PayAmount)
  134. params.Set("payTime", resp.PayTime)
  135. params.Set("reserver", resp.Reserver)
  136. params.Set("status", resp.Status)
  137. // 生成签名
  138. checkContent := createEncryptStr(params) + "&key=" + config.Server.LiuPay.MD5Key
  139. // log.Debug("liu.PayNotify checkContent ==> %s", checkContent)
  140. signature := strings.ToLower(public.GetMd5String(checkContent))
  141. // 校验签名
  142. if resp.Signature != signature {
  143. log.Error("liu.PayNotify 签名失败 ==> %+v", resp)
  144. return
  145. }
  146. log.Debug("liu.PayNotify 签名成功")
  147. if resp.Status != "SUCCESS" {
  148. log.Error("liu.PayNotify resp ==> %+v 失败", resp)
  149. return
  150. }
  151. // 数据库操作
  152. obj := db.NewNotify(db.SP_Liu_NOTIFY)
  153. obj.In.OrderID = resp.OrderNo
  154. obj.In.TradeID = resp.TerraceNo
  155. //obj.In.Price = resp.Amount
  156. obj.DoAction(nil)
  157. // 操作成功,给道具
  158. if obj.Out.RetCode == 1 {
  159. // 充值
  160. resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
  161. log.Debug("%s 充值成功 %+v", "liu.PayNotify", resp)
  162. }
  163. c.String(http.StatusOK, "SUCCESS")
  164. return
  165. }
  166. // 支付完成跳转处理
  167. func PayCallback(c *gin.Context) {
  168. log.Debug("lets.payCallback")
  169. // c.Redirect(http.StatusMovedPermanently, config.Server.Cropay.Url_resultPay)
  170. }