pay.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/app/interface/main/web/model"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. "go-common/library/net/metadata"
  16. "github.com/pkg/errors"
  17. )
  18. // Wallet get user bcoin doc:http://info.bilibili.co/pages/viewpage.action?pageId=7559096
  19. func (d *Dao) Wallet(c context.Context, mid int64) (wallet *model.Wallet, err error) {
  20. const (
  21. plat = 3
  22. platStr = "3"
  23. customID = "10008"
  24. )
  25. params := url.Values{}
  26. params.Set("customerId", customID)
  27. params.Set("platformType", platStr)
  28. params.Set("mid", strconv.FormatInt(mid, 10))
  29. params.Set("traceId", strconv.FormatInt(time.Now().Unix(), 10))
  30. params.Set("timestamp", strconv.FormatInt(time.Now().UnixNano()/1000, 10))
  31. params.Set("signType", "MD5")
  32. params.Set("appkey", d.c.HTTPClient.Pay.Key)
  33. type pJSON struct {
  34. CustomerID string `json:"customerId"`
  35. PlatformType int `json:"platformType"`
  36. Mid int64 `json:"mid"`
  37. TraceID string `json:"traceId"`
  38. Timestamp string `json:"timestamp"`
  39. SignType string `json:"signType"`
  40. Appkey string `json:"appkey"`
  41. Sign string `json:"sign"`
  42. }
  43. tmp := params.Encode() + d.c.HTTPClient.Pay.Secret
  44. if strings.IndexByte(tmp, '+') > -1 {
  45. tmp = strings.Replace(tmp, "+", "%20", -1)
  46. }
  47. mh := md5.Sum([]byte(tmp))
  48. sign := hex.EncodeToString(mh[:])
  49. p := &pJSON{
  50. CustomerID: customID,
  51. PlatformType: plat,
  52. Mid: mid,
  53. TraceID: params.Get("traceId"),
  54. Timestamp: params.Get("timestamp"),
  55. SignType: params.Get("signType"),
  56. Appkey: params.Get("appkey"),
  57. Sign: sign,
  58. }
  59. bs, _ := json.Marshal(p)
  60. req, _ := http.NewRequest("POST", d.walletURL, strings.NewReader(string(bs)))
  61. req.Header.Set("Content-Type", "application/json")
  62. var res struct {
  63. Code int `json:"code"`
  64. Data struct {
  65. // DefaultBp float32 `json:"defaultBp"`
  66. CouponBalance float32 `json:"couponBalance"`
  67. AvailableBp float32 `json:"availableBp"`
  68. } `json:"data"`
  69. }
  70. if err = d.httpPay.Do(c, req, &res); err != nil {
  71. return
  72. }
  73. if res.Code != 0 {
  74. err = errors.Wrap(ecode.Int(res.Code), d.walletURL+"?"+params.Encode())
  75. log.Error("account pay url(%s) error(%v)", d.walletURL+"?"+params.Encode(), res.Code)
  76. return
  77. }
  78. wallet = &model.Wallet{
  79. Mid: mid,
  80. BcoinBalance: res.Data.AvailableBp,
  81. CouponBalance: res.Data.CouponBalance,
  82. }
  83. return
  84. }
  85. // OldWallet get wallet info
  86. func (d *Dao) OldWallet(c context.Context, mid int64) (w *model.Wallet, err error) {
  87. var (
  88. params = url.Values{}
  89. remoteIP = metadata.String(c, metadata.RemoteIP)
  90. )
  91. params.Set("mid", strconv.FormatInt(mid, 10))
  92. var res struct {
  93. Code int `json:"code"`
  94. Data *model.Wallet `json:"data"`
  95. }
  96. err = d.httpR.Get(c, d.walletOldURL, remoteIP, params, &res)
  97. if err != nil {
  98. log.Error("account pay url(%s) error(%v)", d.walletOldURL+"?"+params.Encode(), err)
  99. return
  100. }
  101. if res.Code != 0 {
  102. log.Error("account pay url(%s) error(%v)", d.walletOldURL+"?"+params.Encode(), res.Code)
  103. err = ecode.Int(res.Code)
  104. return
  105. }
  106. w = res.Data
  107. return
  108. }