util.go 2.2 KB

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