pay.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package lets
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "bet24.com/servers/payment/shop"
  11. "bet24.com/log"
  12. "bet24.com/public"
  13. coreClient "bet24.com/servers/coreservice/client"
  14. "bet24.com/servers/payment/config"
  15. "bet24.com/servers/payment/db"
  16. "github.com/gin-gonic/gin"
  17. )
  18. // 下单
  19. func PayOrder(c *gin.Context) {
  20. obj := db.NewOrder(db.SP_Lets_ORDER)
  21. if err := c.ShouldBind(&obj.In); err != nil {
  22. log.Debug("%s query params err %v", "lets.PayOrder", err)
  23. c.String(http.StatusOK, "")
  24. return
  25. }
  26. obj.In.IpAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  27. //// 币种为空,根据ip获取
  28. //if obj.In.Currency == "" {
  29. // currency := shop.GetCurrencyRateByIp(obj.In.UserID, obj.In.IpAddress)
  30. // obj.In.Currency = currency
  31. //}
  32. // 获取产品信息
  33. item := shop.GetProduct(obj.In.ProductID)
  34. if item == nil {
  35. log.Error("%s query GetProduct productId=%s currency=%s is nil", "lets.PayOrder", obj.In.ProductID, obj.In.Currency)
  36. c.String(http.StatusOK, "")
  37. return
  38. }
  39. //// 获取当前汇率信息
  40. //info := shop.GetExchangeRate(obj.In.Currency)
  41. //if info == nil {
  42. // log.Error("%s query GetExchangeRate obj.In.Currency=%s is nil", "lets.PayOrder", obj.In.Currency)
  43. // c.String(http.StatusOK, "")
  44. // return
  45. //}
  46. //
  47. //// 计算价格
  48. //calPrice := info.Rate * item.Price
  49. //
  50. //// 检查价格是否篡改
  51. //if calPrice != obj.In.Price {
  52. // log.Error("%s obj.In.Price=%v info.Rate=%v calPrice=%v is invalid", "lets.PayOrder", obj.In.Price, info.Rate, calPrice)
  53. // c.String(http.StatusOK, "")
  54. // return
  55. //}
  56. // 检查价格是否篡改
  57. if item.Price != obj.In.Price {
  58. log.Error("%s obj.In.Price=%v calPrice=%v is invalid", "lets.PayOrder", obj.In.Price, item.Price)
  59. c.String(http.StatusOK, "")
  60. return
  61. }
  62. obj.DoAction(nil)
  63. if obj.Out.OrderID == "" {
  64. log.Debug("%s GenOrder fail obj.In=%+v", "lets.PayOrder", obj.In)
  65. c.String(http.StatusOK, "")
  66. return
  67. }
  68. // 请求payOrder的代码
  69. req := pay_req{
  70. MchId: config.Server.LetsPay.MchId,
  71. OrderNo: obj.Out.OrderID,
  72. Amount: int(math.Ceil(obj.In.Price)),
  73. Product: "indoovo",
  74. Bankcode: "all",
  75. Goods: fmt.Sprintf("email:%s/name:%s/phone:%s", obj.In.Email, obj.In.Name, obj.In.Email),
  76. NotifyUrl: config.Server.LetsPay.Url_pay_notify,
  77. ReturnUrl: config.Server.LetsPay.Url_pay_return,
  78. }
  79. params := url.Values{}
  80. params.Set("mchId", req.MchId)
  81. params.Set("orderNo", req.OrderNo)
  82. params.Set("amount", fmt.Sprintf("%d.00", req.Amount))
  83. params.Set("product", req.Product)
  84. params.Set("bankcode", req.Bankcode)
  85. params.Set("goods", req.Goods)
  86. params.Set("notifyUrl", req.NotifyUrl)
  87. params.Set("returnUrl", req.ReturnUrl)
  88. // 生成签名
  89. checkContent := createEncryptStr(params) + "&key=" + config.Server.LetsPay.MD5Key
  90. // log.Debug("lets.payOrder checkContent ==> %s", checkContent)
  91. signature := strings.ToUpper(public.GetMd5String(checkContent))
  92. params.Set("sign", signature)
  93. // POST请求
  94. respBody := public.HttpPostForm(config.Server.LetsPay.Url_pay_order, params)
  95. log.Debug("lets.payOrder req ==> %+v resp ==> %+v", params, respBody)
  96. var resp pay_resp
  97. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  98. log.Error("lets.payOrder json unmarshal req ==> %+v resp ==> %+v fail %v", params, respBody, err)
  99. return
  100. }
  101. log.Debug("lets.payOrder resp ==> %+v", resp)
  102. // 请求响应码
  103. if resp.RetCode != "SUCCESS" || resp.PayUrl == "" {
  104. log.Error("lets.payOrder post return resp fail ==> %+v", resp)
  105. return
  106. }
  107. // 跳转到支付页面,以便持卡人完成支付过程
  108. c.Redirect(http.StatusMovedPermanently, resp.PayUrl)
  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", "lets.PayNotify", err)
  116. c.String(http.StatusOK, "")
  117. return
  118. }
  119. log.Debug("lets.PayNotify resp ==> %+v", resp)
  120. amount := strconv.FormatFloat(resp.Amount, 'f', 2, 64)
  121. params := url.Values{}
  122. params.Set("mchId", config.Server.LetsPay.MchId)
  123. params.Set("orderNo", resp.OrderNo)
  124. params.Set("amount", amount)
  125. params.Set("product", resp.Product)
  126. params.Set("paySuccTime", resp.PaySuccTime)
  127. params.Set("status", resp.Status)
  128. // 生成签名
  129. checkContent := createEncryptStr(params) + "&key=" + config.Server.LetsPay.MD5Key
  130. // log.Debug("lets.PayNotify checkContent ==> %s", checkContent)
  131. signature := strings.ToUpper(public.GetMd5String(checkContent))
  132. // 校验签名
  133. if resp.Sign != signature {
  134. log.Error("lets.PayNotify 签名失败 ==> %+v", resp)
  135. return
  136. }
  137. log.Debug("lets.PayNotify 签名成功")
  138. // 1 支付中,2 成功,5 失效
  139. if resp.Status != "2" {
  140. log.Error("lets.PayNotify resp ==> %+v 失败", resp)
  141. return
  142. }
  143. //price, err := strconv.Atoi(strings.ReplaceAll(amount, ".00", ""))
  144. //if err != nil {
  145. // log.Error("lets.PayNotify price err %v", err)
  146. // return
  147. //}
  148. // 数据库操作
  149. obj := db.NewNotify(db.SP_Lets_NOTIFY)
  150. obj.In.OrderID = resp.OrderNo
  151. obj.In.TradeID = resp.PaySuccTime
  152. //obj.In.Price = price
  153. obj.DoAction(nil)
  154. // 操作成功,给道具
  155. if obj.Out.RetCode == 1 {
  156. // 充值
  157. resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
  158. log.Debug("%s 充值成功 %+v", "lets.PayNotify", resp)
  159. }
  160. c.String(http.StatusOK, "success")
  161. return
  162. }
  163. // 支付完成跳转处理
  164. func PayCallback(c *gin.Context) {
  165. log.Debug("lets.payCallback:%v", c)
  166. // c.Redirect(http.StatusMovedPermanently, config.Server.Cropay.Url_resultPay)
  167. }