forked from smartwalle/alipay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alipay.go
515 lines (435 loc) · 11.9 KB
/
alipay.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package alipay
import (
"crypto"
"crypto/md5"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"sort"
"strings"
"sync"
"time"
"github.com/smartwalle/crypto4go"
)
var (
ErrSignNotFound = errors.New("alipay: sign content not found")
ErrAliPublicKeyNotFound = errors.New("alipay: alipay public key not found")
)
const (
kAliPayPublicKeySN = "alipay-public-key"
kAppAuthToken = "app_auth_token"
)
type Client struct {
mu sync.Mutex
isProduction bool
appId string
apiDomain string
notifyVerifyDomain string
Client *http.Client
location *time.Location
appPrivateKey *rsa.PrivateKey // 应用私钥
appCertSN string
rootCertSN string
aliPublicCertSN string
aliPublicKeyList map[string]*rsa.PublicKey
}
type OptionFunc func(c *Client)
func WithTimeLocation(location *time.Location) OptionFunc {
return func(c *Client) {
c.location = location
}
}
func WithHTTPClient(client *http.Client) OptionFunc {
return func(c *Client) {
c.Client = client
}
}
// New 初始化支付宝客户端
//
// appId - 支付宝应用 id
//
// privateKey - 应用私钥,开发者自己生成
//
// isProduction - 是否为生产环境,传 false 的时候为沙箱环境,用于开发测试,正式上线的时候需要改为 true
func New(appId, privateKey string, isProduction bool, opts ...OptionFunc) (client *Client, err error) {
priKey, err := crypto4go.ParsePKCS1PrivateKey(crypto4go.FormatPKCS1PrivateKey(privateKey))
if err != nil {
priKey, err = crypto4go.ParsePKCS8PrivateKey(crypto4go.FormatPKCS8PrivateKey(privateKey))
if err != nil {
return nil, err
}
}
client = &Client{}
client.isProduction = isProduction
client.appId = appId
if client.isProduction {
client.apiDomain = kProductionURL
client.notifyVerifyDomain = kProductionMAPIURL
} else {
client.apiDomain = kSandboxURL
client.notifyVerifyDomain = kSandboxURL
}
client.Client = http.DefaultClient
client.location = time.Local
client.appPrivateKey = priKey
client.aliPublicKeyList = make(map[string]*rsa.PublicKey)
for _, opt := range opts {
opt(client)
}
return client, nil
}
func (this *Client) IsProduction() bool {
return this.isProduction
}
// LoadAliPayPublicKey 加载支付宝公钥
func (this *Client) LoadAliPayPublicKey(aliPublicKey string) error {
var pub *rsa.PublicKey
var err error
if len(aliPublicKey) < 0 {
return ErrAliPublicKeyNotFound
}
pub, err = crypto4go.ParsePublicKey(crypto4go.FormatPublicKey(aliPublicKey))
if err != nil {
return err
}
this.mu.Lock()
this.aliPublicCertSN = kAliPayPublicKeySN
this.aliPublicKeyList[this.aliPublicCertSN] = pub
this.mu.Unlock()
return nil
}
// LoadAppPublicCert 加载应用公钥证书
func (this *Client) LoadAppPublicCert(s string) error {
cert, err := crypto4go.ParseCertificate([]byte(s))
if err != nil {
return err
}
this.appCertSN = getCertSN(cert)
return nil
}
// LoadAppPublicCertFromFile 加载应用公钥证书
func (this *Client) LoadAppPublicCertFromFile(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return this.LoadAppPublicCert(string(b))
}
// LoadAliPayPublicCert 加载支付宝公钥证书
func (this *Client) LoadAliPayPublicCert(s string) error {
cert, err := crypto4go.ParseCertificate([]byte(s))
if err != nil {
return err
}
key, ok := cert.PublicKey.(*rsa.PublicKey)
if ok == false {
return nil
}
this.mu.Lock()
this.aliPublicCertSN = getCertSN(cert)
this.aliPublicKeyList[this.aliPublicCertSN] = key
this.mu.Unlock()
return nil
}
// LoadAliPayPublicCertFromFile 加载支付宝公钥证书
func (this *Client) LoadAliPayPublicCertFromFile(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return this.LoadAliPayPublicCert(string(b))
}
// LoadAliPayRootCert 加载支付宝根证书
func (this *Client) LoadAliPayRootCert(s string) error {
var certStrList = strings.Split(s, kCertificateEnd)
var certSNList = make([]string, 0, len(certStrList))
for _, certStr := range certStrList {
certStr = certStr + kCertificateEnd
var cert, _ = crypto4go.ParseCertificate([]byte(certStr))
if cert != nil && (cert.SignatureAlgorithm == x509.SHA256WithRSA || cert.SignatureAlgorithm == x509.SHA1WithRSA) {
certSNList = append(certSNList, getCertSN(cert))
}
}
this.rootCertSN = strings.Join(certSNList, "_")
return nil
}
// LoadAliPayRootCertFromFile 加载支付宝根证书
func (this *Client) LoadAliPayRootCertFromFile(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return this.LoadAliPayRootCert(string(b))
}
func (this *Client) URLValues(param Param) (value url.Values, err error) {
var p = url.Values{}
p.Add("app_id", this.appId)
p.Add("method", param.APIName())
p.Add("format", kFormat)
p.Add("charset", kCharset)
p.Add("sign_type", kSignTypeRSA2)
p.Add("timestamp", time.Now().In(this.location).Format(kTimeFormat))
p.Add("version", kVersion)
if this.appCertSN != "" {
p.Add("app_cert_sn", this.appCertSN)
}
if this.rootCertSN != "" {
p.Add("alipay_root_cert_sn", this.rootCertSN)
}
bytes, err := json.Marshal(param)
if err != nil {
return nil, err
}
p.Add("biz_content", string(bytes))
var ps = param.Params()
if ps != nil {
for key, value := range ps {
if key == kAppAuthToken && value == "" {
continue
}
p.Add(key, value)
}
}
sign, err := signWithPKCS1v15(p, this.appPrivateKey, crypto.SHA256)
if err != nil {
return nil, err
}
p.Add("sign", sign)
return p, nil
}
func (this *Client) doRequest(method string, param Param, result interface{}) (err error) {
var buf io.Reader
if param != nil {
p, err := this.URLValues(param)
if err != nil {
return err
}
buf = strings.NewReader(p.Encode())
}
req, err := http.NewRequest(method, this.apiDomain, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", kContentType)
resp, err := this.Client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var dataStr = string(data)
var rootNodeName = strings.Replace(param.APIName(), ".", "_", -1) + kResponseSuffix
var rootIndex = strings.LastIndex(dataStr, rootNodeName)
var errorIndex = strings.LastIndex(dataStr, kErrorResponse)
var content string
var certSN string
var sign string
if rootIndex > 0 {
content, certSN, sign = parseJSONSource(dataStr, rootNodeName, rootIndex)
if sign == "" {
var errRsp *ErrorRsp
if err = json.Unmarshal([]byte(content), &errRsp); err != nil {
return err
}
// alipay.open.app.alipaycert.download(应用支付宝公钥证书下载) 没有返回 sign 字段,所以再判断一次 code
if errRsp.Code != CodeSuccess {
if errRsp != nil {
return errRsp
}
return ErrSignNotFound
}
}
} else if errorIndex > 0 {
content, certSN, sign = parseJSONSource(dataStr, kErrorResponse, errorIndex)
if sign == "" {
var errRsp *ErrorRsp
if err = json.Unmarshal([]byte(content), &errRsp); err != nil {
return err
}
return errRsp
}
} else {
return ErrSignNotFound
}
if sign != "" {
publicKey, err := this.getAliPayPublicKey(certSN)
if err != nil {
return err
}
if ok, err := verifyData([]byte(content), sign, publicKey); ok == false {
return err
}
}
err = json.Unmarshal(data, result)
if err != nil {
return err
}
return err
}
func (this *Client) DoRequest(method string, param Param, result interface{}) (err error) {
return this.doRequest(method, param, result)
}
func (this *Client) VerifySign(data url.Values) (ok bool, err error) {
var certSN = data.Get(kCertSNNodeName)
publicKey, err := this.getAliPayPublicKey(certSN)
if err != nil {
return false, err
}
return verifySign(data, publicKey)
}
func (this *Client) getAliPayPublicKey(certSN string) (key *rsa.PublicKey, err error) {
this.mu.Lock()
defer this.mu.Unlock()
if certSN == "" {
certSN = this.aliPublicCertSN
}
key = this.aliPublicKeyList[certSN]
if key == nil {
if this.isProduction {
cert, err := this.downloadAliPayCert(certSN)
if err != nil {
return nil, err
}
var ok bool
key, ok = cert.PublicKey.(*rsa.PublicKey)
if ok == false {
return nil, ErrAliPublicKeyNotFound
}
} else {
return nil, ErrAliPublicKeyNotFound
}
}
return key, nil
}
func (this *Client) CertDownload(param CertDownload) (result *CertDownloadRsp, err error) {
err = this.doRequest("POST", param, &result)
return result, err
}
func (this *Client) downloadAliPayCert(certSN string) (cert *x509.Certificate, err error) {
var cp = CertDownload{}
cp.AliPayCertSN = certSN
rsp, err := this.CertDownload(cp)
if err != nil {
return nil, err
}
certBytes, err := base64.StdEncoding.DecodeString(rsp.Content.AliPayCertContent)
if err != nil {
return nil, err
}
cert, err = crypto4go.ParseCertificate(certBytes)
if err != nil {
return nil, err
}
key, ok := cert.PublicKey.(*rsa.PublicKey)
if ok == false {
return nil, nil
}
this.aliPublicCertSN = getCertSN(cert)
this.aliPublicKeyList[this.aliPublicCertSN] = key
return cert, nil
}
func parseJSONSource(rawData string, nodeName string, nodeIndex int) (content, certSN, sign string) {
var dataStartIndex = nodeIndex + len(nodeName) + 2
var signIndex = strings.LastIndex(rawData, "\""+kSignNodeName+"\"")
var certIndex = strings.LastIndex(rawData, "\""+kCertSNNodeName+"\"")
var dataEndIndex int
if signIndex > 0 && certIndex > 0 {
dataEndIndex = int(math.Min(float64(signIndex), float64(certIndex))) - 1
} else if certIndex > 0 {
dataEndIndex = certIndex - 1
} else if signIndex > 0 {
dataEndIndex = signIndex - 1
} else {
dataEndIndex = len(rawData) - 1
}
var indexLen = dataEndIndex - dataStartIndex
if indexLen < 0 {
return "", "", ""
}
content = rawData[dataStartIndex:dataEndIndex]
if certIndex > 0 {
var certStartIndex = certIndex + len(kCertSNNodeName) + 4
certSN = rawData[certStartIndex:]
var certEndIndex = strings.Index(certSN, "\"")
certSN = certSN[:certEndIndex]
}
if signIndex > 0 {
var signStartIndex = signIndex + len(kSignNodeName) + 4
sign = rawData[signStartIndex:]
var signEndIndex = strings.LastIndex(sign, "\"")
sign = sign[:signEndIndex]
}
return content, certSN, sign
}
func signWithPKCS1v15(param url.Values, privateKey *rsa.PrivateKey, hash crypto.Hash) (s string, err error) {
if param == nil {
param = make(url.Values, 0)
}
var pList = make([]string, 0, 0)
for key := range param {
var value = strings.TrimSpace(param.Get(key))
if len(value) > 0 {
pList = append(pList, key+"="+value)
}
}
sort.Strings(pList)
var src = strings.Join(pList, "&")
sig, err := crypto4go.RSASignWithKey([]byte(src), privateKey, hash)
if err != nil {
return "", err
}
s = base64.StdEncoding.EncodeToString(sig)
return s, nil
}
func verifySign(data url.Values, key *rsa.PublicKey) (ok bool, err error) {
sign := data.Get(kSignNodeName)
var keys = make([]string, 0, 0)
for k := range data {
if k == kSignNodeName || k == kSignTypeNodeName || k == kCertSNNodeName {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
var buf strings.Builder
for _, k := range keys {
vs := data[k]
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
}
}
s := buf.String()
return verifyData([]byte(s), sign, key)
}
func verifyData(data []byte, sign string, key *rsa.PublicKey) (ok bool, err error) {
signBytes, err := base64.StdEncoding.DecodeString(sign)
if err != nil {
return false, err
}
if err = crypto4go.RSAVerifyWithKey(data, signBytes, key, crypto.SHA256); err != nil {
return false, err
}
return true, nil
}
func getCertSN(cert *x509.Certificate) string {
var value = md5.Sum([]byte(cert.Issuer.String() + cert.SerialNumber.String()))
return hex.EncodeToString(value[:])
}