pay.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package crush
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  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_Crush_ORDER)
  20. if err := c.ShouldBind(&obj.In); err != nil {
  21. log.Debug("%s query params err %v", "crush.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", "crush.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", "crush.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", "crush.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", "crush.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", "crush.PayOrder", obj.In)
  64. c.String(http.StatusOK, "")
  65. return
  66. }
  67. // 请求payOrder的代码
  68. req := pay_req{
  69. MerchantOrderId: obj.Out.OrderID,
  70. OrderAmount: fmt.Sprintf("%f", obj.In.Price),
  71. NotifyUrl: config.Server.CrushPay.Url_pay_Notify,
  72. }
  73. // 生成签名
  74. checkContent, err := json.Marshal(req)
  75. if err != nil {
  76. log.Error("crush.PayOrder json marshal fail %v", err)
  77. return
  78. }
  79. buf, err := public.AesEncrypt(checkContent, []byte(config.Server.CrushPay.MerchantKey))
  80. if err != nil {
  81. log.Error("crush.PayOrder AESEncrypt err %v", err)
  82. return
  83. }
  84. sign := strings.ToUpper(hex.EncodeToString(buf))
  85. // 生成请求的 body
  86. bodyBuf, _ := json.Marshal(struct {
  87. Data string `json:"data"`
  88. }{
  89. Data: sign,
  90. })
  91. body := string(bodyBuf)
  92. // POST请求
  93. respBody := httpPostByJson(config.Server.CrushPay.Url_pay_order, body, config.Server.CrushPay.MerchantAppId)
  94. log.Debug("crush.payOrder req ==> %+v resp ==> %+v", string(checkContent), respBody)
  95. var resp pay_resp
  96. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  97. log.Error("crush.payOrder json unmarshal req ==> %+v resp ==> %+v fail %v", body, respBody, err)
  98. return
  99. }
  100. log.Debug("crush.payOrder resp ==> %+v", resp)
  101. // 返回码,’APPLY_SUCCESS’代表成功
  102. if !resp.Success {
  103. log.Error("crush.payOrder post return resp fail ==> %+v", resp)
  104. return
  105. }
  106. // 跳转到支付页面,以便持卡人完成支付过程
  107. c.Redirect(http.StatusMovedPermanently, resp.Data.PayUrl)
  108. // c.String(http.StatusOK, "Success")
  109. return
  110. }
  111. // 回调通知
  112. func PayNotify(c *gin.Context) {
  113. var Req struct {
  114. Data string `json:"data" form:"data"`
  115. }
  116. if err := c.ShouldBind(&Req); err != nil {
  117. log.Debug("%s query params err %v", "crush.PayNotify", err)
  118. c.String(http.StatusOK, "")
  119. return
  120. }
  121. log.Debug("crush.PayNotify resp ==> %+v", Req)
  122. data, err := hex.DecodeString(Req.Data)
  123. if err != nil {
  124. log.Debug("crush.PayNotify hex.DecodeString %s ==> err %v", Req.Data, err)
  125. c.String(http.StatusOK, "")
  126. return
  127. }
  128. buf, err := public.AesDecrypt(data, []byte(config.Server.CrushPay.MerchantKey))
  129. if err != nil {
  130. log.Debug("crush.PayNotify AESDecrypt err %v", err)
  131. c.String(http.StatusOK, "")
  132. return
  133. }
  134. log.Debug("crush.PayNotify 解析成功 ==> %s", string(buf))
  135. var resp payNotify
  136. if err := json.Unmarshal(buf, &resp); err != nil {
  137. log.Debug("%s query params err %v", "crush.PayNotify", err)
  138. c.String(http.StatusOK, "")
  139. return
  140. }
  141. log.Debug("crush.PayNotify resp ==> %+v", resp)
  142. // 返回码,’APPLY_SUCCESS’代表成功
  143. if resp.Status != "SUCCESS" {
  144. log.Error("crush.PayNotify resp ==> %+v 失败", resp)
  145. c.String(http.StatusOK, "SUCCESS")
  146. return
  147. }
  148. payAmount, err := strconv.Atoi(strings.ReplaceAll(resp.PayAmount, ".", ""))
  149. if err != nil {
  150. log.Error("crush.WithdrawNotify balance err %v", err)
  151. return
  152. }
  153. payAmount = payAmount / 100
  154. // 数据库操作
  155. obj := db.NewNotify(db.SP_Crush_NOTIFY)
  156. obj.In.OrderID = resp.MerchantOrderId
  157. obj.In.TradeID = resp.OrderId
  158. // obj.In.Price = payAmount
  159. obj.DoAction(nil)
  160. // 操作成功,给道具
  161. if obj.Out.RetCode == 1 {
  162. // 充值
  163. resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
  164. log.Debug("%s 充值成功 %+v", "crush.PayNotify", resp)
  165. }
  166. c.String(http.StatusOK, "SUCCESS")
  167. return
  168. }
  169. // 支付完成跳转处理
  170. func PayCallback(c *gin.Context) {
  171. c.String(http.StatusOK, "SUCCESS")
  172. log.Debug("crush.payCallback")
  173. }