pay.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package coda
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "bet24.com/log"
  9. "bet24.com/public"
  10. coreClient "bet24.com/servers/coreservice/client"
  11. "bet24.com/servers/payment/config"
  12. "bet24.com/servers/payment/db"
  13. "bet24.com/servers/payment/shop"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // 下单
  17. func PayOrder(c *gin.Context) {
  18. obj := db.NewOrder(db.SP_coda_ORDER)
  19. if err := c.ShouldBind(&obj.In); err != nil {
  20. log.Debug("%s query params err %v", "coda.PayOrder", err)
  21. c.String(http.StatusOK, "")
  22. return
  23. }
  24. obj.In.IpAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  25. //// 币种为空,根据ip获取
  26. //if obj.In.Currency == "" {
  27. // currency := shop.GetCurrencyRateByIp(obj.In.UserID, obj.In.IpAddress)
  28. // obj.In.Currency = currency
  29. //}
  30. // 获取产品信息
  31. item := shop.GetProduct(obj.In.ProductID)
  32. if item == nil {
  33. log.Error("%s query GetProduct productId=%s currency=%s is nil", "coda.PayOrder", obj.In.ProductID, obj.In.Currency)
  34. c.String(http.StatusOK, "")
  35. return
  36. }
  37. //// 获取当前汇率信息
  38. //info := shop.GetExchangeRate(obj.In.Currency)
  39. //if info == nil {
  40. // log.Error("%s query GetExchangeRate obj.In.Currency=%s is nil", "coda.PayOrder", obj.In.Currency)
  41. // c.String(http.StatusOK, "")
  42. // return
  43. //}
  44. //
  45. //// 计算价格
  46. //calPrice := info.Rate * item.Price
  47. //
  48. //// 检查价格是否篡改
  49. //if calPrice != obj.In.Price {
  50. // log.Error("%s obj.In.Price=%v info.Rate=%v calPrice=%v is invalid", "coda.PayOrder", obj.In.Price, info.Rate, calPrice)
  51. // c.String(http.StatusOK, "")
  52. // return
  53. //}
  54. // 检查价格是否篡改
  55. if item.Price != obj.In.Price {
  56. log.Error("%s obj.In.Price=%v calPrice=%v is invalid", "coda.PayOrder", obj.In.Price, item.Price)
  57. c.String(http.StatusOK, "")
  58. return
  59. }
  60. obj.DoAction(nil)
  61. if obj.Out.OrderID == "" {
  62. log.Debug("%s GenOrder fail obj.In=%+v", "coda.PayOrder", obj.In)
  63. c.String(http.StatusOK, "")
  64. return
  65. }
  66. var itemList []item_req
  67. itemList = append(itemList, item_req{
  68. Code: obj.In.ProductID,
  69. Name: obj.In.ProductID,
  70. Price: obj.In.Price,
  71. Type: 1,
  72. })
  73. var entryList []entry_req
  74. entryList = append(entryList, entry_req{
  75. Key: "user_id",
  76. Value: strconv.Itoa(obj.In.UserID),
  77. })
  78. var profileList []profile_req
  79. profileList = append(profileList, profile_req{
  80. Entry: entryList,
  81. })
  82. country := 0
  83. currency := 0
  84. apiKey := ""
  85. for _, v := range config.Server.Coda.ApiKeys {
  86. if v.Currency != obj.In.Currency {
  87. continue
  88. }
  89. country = v.CountryCode
  90. currency = v.CurrencyCode
  91. apiKey = v.ApiKey
  92. break
  93. }
  94. // 判断是否无效
  95. if country == 0 || currency == 0 || apiKey == "" {
  96. log.Error("coda.PayOrder obj.In=%+v is invalid", obj.In)
  97. return
  98. }
  99. // 请求payOrder的代码
  100. req := init_req{InitRequest: initRequest{
  101. ApiKey: apiKey,
  102. OrderId: obj.Out.OrderID,
  103. Country: country,
  104. Currency: currency,
  105. PayType: 0,
  106. Items: itemList,
  107. Profiles: profileList,
  108. }}
  109. // 生成签名
  110. checkContent, err := json.Marshal(req)
  111. if err != nil {
  112. log.Error("coda.PayOrder json marshal fail %v", err)
  113. return
  114. }
  115. // POST请求
  116. respBody := public.HttpPostByJson(config.Server.Coda.Url_pay_init, string(checkContent))
  117. log.Debug("coda.payOrder req ==> %+v resp ==> %+v", string(checkContent), respBody)
  118. var resp init_resp
  119. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  120. log.Error("coda.payOrder json unmarshal err %v", err)
  121. return
  122. }
  123. if resp.InitResult.ResultCode != 0 {
  124. log.Error("coda.payOrder init_resp %v", resp)
  125. return
  126. }
  127. begin_url := fmt.Sprintf(config.Server.Coda.Url_pay_begin, resp.InitResult.TxnId)
  128. // 跳转到支付页面,以便持卡人完成支付过程
  129. c.Redirect(http.StatusMovedPermanently, begin_url)
  130. return
  131. }
  132. // 回调通知
  133. func PayNotify(c *gin.Context) {
  134. // bodyData, _ := io.ReadAll(c.Request.Body)
  135. // log.Debug("coda.PayNotify ==> ctx.Request.body: %v", string(bodyData))
  136. var resp payNotify
  137. if err := c.ShouldBind(&resp); err != nil {
  138. log.Debug("%s query params err %v", "coda.PayNotify", err)
  139. c.String(http.StatusOK, "")
  140. return
  141. }
  142. log.Debug("coda.PayNotify resp ==> %+v", resp)
  143. apiKey := ""
  144. for _, v := range config.Server.Coda.ApiKeys {
  145. for _, k := range v.PaymentTypes {
  146. if k != resp.PaymentType {
  147. continue
  148. }
  149. apiKey = v.ApiKey
  150. break
  151. }
  152. if apiKey != "" {
  153. break
  154. }
  155. }
  156. // TxnID + API key + OrderId + ResultCode
  157. data := fmt.Sprintf("%d%s%s%d", resp.TxnId, apiKey, resp.OrderId, resp.ResultCode)
  158. sign := public.GetMd5String(data)
  159. // 验证签名
  160. if sign != resp.Checksum {
  161. log.Error("coda.PayNotify 签名失败 ==> %+v", resp)
  162. return
  163. }
  164. log.Debug("coda.PayNotify 签名成功")
  165. if resp.ResultCode != 0 {
  166. log.Debug("coda.PayNotify resp ==> %+v 接收响应", resp)
  167. c.String(http.StatusOK, "ResultCode=0")
  168. return
  169. }
  170. // 数据库操作
  171. obj := db.NewNotify(db.SP_coda_NOTIFY)
  172. obj.In.OrderID = resp.OrderId
  173. obj.In.TradeID = strconv.Itoa(resp.TxnId)
  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", "coda.PayNotify", resp)
  180. }
  181. c.String(http.StatusOK, "ResultCode=0")
  182. return
  183. }
  184. // 支付完成跳转处理
  185. func PayCallback(c *gin.Context) {
  186. var resp payNotify
  187. if err := c.ShouldBind(&resp); err != nil {
  188. log.Debug("%s query params err %v", "coda.PayCallback", err)
  189. c.String(http.StatusOK, "")
  190. return
  191. }
  192. log.Debug("coda.PayCallback ==> %+v", resp)
  193. c.HTML(http.StatusOK, "coda.html", gin.H{
  194. "title": "Coda Recharge Result",
  195. "outTradeNo": resp.OrderId,
  196. "tradeToken": resp.TxnId,
  197. "status": "SUCCESS",
  198. })
  199. return
  200. }