pay_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package pay
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "net/url"
  6. "os"
  7. "testing"
  8. "go-common/app/service/main/ugcpay/conf"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. var (
  12. p *Pay
  13. )
  14. func TestMain(m *testing.M) {
  15. flag.Set("conf", "../../cmd/test.toml")
  16. if err := conf.Init(); err != nil {
  17. panic(err)
  18. }
  19. p = &Pay{
  20. ID: conf.Conf.Biz.Pay.ID,
  21. Token: conf.Conf.Biz.Pay.Token,
  22. RechargeShellNotifyURL: "http://api.bilibili.co/x/internal/ugcpay/trade/recharge/callback",
  23. }
  24. m.Run()
  25. os.Exit(0)
  26. }
  27. func TestCheckOrder(t *testing.T) {
  28. Convey("", t, func() {
  29. param := p.CheckOrder("3059753508505497600")
  30. p.Sign(param)
  31. t.Log(p.ToJSON(param))
  32. })
  33. }
  34. func TestCheckRefundOrder(t *testing.T) {
  35. Convey("", t, func() {
  36. param := p.CheckRefundOrder("3059753508505497600")
  37. p.Sign(param)
  38. t.Log(p.ToJSON(param))
  39. })
  40. }
  41. func TestRechargeShell(t *testing.T) {
  42. var (
  43. orderID = "123"
  44. mid = int64(46333)
  45. assetBP = int64(1)
  46. shell = int64(1)
  47. )
  48. Convey("", t, func() {
  49. _, json, err := p.RechargeShell(orderID, mid, assetBP, shell)
  50. So(err, ShouldBeNil)
  51. t.Log(json)
  52. })
  53. }
  54. func TestSign(t *testing.T) {
  55. Convey("", t, func() {
  56. var (
  57. param = url.Values{
  58. "customerId": []string{"10017"},
  59. "deviceType": []string{"3"},
  60. "notifyUrl": []string{"http://api.bilibili.co/x/internal/ugcpay/trade/pay/callback"},
  61. "orderCreateTime": []string{"1539935981000"},
  62. "orderExpire": []string{"1800"},
  63. "orderId": []string{"224"},
  64. "originalAmount": []string{"2000"},
  65. "payAmount": []string{"2000"},
  66. "productId": []string{"10110688"},
  67. "serviceType": []string{"99"},
  68. "showTitle": []string{"传点什么好呢?"},
  69. "timestamp": []string{"1539935981000"},
  70. "traceId": []string{"1539935981967342977"},
  71. "uid": []string{"27515244"},
  72. "version": []string{"1.0"},
  73. "feeType": []string{"CNY"},
  74. }
  75. )
  76. err := p.Sign(param)
  77. So(err, ShouldBeNil)
  78. pmap := make(map[string]string)
  79. var payBytes []byte
  80. for k, v := range param {
  81. if len(v) > 0 {
  82. pmap[k] = v[0]
  83. }
  84. }
  85. if payBytes, err = json.Marshal(pmap); err != nil {
  86. return
  87. }
  88. t.Log(string(payBytes))
  89. })
  90. }
  91. func TestSignVerify(t *testing.T) {
  92. Convey("", t, func() {
  93. var (
  94. param = url.Values{
  95. "customerId": []string{"10017"},
  96. "deviceType": []string{"3"},
  97. "notifyUrl": []string{"http://api.bilibili.co/x/internal/ugcpay/trade/pay/callback"},
  98. "orderCreateTime": []string{"1539935981000"},
  99. "orderExpire": []string{"1800"},
  100. "orderId": []string{"15"},
  101. "originalAmount": []string{"2000"},
  102. "payAmount": []string{"2000"},
  103. "productId": []string{"10110688"},
  104. "serviceType": []string{"99"},
  105. "showTitle": []string{"传点什么好呢?"},
  106. "timestamp": []string{"1539935981000"},
  107. "traceId": []string{"1539935981967342977"},
  108. "uid": []string{"27515244"},
  109. "version": []string{"1.0"},
  110. "feeType": []string{"CNY"},
  111. }
  112. )
  113. err := p.Sign(param)
  114. So(err, ShouldBeNil)
  115. ok := p.Verify(param)
  116. So(ok, ShouldBeTrue)
  117. })
  118. }