param.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package qq
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "crypto/tls"
  7. "crypto/x509"
  8. "encoding/hex"
  9. "encoding/xml"
  10. "errors"
  11. "fmt"
  12. "hash"
  13. "io/ioutil"
  14. "strings"
  15. "github.com/iGoogle-ink/gopay"
  16. )
  17. // 添加QQ证书 Path 路径
  18. // certFilePath:apiclient_cert.pem 路径
  19. // keyFilePath:apiclient_key.pem 路径
  20. // pkcs12FilePath:apiclient_cert.p12 路径
  21. // 返回err
  22. func (w *Client) AddCertFilePath(certFilePath, keyFilePath, pkcs12FilePath interface{}) (err error) {
  23. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  24. return err
  25. }
  26. cert, err := ioutil.ReadFile(certFilePath.(string))
  27. if err != nil {
  28. return fmt.Errorf("ioutil.ReadFile:%w", err)
  29. }
  30. key, err := ioutil.ReadFile(keyFilePath.(string))
  31. if err != nil {
  32. return fmt.Errorf("ioutil.ReadFile:%w", err)
  33. }
  34. pkcs, err := ioutil.ReadFile(pkcs12FilePath.(string))
  35. if err != nil {
  36. return fmt.Errorf("ioutil.ReadFile:%w", err)
  37. }
  38. certificate, err := tls.X509KeyPair(cert, key)
  39. if err != nil {
  40. return fmt.Errorf("tls.LoadX509KeyPair:%w", err)
  41. }
  42. pkcsPool := x509.NewCertPool()
  43. pkcsPool.AppendCertsFromPEM(pkcs)
  44. w.mu.Lock()
  45. w.certificate = certificate
  46. w.certPool = pkcsPool
  47. w.mu.Unlock()
  48. return nil
  49. }
  50. func checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath interface{}) error {
  51. if certFilePath != nil && keyFilePath != nil && pkcs12FilePath != nil {
  52. if v, ok := certFilePath.(string); !ok || v == gopay.NULL {
  53. return errors.New("certFilePath not string type or is null string")
  54. }
  55. if v, ok := keyFilePath.(string); !ok || v == gopay.NULL {
  56. return errors.New("keyFilePath not string type or is null string")
  57. }
  58. if v, ok := pkcs12FilePath.(string); !ok || v == gopay.NULL {
  59. return errors.New("pkcs12FilePath not string type or is null string")
  60. }
  61. return nil
  62. }
  63. if !(certFilePath == nil && keyFilePath == nil && pkcs12FilePath == nil) {
  64. return errors.New("cert paths must all nil or all not nil")
  65. }
  66. return nil
  67. }
  68. // 生成请求XML的Body体
  69. func generateXml(bm gopay.BodyMap) (reqXml string) {
  70. bs, err := xml.Marshal(bm)
  71. if err != nil {
  72. return gopay.NULL
  73. }
  74. return string(bs)
  75. }
  76. // 获取QQ支付正式环境Sign值
  77. func getReleaseSign(apiKey string, signType string, bm gopay.BodyMap) (sign string) {
  78. var h hash.Hash
  79. if signType == SignType_HMAC_SHA256 {
  80. h = hmac.New(sha256.New, []byte(apiKey))
  81. } else {
  82. h = md5.New()
  83. }
  84. h.Write([]byte(bm.EncodeWeChatSignParams(apiKey)))
  85. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  86. }
  87. func (q *Client) addCertConfig(certFilePath, keyFilePath, pkcs12FilePath interface{}) (tlsConfig *tls.Config, err error) {
  88. if certFilePath == nil && keyFilePath == nil && pkcs12FilePath == nil {
  89. q.mu.RLock()
  90. defer q.mu.RUnlock()
  91. if &q.certificate != nil && q.certPool != nil {
  92. tlsConfig = &tls.Config{
  93. Certificates: []tls.Certificate{q.certificate},
  94. RootCAs: q.certPool,
  95. InsecureSkipVerify: true,
  96. }
  97. return tlsConfig, nil
  98. }
  99. }
  100. if certFilePath != nil && keyFilePath != nil && pkcs12FilePath != nil {
  101. cert, err := ioutil.ReadFile(certFilePath.(string))
  102. if err != nil {
  103. return nil, fmt.Errorf("ioutil.ReadFile:%w", err)
  104. }
  105. key, err := ioutil.ReadFile(keyFilePath.(string))
  106. if err != nil {
  107. return nil, fmt.Errorf("ioutil.ReadFile:%w", err)
  108. }
  109. pkcs, err := ioutil.ReadFile(pkcs12FilePath.(string))
  110. if err != nil {
  111. return nil, fmt.Errorf("ioutil.ReadFile:%w", err)
  112. }
  113. pkcsPool := x509.NewCertPool()
  114. pkcsPool.AppendCertsFromPEM(pkcs)
  115. certificate, err := tls.X509KeyPair(cert, key)
  116. if err != nil {
  117. return nil, fmt.Errorf("tls.LoadX509KeyPair:%w", err)
  118. }
  119. tlsConfig = &tls.Config{
  120. Certificates: []tls.Certificate{certificate},
  121. RootCAs: pkcsPool,
  122. InsecureSkipVerify: true}
  123. return tlsConfig, nil
  124. }
  125. return nil, errors.New("cert paths must all nil or all not nil")
  126. }