wechat_params.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package gopay
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/xml"
  8. "errors"
  9. "strings"
  10. )
  11. type Country int
  12. //设置支付国家(默认:中国国内)
  13. // 根据支付地区情况设置国家
  14. // country:<China:中国国内,China2:中国国内(冗灾方案),SoutheastAsia:东南亚,Other:其他国家>
  15. func (this *weChatClient) SetCountry(country Country) (client *weChatClient) {
  16. switch country {
  17. case China:
  18. this.baseURL = wx_base_url_ch
  19. case China2:
  20. this.baseURL = wx_base_url_ch2
  21. case SoutheastAsia:
  22. this.baseURL = wx_base_url_hk
  23. case Other:
  24. this.baseURL = wx_base_url_us
  25. default:
  26. this.baseURL = wx_base_url_ch
  27. }
  28. return this
  29. }
  30. //本地通过支付参数计算Sign值
  31. func getLocalSign(apiKey string, signType string, bm BodyMap) (sign string) {
  32. signStr := bm.EncodeWeChatSignParams(apiKey)
  33. //fmt.Println("signStr:", signStr)
  34. var hashSign []byte
  35. if signType == SignType_HMAC_SHA256 {
  36. hash := hmac.New(sha256.New, []byte(apiKey))
  37. hash.Write([]byte(signStr))
  38. hashSign = hash.Sum(nil)
  39. } else {
  40. hash := md5.New()
  41. hash.Write([]byte(signStr))
  42. hashSign = hash.Sum(nil)
  43. }
  44. sign = strings.ToUpper(hex.EncodeToString(hashSign))
  45. return
  46. }
  47. //从微信提供的接口获取:SandboxSignKey
  48. func getSanBoxSign(mchId, nonceStr, apiKey, signType string) (key string, err error) {
  49. body := make(BodyMap)
  50. body.Set("mch_id", mchId)
  51. body.Set("nonce_str", nonceStr)
  52. //计算沙箱参数Sign
  53. sanboxSign := getLocalSign(apiKey, signType, body)
  54. //沙箱环境:获取key后,重新计算Sign
  55. key, err = getSanBoxSignKey(mchId, nonceStr, sanboxSign)
  56. if err != nil {
  57. return null, err
  58. }
  59. return
  60. }
  61. //从微信提供的接口获取:SandboxSignkey
  62. func getSanBoxSignKey(mchId, nonceStr, sign string) (key string, err error) {
  63. reqs := make(BodyMap)
  64. reqs.Set("mch_id", mchId)
  65. reqs.Set("nonce_str", nonceStr)
  66. reqs.Set("sign", sign)
  67. reqXml := generateXml(reqs)
  68. //fmt.Println("req:::", reqXml)
  69. _, byteList, errorList := HttpAgent().
  70. Post(wx_SanBox_GetSignKey).
  71. Type("xml").
  72. SendString(reqXml).EndBytes()
  73. if len(errorList) > 0 {
  74. return null, errorList[0]
  75. }
  76. keyResponse := new(getSignKeyResponse)
  77. err = xml.Unmarshal(byteList, keyResponse)
  78. if err != nil {
  79. return null, err
  80. }
  81. if keyResponse.ReturnCode == "FAIL" {
  82. return null, errors.New(keyResponse.ReturnMsg)
  83. }
  84. return keyResponse.SandboxSignkey, nil
  85. }
  86. //生成请求XML的Body体
  87. func generateXml(bm BodyMap) (reqXml string) {
  88. var buffer strings.Builder
  89. buffer.WriteString("<xml>")
  90. for key := range bm {
  91. buffer.WriteByte('<')
  92. buffer.WriteString(key)
  93. buffer.WriteString("><![CDATA[")
  94. buffer.WriteString(bm.Get(key))
  95. buffer.WriteString("]]></")
  96. buffer.WriteString(key)
  97. buffer.WriteByte('>')
  98. }
  99. buffer.WriteString("</xml>")
  100. reqXml = buffer.String()
  101. return
  102. }