forked from go-pay/gopay
/
wechat_client.go
335 lines (314 loc) · 10 KB
/
wechat_client.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package gopay
import (
"crypto/tls"
"encoding/xml"
"errors"
"fmt"
"strings"
"sync"
)
type WeChatClient struct {
AppId string
MchId string
ApiKey string
BaseURL string
CertFile []byte
KeyFile []byte
Pkcs12File []byte
IsProd bool
mu sync.RWMutex
}
// 初始化微信客户端
// appId:应用ID
// mchId:商户ID
// ApiKey:API秘钥值
// IsProd:是否是正式环境
func NewWeChatClient(appId, mchId, apiKey string, isProd bool) (client *WeChatClient) {
return &WeChatClient{
AppId: appId,
MchId: mchId,
ApiKey: apiKey,
IsProd: isProd}
}
// 提交付款码支付
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
func (w *WeChatClient) Micropay(body BodyMap) (wxRsp *WeChatMicropayResponse, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxMicropay)
} else {
bs, err = w.doWeChat(body, wxSandboxMicropay)
}
if err != nil {
return
}
wxRsp = new(WeChatMicropayResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 统一下单
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
func (w *WeChatClient) UnifiedOrder(body BodyMap) (wxRsp *WeChatUnifiedOrderResponse, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxUnifiedorder)
} else {
body.Set("total_fee", 101)
bs, err = w.doWeChat(body, wxSandboxUnifiedorder)
}
if err != nil {
return
}
wxRsp = new(WeChatUnifiedOrderResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 查询订单
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
func (w *WeChatClient) QueryOrder(body BodyMap) (wxRsp *WeChatQueryOrderResponse, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxOrderquery)
} else {
bs, err = w.doWeChat(body, wxSandboxOrderquery)
}
if err != nil {
return
}
wxRsp = new(WeChatQueryOrderResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 关闭订单
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3
func (w *WeChatClient) CloseOrder(body BodyMap) (wxRsp *WeChatCloseOrderResponse, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxCloseorder)
} else {
bs, err = w.doWeChat(body, wxSandboxCloseorder)
}
if err != nil {
return
}
wxRsp = new(WeChatCloseOrderResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 撤销订单
// 注意:如已使用client.AddCertFilePath()或client.AddCertFileByte()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",如方法需单独使用证书,则传证书Path
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
func (w *WeChatClient) Reverse(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp *WeChatReverseResponse, err error) {
var (
bs []byte
tlsConfig *tls.Config
)
if w.IsProd {
if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
bs, err = w.doWeChat(body, wxReverse, tlsConfig)
} else {
bs, err = w.doWeChat(body, wxSandboxReverse)
}
if err != nil {
return
}
wxRsp = new(WeChatReverseResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 申请退款
// 注意:如已使用client.AddCertFilePath()或client.AddCertFileByte()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",如方法需单独使用证书,则传证书Path
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
func (w *WeChatClient) Refund(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp *WeChatRefundResponse, err error) {
var (
bs []byte
tlsConfig *tls.Config
)
if w.IsProd {
if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
bs, err = w.doWeChat(body, wxRefund, tlsConfig)
} else {
bs, err = w.doWeChat(body, wxSandboxRefund)
}
if err != nil {
return
}
wxRsp = new(WeChatRefundResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 查询退款
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5
func (w *WeChatClient) QueryRefund(body BodyMap) (wxRsp *WeChatQueryRefundResponse, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxRefundquery)
} else {
bs, err = w.doWeChat(body, wxSandboxRefundquery)
}
if err != nil {
return
}
wxRsp = new(WeChatQueryRefundResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
}
return
}
// 下载对账单
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6
func (w *WeChatClient) DownloadBill(body BodyMap) (wxRsp string, err error) {
var bs []byte
if w.IsProd {
bs, err = w.doWeChat(body, wxDownloadbill)
} else {
bs, err = w.doWeChat(body, wxSandboxDownloadbill)
}
if err != nil {
return
}
wxRsp = string(bs)
return
}
// 下载资金账单
// 注意:如已使用client.AddCertFilePath()或client.AddCertFileByte()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",如方法需单独使用证书,则传证书Path
// 貌似不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18&index=7
func (w *WeChatClient) DownloadFundFlow(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp string, err error) {
var (
bs []byte
tlsConfig *tls.Config
)
if w.IsProd {
if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return null, err
}
bs, err = w.doWeChat(body, wxDownloadfundflow, tlsConfig)
} else {
bs, err = w.doWeChat(body, wxSandboxDownloadfundflow)
}
if err != nil {
return
}
wxRsp = string(bs)
return
}
// 拉取订单评价数据
// 注意:如已使用client.AddCertFilePath()或client.AddCertFileByte()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",如方法需单独使用证书,则传证书Path
// 貌似不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=11
func (w *WeChatClient) BatchQueryComment(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp string, err error) {
var (
bs []byte
tlsConfig *tls.Config
)
if w.IsProd {
body.Set("sign_type", SignType_HMAC_SHA256)
if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return null, err
}
bs, err = w.doWeChat(body, wxBatchquerycomment, tlsConfig)
} else {
bs, err = w.doWeChat(body, wxSandboxBatchquerycomment)
}
if err != nil {
return
}
wxRsp = string(bs)
return
}
// 企业向微信用户个人付款
// 注意:如已使用client.AddCertFilePath()或client.AddCertFileByte()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",如方法需单独使用证书,则传证书Path
// 注意:此方法未支持沙箱环境,默认正式环境,转账请慎重
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1
func (w *WeChatClient) Transfer(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp *WeChatTransfersResponse, err error) {
body.Set("mch_appid", w.AppId)
body.Set("mchid", w.MchId)
var (
tlsConfig *tls.Config
url = wxBaseUrlCh + wxTransfers
)
if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
body.Set("sign", getWeChatReleaseSign(w.ApiKey, SignType_MD5, body))
httpClient := NewHttpClient().SetTLSConfig(tlsConfig).Type(TypeXML)
if w.BaseURL != null {
w.mu.RLock()
url = w.BaseURL + wxTransfers
w.mu.RUnlock()
}
wxRsp = new(WeChatTransfersResponse)
res, errs := httpClient.Post(url).SendString(generateXml(body)).EndStruct(wxRsp)
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
}
return wxRsp, nil
}
// 公众号纯签约(未完成)
// 文档地址:https://pay.weixin.qq.com/wiki/doc/api/pap.php?chapter=18_1&index=1
func (w *WeChatClient) EntrustPublic(body BodyMap) (bs []byte, err error) {
bs, err = w.doWeChat(body, wxEntrustPublic)
return nil, nil
}
// 向微信发送请求
func (w *WeChatClient) doWeChat(body BodyMap, path string, tlsConfig ...*tls.Config) (bs []byte, err error) {
body.Set("appid", w.AppId)
body.Set("mch_id", w.MchId)
var (
sign string
url = wxBaseUrlCh + path
)
if body.Get("sign") != null {
goto GoRequest
}
if !w.IsProd {
body.Set("sign_type", SignType_MD5)
if sign, err = getWeChatSignBoxSign(w.MchId, w.ApiKey, body); err != nil {
return nil, err
}
} else {
sign = getWeChatReleaseSign(w.ApiKey, body.Get("sign_type"), body)
}
body.Set("sign", sign)
GoRequest:
httpClient := NewHttpClient()
if w.IsProd && tlsConfig != nil {
httpClient.SetTLSConfig(tlsConfig[0])
}
if w.BaseURL != null {
w.mu.RLock()
url = w.BaseURL + path
w.mu.RUnlock()
}
res, bs, errs := httpClient.Type(TypeXML).Post(url).SendString(generateXml(body)).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
}
if strings.Contains(string(bs), "HTML") {
return nil, errors.New(string(bs))
}
return bs, nil
}