param.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (q *Client) AddCertFilePath(certFilePath, keyFilePath, pkcs12FilePath string) (err error) {
  23. cert, err := ioutil.ReadFile(certFilePath)
  24. if err != nil {
  25. return fmt.Errorf("ioutil.ReadFile:%w", err)
  26. }
  27. key, err := ioutil.ReadFile(keyFilePath)
  28. if err != nil {
  29. return fmt.Errorf("ioutil.ReadFile:%w", err)
  30. }
  31. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  32. if err != nil {
  33. return fmt.Errorf("ioutil.ReadFile:%w", err)
  34. }
  35. certificate, err := tls.X509KeyPair(cert, key)
  36. if err != nil {
  37. return fmt.Errorf("tls.LoadX509KeyPair:%w", err)
  38. }
  39. pkcsPool := x509.NewCertPool()
  40. pkcsPool.AppendCertsFromPEM(pkcs)
  41. q.mu.Lock()
  42. q.certificate = certificate
  43. q.certPool = pkcsPool
  44. q.mu.Unlock()
  45. return nil
  46. }
  47. // 生成请求XML的Body体
  48. func generateXml(bm gopay.BodyMap) (reqXml string) {
  49. bs, err := xml.Marshal(bm)
  50. if err != nil {
  51. return gopay.NULL
  52. }
  53. return string(bs)
  54. }
  55. // 获取QQ支付正式环境Sign值
  56. func getReleaseSign(apiKey string, signType string, bm gopay.BodyMap) (sign string) {
  57. var h hash.Hash
  58. if signType == SignType_HMAC_SHA256 {
  59. h = hmac.New(sha256.New, []byte(apiKey))
  60. } else {
  61. h = md5.New()
  62. }
  63. h.Write([]byte(bm.EncodeWeChatSignParams(apiKey)))
  64. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  65. }
  66. func (q *Client) addCertConfig(certFilePath, keyFilePath, pkcs12FilePath string) (tlsConfig *tls.Config, err error) {
  67. if certFilePath == gopay.NULL && keyFilePath == gopay.NULL && pkcs12FilePath == gopay.NULL {
  68. q.mu.RLock()
  69. defer q.mu.RUnlock()
  70. if &q.certificate != nil && q.certPool != nil {
  71. tlsConfig = &tls.Config{
  72. Certificates: []tls.Certificate{q.certificate},
  73. RootCAs: q.certPool,
  74. InsecureSkipVerify: true,
  75. }
  76. return tlsConfig, nil
  77. }
  78. }
  79. if certFilePath != gopay.NULL && keyFilePath != gopay.NULL && pkcs12FilePath != gopay.NULL {
  80. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  81. if err != nil {
  82. return nil, fmt.Errorf("ioutil.ReadFile:%w", err)
  83. }
  84. pkcsPool := x509.NewCertPool()
  85. pkcsPool.AppendCertsFromPEM(pkcs)
  86. certificate, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
  87. if err != nil {
  88. return nil, fmt.Errorf("tls.LoadX509KeyPair:%w", err)
  89. }
  90. tlsConfig = &tls.Config{
  91. Certificates: []tls.Certificate{certificate},
  92. RootCAs: pkcsPool,
  93. InsecureSkipVerify: true}
  94. return tlsConfig, nil
  95. }
  96. return nil, errors.New("certificate file path must be all input or all input null")
  97. }