-
Notifications
You must be signed in to change notification settings - Fork 5
/
alipay.go
346 lines (293 loc) · 8.4 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
// Package alipay provides alipay related api. See details on https://docs.open.alipay.com/api_1
package alipay
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/google/go-querystring/query"
)
// AlipayGateway ...
const (
AlipayGateway = "https://openapi.alipay.com/gateway.do"
)
var client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: time.Second * 60,
}
// CommonParam ...
type CommonParam struct {
AppID string `url:"app_id,omitempty"` // 支付宝分配给开发者的应用ID
Method string `url:"method,omitempty"` // 接口名称
Format string `url:"format,omitempty"` // 仅支持JSON
ReturnURL string `url:"return_url,omitempty"` // 同步返回地址
Charset string `url:"charset,omitempty"` // 请求使用的编码格式,如utf-8,gbk,gb2312等
SignType string `url:"sign_type,omitempty"` // 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
Sign string `url:"sign,omitempty"` // 商户请求参数的签名串
Timestamp string `url:"timestamp,omitempty"` // 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss"
Version string `url:"version,omitempty"` // 调用的接口版本,固定为:1.0
NotifyURL string `url:"notify_url,omitempty"` // 支付宝服务器主动通知商户服务器里指定的页面http/https路径。
AppAuthToken string `url:"app_auth_token,omitempty"` // 详见应用授权概述
BizContent string `url:"biz_content,omitempty"` // 请求参数的集合
}
var defaultCommonParam = CommonParam{
Format: "JSON",
Charset: "utf-8",
SignType: "RSA2",
Version: "1.0",
}
// Alipay ...
type Alipay struct {
appID string
*rsa.PrivateKey
*rsa.PublicKey
client *http.Client
}
// HTTPClient ...
func (alipay *Alipay) HTTPClient() *http.Client {
return alipay.client
}
// AppID ...
func (alipay *Alipay) AppID() string {
return alipay.appID
}
// Fill ...
type Fill func(*CommonParam)
// WithNotifyURL ...
func WithNotifyURL(notifyURL string) Fill {
return func(cp *CommonParam) {
cp.NotifyURL = notifyURL
}
}
// WithReturnURL ...
func WithReturnURL(returnURL string) Fill {
return func(cp *CommonParam) {
cp.ReturnURL = returnURL
}
}
// WithAppAuthToken ...
func WithAppAuthToken(authToken string) Fill {
return func(cp *CommonParam) {
cp.AppAuthToken = authToken
}
}
// WithSignType ...
func WithSignType(signType string) Fill {
return func(cp *CommonParam) {
cp.SignType = signType
}
}
// MakeParam ...
func (alipay *Alipay) MakeParam(content interface{}, method string, fillList ...Fill) (string, error) {
biz, err := json.Marshal(&content)
if err != nil {
return "", fmt.Errorf("支付宝请求业务参数序列化失败: %w", err)
}
requestParam := CommonParam{
AppID: alipay.appID,
Method: method,
Format: defaultCommonParam.Format,
Charset: defaultCommonParam.Charset,
SignType: defaultCommonParam.SignType,
Timestamp: time.Now().Format("2006-01-02 15:04:05"),
Version: defaultCommonParam.Version,
BizContent: string(biz),
}
for _, fill := range fillList {
fill(&requestParam)
}
requestSignValues, err := query.Values(&requestParam)
if err != nil {
return "", fmt.Errorf("支付宝请求参数序列化失败: %w", err)
}
requestSignedStr := NormValues(requestSignValues)
requestSign, err := RSA2(alipay.PrivateKey, requestSignedStr)
if err != nil {
return "", fmt.Errorf("支付宝商户私钥签名不成功: %w", err)
}
requestParam.Sign = requestSign
values, err := query.Values(&requestParam)
if err != nil {
return "", fmt.Errorf("支付宝请求结构体不能序列化: %w", err)
}
return values.Encode(), nil
}
// OnRequest ...
func (alipay *Alipay) OnRequest(content interface{}, method string, fillList ...Fill) (int, []byte, error) {
request, err := http.NewRequest(http.MethodGet, AlipayGateway, nil)
if err != nil {
return 0, nil, fmt.Errorf("支付宝生成请求失败: %w", err)
}
param, err := alipay.MakeParam(content, method, fillList...)
if err != nil {
return 0, nil, fmt.Errorf("支付宝构造请求参数失败: %w", err)
}
request.URL.RawQuery = param
response, err := alipay.client.Do(request)
if err != nil {
return 0, nil, fmt.Errorf("支付宝发起请求失败: %w", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, nil, fmt.Errorf("支付宝请求结果读取body失败: %w", err)
}
obj := make(map[string]*json.RawMessage)
if err := json.Unmarshal(body, &obj); err != nil {
return 0, nil, fmt.Errorf("支付宝请求结果反序列化失败: %w", err)
}
var (
data []byte
sig []byte
)
for k, v := range obj {
if strings.Contains(k, "response") {
data = []byte(*v)
break
}
}
if rawMessage, ok := obj["sign"]; ok {
sig = []byte(*rawMessage)
}
if len(sig) > 0 {
var base64SignStr string
if err := json.Unmarshal(sig, &base64SignStr); err != nil {
return 0, nil, fmt.Errorf("反序列化签名失败: %w", err)
}
signStr, err := base64.StdEncoding.DecodeString(base64SignStr)
if err != nil {
return 0, nil, fmt.Errorf("base64解码签名失败: %w", err)
}
if err := Verify(alipay.PublicKey, data, signStr); err != nil {
return 0, nil, fmt.Errorf("支付宝同步请求签名验证不通过: %w", err)
}
}
return response.StatusCode, data, nil
}
func (alipay *Alipay) asyncVerifyRequest(values url.Values) error {
// 验证参数步骤
// 1) 去掉sign, sign_type
// 2) 对剩下参数进行url decode
// 3) 字典序排序
// 4) sign解码
base64Sign := values.Get("sign")
toVerifyValues := url.Values{}
for key, list := range values {
if key == "sign" || key == "sign_type" {
continue
}
for _, value := range list {
toVerifyValues.Add(key, value)
}
}
normToVerifyStr := NormValues(toVerifyValues)
signBytes, _ := base64.StdEncoding.DecodeString(base64Sign)
return Verify(alipay.PublicKey, []byte(normToVerifyStr), signBytes)
}
// NormValues ...
func NormValues(v url.Values) string {
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k, l := range v {
if len(l) == 0 {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := v[k]
prefix := k + "="
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
if v == "" {
continue
}
buf.WriteString(prefix)
buf.WriteString(v)
}
}
return buf.String()
}
// New ...
func New(appID string, pubBytes, privateBytes []byte) (*Alipay, error) {
alipay := Alipay{
appID: appID,
client: client,
}
var err error
alipay.PublicKey, err = NewPublicKey(pubBytes)
if err != nil {
return nil, fmt.Errorf("支付宝公钥构建失败: %w", err)
}
alipay.PrivateKey, err = NewPrivateKey(privateBytes)
if err != nil {
return nil, fmt.Errorf("商户私钥构建失败: %w", err)
}
return &alipay, nil
}
// NewPublicKey publicKey参照ParsePKIXPublicKey
func NewPublicKey(pubBytes []byte) (pub *rsa.PublicKey, err error) {
pubBlock, _ := pem.Decode(pubBytes)
pubInterface, err := x509.ParsePKIXPublicKey(pubBlock.Bytes)
if err != nil {
return nil, err
}
publicKey, ok := pubInterface.(*rsa.PublicKey)
if !ok {
return nil, errors.New("public key format error")
}
return publicKey, nil
}
// NewPrivateKey privateKey格式参照ParsePKCS1PrivateKey
func NewPrivateKey(privateBytes []byte) (priKey *rsa.PrivateKey, err error) {
priBlock, _ := pem.Decode(privateBytes)
privateKey, err := x509.ParsePKCS1PrivateKey(priBlock.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
// Verify ...
func Verify(publicKey *rsa.PublicKey, data []byte, sig []byte) error {
h := sha256.New()
h.Write(data)
digest := h.Sum(nil)
return rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, digest, sig)
}
// RSA2 ...
func RSA2(privateKey *rsa.PrivateKey, in string) (string, error) {
hash := sha256.New()
_, err := io.WriteString(hash, in)
if err != nil {
return "", err
}
hashed := hash.Sum(nil)
signBytes, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(signBytes), nil
}