forked from chanxuehong/wechat
/
request.go
202 lines (157 loc) · 5.79 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/chanxuehong/wechat for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/master/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
package feedback
import (
"crypto/subtle"
"encoding/hex"
"errors"
"fmt"
"strconv"
)
// 微信服务器推送过来的消息结构.
// 包含投诉消息, 确认消除投诉消息, 拒绝消除投诉消息
type MsgRequest struct {
XMLName struct{} `xml:"xml" json:"-"`
AppId string `xml:"AppId"` // 公众号 id
TimeStamp int64 `xml:"TimeStamp"` // 时间戳, unixtime
OpenId string `xml:"OpenId"` // 支付该笔订单的用户 OpenId
FeedbackId int64 `xml:"FeedBackId"` // 投诉单号
MsgType string `xml:"MsgType"` // request
TransactionId string `xml:"TransId"` // 交易订单号
Reason string `xml:"Reason"` // 用户投诉的原因
Solution string `xml:"Solution"` // 用户希望解决方案
ExtInfo string `xml:"ExtInfo"` // 备注+电话
Signature string `xml:"AppSignature"` // 签名
SignMethod string `xml:"SignMethod"` // 签名方法, sha1
PicInfo []struct {
PicURL string `xml:"PicUrl"`
} `xml:"PicInfo>item"` // 用户上传的图片凭证, 最多 5 张
}
// 检查 req *MsgRequest 是否合法(包括签名的检查), 合法返回 nil, 否则返回错误信息.
// @paySignKey: 公众号支付请求中用于加密的密钥 Key, 对应于支付场景中的 appKey
func (req *MsgRequest) Check(paySignKey string) (err error) {
// 检查签名
var hashSumLen, twoHashSumLen int
var sumFunc hashSumFunc
switch req.SignMethod {
case "sha1", "SHA1":
hashSumLen = 40
twoHashSumLen = 80
sumFunc = sha1Sum
default:
err = fmt.Errorf(`not implement for "%s" sign method`, req.SignMethod)
return
}
if len(req.Signature) != hashSumLen {
err = errors.New("签名不正确")
return
}
TimeStampStr := strconv.FormatInt(req.TimeStamp, 10)
const keysLen = len(`appid=&appkey=&openid=×tamp=`)
n := twoHashSumLen + keysLen + len(req.AppId) + len(paySignKey) +
len(req.OpenId) + len(TimeStampStr)
// buf[:hashSumLen] 保存参数 req.Signature,
// buf[hashSumLen:twoHashSumLen] 保存生成的签名
// buf[twoHashSumLen:] 按照字典序列保存 string1
buf := make([]byte, n)
reqSignature := buf[:hashSumLen]
signature := buf[hashSumLen:twoHashSumLen]
string1 := buf[twoHashSumLen:twoHashSumLen]
copy(reqSignature, req.Signature)
// 字典序
// appid
// appkey
// openid
// timestamp
string1 = append(string1, "appid="...)
string1 = append(string1, req.AppId...)
string1 = append(string1, "&appkey="...)
string1 = append(string1, paySignKey...)
string1 = append(string1, "&openid="...)
string1 = append(string1, req.OpenId...)
string1 = append(string1, "×tamp="...)
string1 = append(string1, TimeStampStr...)
hex.Encode(signature, sumFunc(string1))
// 采用 subtle.ConstantTimeCompare 是防止 计时攻击!
if subtle.ConstantTimeCompare(reqSignature, signature) != 1 {
err = errors.New("签名不正确")
return
}
return
}
// 从 MsgRequest 获取投诉消息
func (msg *MsgRequest) GetRequest() *Request {
return (*Request)(msg)
}
// 从 MsgRequest 获取 Confirm 消息
func (msg *MsgRequest) GetConfirm() *Confirm {
return &Confirm{
XMLName: msg.XMLName,
AppId: msg.AppId,
TimeStamp: msg.TimeStamp,
OpenId: msg.OpenId,
FeedbackId: msg.FeedbackId,
MsgType: msg.MsgType,
Reason: msg.Reason,
Signature: msg.Signature,
SignMethod: msg.SignMethod,
}
}
// 从 MsgRequest 获取 Reject 消息
func (msg *MsgRequest) GetReject() *Reject {
return &Reject{
XMLName: msg.XMLName,
AppId: msg.AppId,
TimeStamp: msg.TimeStamp,
OpenId: msg.OpenId,
FeedbackId: msg.FeedbackId,
MsgType: msg.MsgType,
Reason: msg.Reason,
Signature: msg.Signature,
SignMethod: msg.SignMethod,
}
}
// 用户提交投诉消息
type Request struct {
XMLName struct{} `xml:"xml" json:"-"`
AppId string `xml:"AppId"` // 公众号 id
TimeStamp int64 `xml:"TimeStamp"` // 时间戳, unixtime
OpenId string `xml:"OpenId"` // 支付该笔订单的用户 OpenId
FeedbackId int64 `xml:"FeedBackId"` // 投诉单号
MsgType string `xml:"MsgType"` // request
TransactionId string `xml:"TransId"` // 交易订单号
Reason string `xml:"Reason"` // 用户投诉的原因
Solution string `xml:"Solution"` // 用户希望解决方案
ExtInfo string `xml:"ExtInfo"` // 备注+电话
Signature string `xml:"AppSignature"` // 签名
SignMethod string `xml:"SignMethod"` // 签名方法, sha1
PicInfo []struct {
PicURL string `xml:"PicUrl"`
} `xml:"PicInfo>item"` // 用户上传的图片凭证, 最多 5 张
}
// 用户确认消除投诉
type Confirm struct {
XMLName struct{} `xml:"xml" json:"-"`
AppId string `xml:"AppId"` // 公众号 id
TimeStamp int64 `xml:"TimeStamp"` // 时间戳, unixtime
OpenId string `xml:"OpenId"` // 支付该笔订单的用户 OpenId
FeedbackId int64 `xml:"FeedBackId"` // 投诉单号
MsgType string `xml:"MsgType"` // confirm
Reason string `xml:"Reason"`
Signature string `xml:"AppSignature"` // 签名
SignMethod string `xml:"SignMethod"` // 签名方法, sha1
}
// 用户拒绝消除投诉
type Reject struct {
XMLName struct{} `xml:"xml" json:"-"`
AppId string `xml:"AppId"` // 公众号 id
TimeStamp int64 `xml:"TimeStamp"` // 时间戳, unixtime
OpenId string `xml:"OpenId"` // 支付该笔订单的用户 OpenId
FeedbackId int64 `xml:"FeedBackId"` // 投诉单号
MsgType string `xml:"MsgType"` // reject
Reason string `xml:"Reason"` // 拒绝原因
Signature string `xml:"AppSignature"` // 签名
SignMethod string `xml:"SignMethod"` // 签名方法, sha1
}