param.go 3.0 KB

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