util.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package unionpay
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "sort"
  11. "strings"
  12. "time"
  13. "git.chuangxin1.com/cx/util"
  14. "git.chuangxin1.com/cx/util/binding"
  15. )
  16. const (
  17. // TimeFmtNumeric format of time to numeric
  18. TimeFmtNumeric = `20060102150405`
  19. )
  20. var (
  21. base64Encode = base64.StdEncoding.EncodeToString
  22. base64Decode = base64.StdEncoding.DecodeString
  23. hexDump = hex.EncodeToString
  24. )
  25. // TimeToNumericStr 时间转换为数字格式字符串
  26. func TimeToNumericStr(t time.Time) string {
  27. return t.Format(TimeFmtNumeric)
  28. }
  29. // createLinkString unionpay sdk func
  30. func createLinkString(params map[string]string, isSort, isEncode bool) string {
  31. var (
  32. keys []string
  33. ss []string
  34. )
  35. if isSort {
  36. for key := range params {
  37. keys = append(keys, key)
  38. }
  39. sort.Strings(keys)
  40. for _, v := range keys {
  41. value := v + "="
  42. if isEncode {
  43. value += url.QueryEscape(params[v])
  44. } else {
  45. value += params[v]
  46. }
  47. ss = append(ss, value)
  48. }
  49. } else {
  50. for k, v := range params {
  51. value := k + "="
  52. if isEncode {
  53. value += url.QueryEscape(v)
  54. } else {
  55. value += v
  56. }
  57. ss = append(ss, value)
  58. }
  59. }
  60. return strings.Join(ss, "&")
  61. }
  62. // POST 提交请求
  63. func post(uri string, body []byte) (res ReqRespose, err error) {
  64. var msg util.Message
  65. headers := make(map[string]string)
  66. headers["Accept"] = "text/html; charset=UTF-8"
  67. headers["Accept-Encoding"] = "gzip, deflate, br"
  68. headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
  69. msg, err = util.Post(uri, "", "", headers, bytes.NewReader(body))
  70. if err != nil {
  71. return
  72. }
  73. if msg.StatusCode != http.StatusOK {
  74. err = fmt.Errorf("Request status code %d", msg.StatusCode)
  75. return
  76. }
  77. //*
  78. fmt.Println("*************************************")
  79. fmt.Println(uri)
  80. fmt.Println(msg.StatusCode)
  81. fmt.Println(msg.Header)
  82. fmt.Println(string(msg.Body))
  83. fmt.Println("*************************************")
  84. // */
  85. var values url.Values
  86. values, err = url.ParseQuery(string(msg.Body))
  87. if err != nil {
  88. return
  89. }
  90. err = binding.MapForm(&res, values)
  91. //*
  92. if err != nil {
  93. return
  94. }
  95. bs, _ := json.Marshal(&res)
  96. fmt.Println(string(bs))
  97. fmt.Println("respCode", res.RespCode, "respMsg", res.RespMsg)
  98. // */
  99. return
  100. }