forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mch.go
343 lines (264 loc) · 6.84 KB
/
mch.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
336
337
338
339
340
341
342
343
package mch
import (
"context"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/accaolei/gochat/wx"
"golang.org/x/crypto/pkcs12"
)
// Mch 微信支付
type Mch struct {
appid string
mchid string
apikey string
nonce func(size int) string
client wx.Client
tlsClient wx.Client
}
// New returns new wechat pay
func New(appid, mchid, apikey string) *Mch {
c := wx.NewHTTPClient(&tls.Config{InsecureSkipVerify: true})
return &Mch{
appid: appid,
mchid: mchid,
apikey: apikey,
nonce: func(size int) string {
nonce := make([]byte, size/2)
io.ReadFull(rand.Reader, nonce)
return hex.EncodeToString(nonce)
},
client: c,
tlsClient: c,
}
}
// LoadCertFromP12File load cert from p12(pfx) file
func (mch *Mch) LoadCertFromP12File(path string) error {
p12, err := ioutil.ReadFile(path)
if err != nil {
return err
}
cert, err := mch.pkcs12ToPem(p12)
if err != nil {
return err
}
mch.tlsClient = wx.NewHTTPClient(&tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
})
return nil
}
// LoadCertFromPemFile load cert from PEM file
func (mch *Mch) LoadCertFromPemFile(certFile, keyFile string) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
mch.tlsClient = wx.NewHTTPClient(&tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
})
return nil
}
// LoadCertFromPemBlock load cert from a pair of PEM encoded data
func (mch *Mch) LoadCertFromPemBlock(certPEMBlock, keyPEMBlock []byte) error {
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return err
}
mch.tlsClient = wx.NewHTTPClient(&tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
})
return nil
}
// Do exec action
func (mch *Mch) Do(ctx context.Context, action wx.Action, options ...wx.HTTPOption) (wx.WXML, error) {
m, err := action.WXML(mch.appid, mch.mchid, mch.nonce(16))
if err != nil {
return nil, err
}
// 签名
if v, ok := m["sign_type"]; ok && v == SignHMacSHA256 {
m["sign"] = mch.SignWithHMacSHA256(m, true)
} else {
m["sign"] = mch.SignWithMD5(m, true)
}
reqURL := action.URL()
switch reqURL {
case ContractOAEntrust: // 公众号签约
query := url.Values{}
for k, v := range m {
query.Add(k, v)
}
return wx.WXML{"entrust_url": fmt.Sprintf("%s?%s", PappayOAEntrustURL, query.Encode())}, nil
case ContractMPEntrust: // 小程序签约
return m, nil
case ContractH5Entrust: // H5签约
query := url.Values{}
for k, v := range m {
query.Add(k, v)
}
return wx.WXML{"entrust_url": fmt.Sprintf("%s?%s", PappayH5EntrustURL, query.Encode())}, nil
}
var resp wx.WXML
if action.TLS() {
resp, err = mch.tlsClient.PostXML(ctx, reqURL, m, options...)
} else {
resp, err = mch.client.PostXML(ctx, reqURL, m, options...)
}
if err != nil {
return nil, err
}
if resp["return_code"] != ResultSuccess {
return nil, errors.New(resp["return_msg"])
}
if err := mch.VerifyWXMLResult(resp); err != nil {
return nil, err
}
return resp, nil
}
// APPAPI 用于APP拉起支付
func (mch *Mch) APPAPI(prepayID string) wx.WXML {
m := wx.WXML{
"appid": mch.appid,
"partnerid": mch.mchid,
"prepayid": prepayID,
"package": "Sign=WXPay",
"noncestr": mch.nonce(16),
"timestamp": strconv.FormatInt(time.Now().Unix(), 10),
}
m["sign"] = mch.SignWithMD5(m, true)
return m
}
// JSAPI 用于JS拉起支付
func (mch *Mch) JSAPI(prepayID string) wx.WXML {
m := wx.WXML{
"appId": mch.appid,
"nonceStr": mch.nonce(16),
"package": fmt.Sprintf("prepay_id=%s", prepayID),
"signType": SignMD5,
"timeStamp": strconv.FormatInt(time.Now().Unix(), 10),
}
m["paySign"] = mch.SignWithMD5(m, true)
return m
}
// MinipRedpackJSAPI 小程序领取红包
func (mch *Mch) MinipRedpackJSAPI(pkg string) wx.WXML {
m := wx.WXML{
"appId": mch.appid,
"nonceStr": mch.nonce(16),
"package": url.QueryEscape(pkg),
"timeStamp": strconv.FormatInt(time.Now().Unix(), 10),
}
m["paySign"] = mch.SignWithMD5(m, false)
delete(m, "appId")
m["signType"] = SignMD5
return m
}
// SignWithMD5 生成MD5签名
func (mch *Mch) SignWithMD5(m wx.WXML, toUpper bool) string {
h := md5.New()
h.Write([]byte(mch.buildSignStr(m)))
sign := hex.EncodeToString(h.Sum(nil))
if toUpper {
sign = strings.ToUpper(sign)
}
return sign
}
// SignWithHMacSHA256 生成HMAC-SHA256签名
func (mch *Mch) SignWithHMacSHA256(m wx.WXML, toUpper bool) string {
h := hmac.New(sha256.New, []byte(mch.apikey))
h.Write([]byte(mch.buildSignStr(m)))
sign := hex.EncodeToString(h.Sum(nil))
if toUpper {
sign = strings.ToUpper(sign)
}
return sign
}
// VerifyWXMLResult 微信请求/回调通知签名验证
func (mch *Mch) VerifyWXMLResult(m wx.WXML) error {
if wxsign, ok := m["sign"]; ok {
signature := ""
if v, ok := m["sign_type"]; ok && v == SignHMacSHA256 {
signature = mch.SignWithHMacSHA256(m, true)
} else {
signature = mch.SignWithMD5(m, true)
}
if wxsign != signature {
return fmt.Errorf("signature verified failed, want: %s, got: %s", signature, wxsign)
}
}
if appid, ok := m["appid"]; ok {
if appid != mch.appid {
return fmt.Errorf("appid mismatch, want: %s, got: %s", mch.appid, m["appid"])
}
}
if mchid, ok := m["mch_id"]; ok {
if mchid != mch.mchid {
return fmt.Errorf("mchid mismatch, want: %s, got: %s", mch.mchid, m["mch_id"])
}
}
return nil
}
// DecryptWithAES256ECB AES-256-ECB解密(主要用于退款结果通知)
func (mch *Mch) DecryptWithAES256ECB(encrypt string) (wx.WXML, error) {
cipherText, err := base64.StdEncoding.DecodeString(encrypt)
if err != nil {
return nil, err
}
h := md5.New()
h.Write([]byte(mch.apikey))
ecb := wx.NewECBCrypto([]byte(hex.EncodeToString(h.Sum(nil))))
plainText, err := ecb.Decrypt(cipherText, wx.PKCS7)
if err != nil {
return nil, err
}
return wx.ParseXML2Map(plainText)
}
func (mch *Mch) pkcs12ToPem(p12 []byte) (tls.Certificate, error) {
blocks, err := pkcs12.ToPEM(p12, mch.mchid)
if err != nil {
return tls.Certificate{}, err
}
pemData := make([]byte, 0)
for _, b := range blocks {
pemData = append(pemData, pem.EncodeToMemory(b)...)
}
// then use PEM data for tls to construct tls certificate:
return tls.X509KeyPair(pemData, pemData)
}
// Sign 生成签名
func (mch *Mch) buildSignStr(m wx.WXML) string {
l := len(m)
ks := make([]string, 0, l)
kvs := make([]string, 0, l)
for k := range m {
if k == "sign" {
continue
}
ks = append(ks, k)
}
sort.Strings(ks)
for _, k := range ks {
if v, ok := m[k]; ok && v != "" {
kvs = append(kvs, fmt.Sprintf("%s=%s", k, v))
}
}
kvs = append(kvs, fmt.Sprintf("key=%s", mch.apikey))
return strings.Join(kvs, "&")
}