Score
0
Watch 31 Star 84 Fork 20

北京小程科技有限公司 / 微信Go SDKGoMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
微信支付、公众号、小程序相关后端接口的Go语言实现。 spread retract

Clone or download
client_verify_sign.go 1.24 KB
Copy Edit Web IDE Raw Blame History
shallot authored 2019-08-19 18:25 . 更新说明文档和测试用例。
package wechat
import (
"errors"
"github.com/beevik/etree"
)
// 验证微信返回的结果签名
func (c *Client) doVerifySign(xmlStr []byte, breakWhenFail bool) (err error) {
// 生成XML文档
doc := etree.NewDocument()
if err = doc.ReadFromBytes(xmlStr); err != nil {
return
}
root := doc.SelectElement("xml")
// 验证return_code
retCode := root.SelectElement("return_code").Text()
if retCode != ResponseSuccess && breakWhenFail {
return
}
// 遍历所有Tag,生成Map和Sign
result, targetSign := make(map[string]interface{}), ""
for _, elem := range root.ChildElements() {
// 跳过空值
if elem.Text() == "" || elem.Text() == "0" {
continue
}
if elem.Tag != "sign" {
result[elem.Tag] = elem.Text()
} else {
targetSign = elem.Text()
}
}
// 获取签名类型
signType := SignTypeMD5
if result["sign_type"] != nil {
signType = result["sign_type"].(string)
}
// 生成签名
var sign string
if c.isProd {
sign = c.localSign(result, signType, c.apiKey)
} else {
key, iErr := c.sandboxSign(result["nonce_str"].(string), SignTypeMD5)
if err = iErr; iErr != nil {
return
}
sign = c.localSign(result, SignTypeMD5, key)
}
// 验证
if targetSign != sign {
err = errors.New("签名无效")
}
return
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/cuckoopark/wechat.git
git@gitee.com:cuckoopark/wechat.git
cuckoopark
wechat
微信Go SDK
master

Help Search