wechat_params.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package gopay
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "crypto/tls"
  7. "crypto/x509"
  8. "encoding/hex"
  9. "errors"
  10. "fmt"
  11. "hash"
  12. "io/ioutil"
  13. "strings"
  14. )
  15. type Country int
  16. // 设置支付国家(默认:中国国内)
  17. // 根据支付地区情况设置国家
  18. // country:<China:中国国内,China2:中国国内(冗灾方案),SoutheastAsia:东南亚,Other:其他国家>
  19. func (w *WeChatClient) SetCountry(country Country) (client *WeChatClient) {
  20. w.mu.Lock()
  21. switch country {
  22. case China:
  23. w.BaseURL = wxBaseUrlCh
  24. case China2:
  25. w.BaseURL = wxBaseUrlCh2
  26. case SoutheastAsia:
  27. w.BaseURL = wxBaseUrlHk
  28. case Other:
  29. w.BaseURL = wxBaseUrlUs
  30. default:
  31. w.BaseURL = wxBaseUrlCh
  32. }
  33. w.mu.Unlock()
  34. return w
  35. }
  36. // 添加微信证书 Byte 数组
  37. // certFile:apiclient_cert.pem byte数组
  38. // keyFile:apiclient_key.pem byte数组
  39. // pkcs12File:apiclient_cert.p12 byte数组
  40. func (w *WeChatClient) AddCertFileByte(certFile, keyFile, pkcs12File []byte) {
  41. w.mu.Lock()
  42. w.CertFile = certFile
  43. w.KeyFile = keyFile
  44. w.Pkcs12File = pkcs12File
  45. w.mu.Unlock()
  46. }
  47. // 添加微信证书 Path 路径
  48. // certFilePath:apiclient_cert.pem 路径
  49. // keyFilePath:apiclient_key.pem 路径
  50. // pkcs12FilePath:apiclient_cert.p12 路径
  51. // 返回err
  52. func (w *WeChatClient) AddCertFilePath(certFilePath, keyFilePath, pkcs12FilePath string) (err error) {
  53. var (
  54. cert, key, pkcs []byte
  55. )
  56. if cert, err = ioutil.ReadFile(certFilePath); err != nil {
  57. return
  58. }
  59. if key, err = ioutil.ReadFile(keyFilePath); err != nil {
  60. return
  61. }
  62. if pkcs, err = ioutil.ReadFile(pkcs12FilePath); err != nil {
  63. return
  64. }
  65. w.mu.Lock()
  66. w.CertFile = cert
  67. w.KeyFile = key
  68. w.Pkcs12File = pkcs
  69. w.mu.Unlock()
  70. return
  71. }
  72. func (w *WeChatClient) 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 != null && keyFilePath != null && pkcs12FilePath != null {
  79. if pkcs, err = ioutil.ReadFile(pkcs12FilePath); err != nil {
  80. return nil, fmt.Errorf("ioutil.ReadFile:%s", err.Error())
  81. }
  82. pkcsPool.AppendCertsFromPEM(pkcs)
  83. if certificate, err = tls.LoadX509KeyPair(certFilePath, keyFilePath); err != nil {
  84. return nil, fmt.Errorf("tls.LoadX509KeyPair:%s", err.Error())
  85. }
  86. tlsConfig = &tls.Config{
  87. Certificates: []tls.Certificate{certificate},
  88. RootCAs: pkcsPool,
  89. InsecureSkipVerify: true}
  90. return
  91. }
  92. w.mu.RLock()
  93. pkcsPool.AppendCertsFromPEM(w.Pkcs12File)
  94. certificate, err = tls.X509KeyPair(w.CertFile, w.KeyFile)
  95. w.mu.RUnlock()
  96. if err != nil {
  97. return nil, fmt.Errorf("tls.X509KeyPair:%s", err.Error())
  98. }
  99. tlsConfig = &tls.Config{
  100. Certificates: []tls.Certificate{certificate},
  101. RootCAs: pkcsPool,
  102. InsecureSkipVerify: true}
  103. return
  104. }
  105. // 获取微信支付正式环境Sign值
  106. func getWeChatReleaseSign(apiKey string, signType string, bm BodyMap) (sign string) {
  107. var h hash.Hash
  108. if signType == SignType_HMAC_SHA256 {
  109. h = hmac.New(sha256.New, []byte(apiKey))
  110. } else {
  111. h = md5.New()
  112. }
  113. h.Write([]byte(bm.EncodeWeChatSignParams(apiKey)))
  114. sign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  115. return
  116. }
  117. // 获取微信支付沙箱环境Sign值
  118. func getWeChatSignBoxSign(mchId, apiKey string, bm BodyMap) (sign string, err error) {
  119. var (
  120. sandBoxApiKey string
  121. h hash.Hash
  122. )
  123. if sandBoxApiKey, err = getSanBoxKey(mchId, GetRandomString(32), apiKey, SignType_MD5); err != nil {
  124. return
  125. }
  126. h = md5.New()
  127. h.Write([]byte(bm.EncodeWeChatSignParams(sandBoxApiKey)))
  128. sign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  129. return
  130. }
  131. // 从微信提供的接口获取:SandboxSignKey
  132. func getSanBoxKey(mchId, nonceStr, apiKey, signType string) (key string, err error) {
  133. bm := make(BodyMap)
  134. bm.Set("mch_id", mchId)
  135. bm.Set("nonce_str", nonceStr)
  136. //沙箱环境:获取沙箱环境ApiKey
  137. if key, err = getSanBoxSignKey(mchId, nonceStr, getWeChatReleaseSign(apiKey, signType, bm)); err != nil {
  138. return
  139. }
  140. return
  141. }
  142. // 从微信提供的接口获取:SandboxSignKey
  143. func getSanBoxSignKey(mchId, nonceStr, sign string) (key string, err error) {
  144. reqs := make(BodyMap)
  145. reqs.Set("mch_id", mchId)
  146. reqs.Set("nonce_str", nonceStr)
  147. reqs.Set("sign", sign)
  148. keyResponse := new(getSignKeyResponse)
  149. _, errs := NewHttpClient().Type(TypeXML).Post(wxSandboxGetsignkey).SendString(generateXml(reqs)).EndStruct(keyResponse)
  150. if len(errs) > 0 {
  151. return null, errs[0]
  152. }
  153. if keyResponse.ReturnCode == "FAIL" {
  154. return null, errors.New(keyResponse.ReturnMsg)
  155. }
  156. return keyResponse.SandboxSignkey, nil
  157. }
  158. // 生成请求XML的Body体
  159. func generateXml(bm BodyMap) (reqXml string) {
  160. var buffer strings.Builder
  161. buffer.WriteString("<xml>")
  162. for key := range bm {
  163. buffer.WriteByte('<')
  164. buffer.WriteString(key)
  165. buffer.WriteString("><![CDATA[")
  166. buffer.WriteString(bm.Get(key))
  167. buffer.WriteString("]]></")
  168. buffer.WriteString(key)
  169. buffer.WriteByte('>')
  170. }
  171. buffer.WriteString("</xml>")
  172. reqXml = buffer.String()
  173. return
  174. }