paycenter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "github.com/pkg/errors"
  9. "go-common/app/interface/live/app-room/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "net/http"
  13. "net/url"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. const (
  20. customID = "10005"
  21. )
  22. //PayCenterWallet Wallet get user bcoin doc:http://info.bilibili.co/pages/viewpage.action?pageId=7559096
  23. func (d *Dao) PayCenterWallet(c context.Context, mid int64, platform string) (wallet *model.Wallet, err error) {
  24. var plat int
  25. if platform == "ios" {
  26. plat = 1
  27. } else if platform == "android" {
  28. plat = 2
  29. } else if platform == "pc" {
  30. plat = 3
  31. } else {
  32. err = ecode.ParamInvalid
  33. return
  34. }
  35. platStr := strconv.Itoa(plat)
  36. params := url.Values{}
  37. params.Set("customerId", customID)
  38. params.Set("platformType", platStr)
  39. params.Set("mid", strconv.FormatInt(mid, 10))
  40. params.Set("traceId", strconv.FormatInt(time.Now().Unix(), 10))
  41. params.Set("timestamp", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
  42. params.Set("signType", "MD5")
  43. params.Set("token", d.c.HTTPClient.PayCenter.Secret)
  44. type pJSON struct {
  45. CustomerID string `json:"customerId"`
  46. PlatformType int `json:"platformType"`
  47. Mid int64 `json:"mid"`
  48. TraceID string `json:"traceId"`
  49. Timestamp string `json:"timestamp"`
  50. SignType string `json:"signType"`
  51. Sign string `json:"sign"`
  52. }
  53. tmp := encode(params)
  54. mh := md5.Sum([]byte(tmp))
  55. sign := hex.EncodeToString(mh[:])
  56. p := &pJSON{
  57. CustomerID: customID,
  58. PlatformType: plat,
  59. Mid: mid,
  60. TraceID: params.Get("traceId"),
  61. Timestamp: params.Get("timestamp"),
  62. SignType: params.Get("signType"),
  63. Sign: sign,
  64. }
  65. bs, _ := json.Marshal(p)
  66. req, _ := http.NewRequest("POST", d.payCenterWalletURL, strings.NewReader(string(bs)))
  67. req.Header.Set("Content-Type", "application/json")
  68. var res struct {
  69. Code int `json:"errno"`
  70. Msg string `json:"msg"`
  71. Data struct {
  72. // DefaultBp float32 `json:"defaultBp"`
  73. CouponBalance float32 `json:"couponBalance"`
  74. AvailableBp float32 `json:"availableBp"`
  75. DefaultBp float32 `json:"defaultBp"`
  76. IosBp float32 `json:"iosBp"`
  77. } `json:"data"`
  78. }
  79. if err = d.payCenterClient.Do(c, req, &res); err != nil {
  80. return
  81. }
  82. if res.Code != 0 {
  83. err = errors.Wrap(ecode.Int(res.Code), d.payCenterWalletURL+"?"+string(bs))
  84. log.Error("account pay url(%s) error(%v) %s", d.payCenterWalletURL+"?"+string(bs), res.Code, res.Msg)
  85. return
  86. }
  87. wallet = &model.Wallet{
  88. Mid: mid,
  89. BcoinBalance: res.Data.AvailableBp,
  90. CouponBalance: res.Data.CouponBalance,
  91. DefaultBp: res.Data.DefaultBp,
  92. IosBp: res.Data.IosBp,
  93. }
  94. return
  95. }
  96. func encode(v url.Values) string {
  97. if v == nil {
  98. return ""
  99. }
  100. var buf bytes.Buffer
  101. keys := make([]string, 0, len(v))
  102. ht := false
  103. for k := range v {
  104. if k == "token" {
  105. ht = true
  106. continue
  107. }
  108. keys = append(keys, k)
  109. }
  110. sort.Strings(keys)
  111. if ht { // 需要把token放在最后 支付中心的规则
  112. keys = append(keys, "token")
  113. }
  114. for _, k := range keys {
  115. vs := v[k]
  116. prefix := k + "="
  117. for _, v := range vs {
  118. if buf.Len() > 0 {
  119. buf.WriteByte('&')
  120. }
  121. buf.WriteString(prefix)
  122. buf.WriteString(v)
  123. }
  124. }
  125. return buf.String()
  126. }