forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
309 lines (289 loc) · 8.82 KB
/
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
package alipay
import (
"crypto/rsa"
"encoding/json"
"fmt"
"time"
"github.com/cedarwu/gopay"
"github.com/cedarwu/gopay/pkg/util"
"github.com/cedarwu/gopay/pkg/xhttp"
"github.com/cedarwu/gopay/pkg/xlog"
"github.com/cedarwu/gopay/pkg/xpem"
"github.com/cedarwu/gopay/pkg/xrsa"
)
type Client struct {
AppId string
AppCertSN string
AliPayPublicCertSN string
AliPayRootCertSN string
ReturnUrl string
NotifyUrl string
Charset string
SignType string
AppAuthToken string
IsProd bool
privateKey *rsa.PrivateKey
aliPayPublicKey *rsa.PublicKey // 支付宝证书公钥内容 alipayCertPublicKey_RSA2.crt
autoSign bool
DebugSwitch gopay.DebugSwitch
location *time.Location
}
// 初始化支付宝客户端
// 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
// appId:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境
func NewClient(appId, privateKey string, isProd bool) (client *Client, err error) {
key := xrsa.FormatAlipayPrivateKey(privateKey)
priKey, err := xpem.DecodePrivateKey([]byte(key))
if err != nil {
return nil, err
}
client = &Client{
AppId: appId,
Charset: UTF8,
SignType: RSA2,
IsProd: isProd,
privateKey: priKey,
DebugSwitch: gopay.DebugOff,
}
return client, nil
}
// 开启请求完自动验签功能(默认不开启,推荐开启,只支持证书模式)
// 注意:只支持证书模式
// alipayPublicKeyContent:支付宝公钥证书文件内容[]byte
func (a *Client) AutoVerifySign(alipayPublicKeyContent []byte) {
pubKey, err := xpem.DecodePublicKey(alipayPublicKeyContent)
if err != nil {
xlog.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
}
if pubKey != nil {
a.aliPayPublicKey = pubKey
a.autoSign = true
}
}
// Deprecated
// 推荐使用 PostAliPayAPISelfV2()
// 示例:请参考 client_test.go 的 TestClient_PostAliPayAPISelf() 方法
func (a *Client) PostAliPayAPISelf(bm gopay.BodyMap, method string, aliRsp interface{}) (err error) {
var bs []byte
if bs, err = a.doAliPay(bm, method); err != nil {
return err
}
if err = json.Unmarshal(bs, aliRsp); err != nil {
return err
}
return nil
}
// Deprecated
// 推荐使用 RequestParam()
func (a *Client) GetRequestSignParam(bm gopay.BodyMap, method string) (string, error) {
return a.RequestParam(bm, method)
}
// RequestParam 获取支付宝完整请求参数包含签名
// 注意:biz_content 需要自行通过bm.SetBodyMap()设置,不设置则没有此参数
func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {
var (
bodyBs []byte
err error
sign string
)
// check if there is biz_content
bz := bm.GetInterface("biz_content")
if bzBody, ok := bz.(gopay.BodyMap); ok {
if bodyBs, err = json.Marshal(bzBody); err != nil {
return "", fmt.Errorf("json.Marshal(%v):%w", bzBody, err)
}
bm.Set("biz_content", string(bodyBs))
}
bm.Set("method", method)
// check public parameter
a.checkPublicParam(bm)
// check sign
if bm.GetString("sign") == "" {
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.privateKey)
if err != nil {
return "", fmt.Errorf("GetRsaSign Error: %v", err)
}
bm.Set("sign", sign)
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
}
return bm.EncodeURLParams(), nil
}
// PostAliPayAPISelfV2 支付宝接口自行实现方法
// 注意:biz_content 需要自行通过bm.SetBodyMap()设置,不设置则没有此参数
// 示例:请参考 client_test.go 的 TestClient_PostAliPayAPISelfV2() 方法
func (a *Client) PostAliPayAPISelfV2(bm gopay.BodyMap, method string, aliRsp interface{}) (err error) {
var (
bs, bodyBs []byte
)
// check if there is biz_content
bz := bm.GetInterface("biz_content")
if bzBody, ok := bz.(gopay.BodyMap); ok {
if bodyBs, err = json.Marshal(bzBody); err != nil {
return fmt.Errorf("json.Marshal(%v):%w", bzBody, err)
}
bm.Set("biz_content", string(bodyBs))
}
if bs, err = a.doAliPaySelf(bm, method); err != nil {
return err
}
if err = json.Unmarshal(bs, aliRsp); err != nil {
return err
}
return nil
}
// 向支付宝发送自定义请求
func (a *Client) doAliPaySelf(bm gopay.BodyMap, method string) (bs []byte, err error) {
var (
url, sign string
)
bm.Set("method", method)
// check public parameter
a.checkPublicParam(bm)
// check sign
if bm.GetString("sign") == "" {
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.privateKey)
if err != nil {
return nil, fmt.Errorf("GetRsaSign Error: %v", err)
}
bm.Set("sign", sign)
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
}
httpClient := xhttp.NewClient()
if a.IsProd {
url = baseUrlUtf8
} else {
url = sandboxBaseUrlUtf8
}
res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(bm.EncodeURLParams()).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
}
return bs, nil
}
// 向支付宝发送请求
func (a *Client) doAliPay(bm gopay.BodyMap, method string, authToken ...string) (bs []byte, err error) {
var (
bodyStr, url string
bodyBs []byte
aat string
)
if bm != nil {
aat = bm.GetString("app_auth_token")
bm.Remove("app_auth_token")
if bodyBs, err = json.Marshal(bm); err != nil {
return nil, fmt.Errorf("json.Marshal:%w", err)
}
bodyStr = string(bodyBs)
}
pubBody := make(gopay.BodyMap)
pubBody.Set("app_id", a.AppId).
Set("method", method).
Set("format", "JSON").
Set("charset", a.Charset).
Set("sign_type", a.SignType).
Set("version", "1.0").
Set("timestamp", time.Now().Format(util.TimeLayout))
if a.AppCertSN != util.NULL {
pubBody.Set("app_cert_sn", a.AppCertSN)
}
if a.AliPayRootCertSN != util.NULL {
pubBody.Set("alipay_root_cert_sn", a.AliPayRootCertSN)
}
if a.ReturnUrl != util.NULL {
pubBody.Set("return_url", a.ReturnUrl)
}
if a.location != nil {
pubBody.Set("timestamp", time.Now().In(a.location).Format(util.TimeLayout))
}
if a.NotifyUrl != util.NULL {
pubBody.Set("notify_url", a.NotifyUrl)
}
if a.AppAuthToken != util.NULL {
pubBody.Set("app_auth_token", a.AppAuthToken)
}
if aat != util.NULL {
pubBody.Set("app_auth_token", aat)
}
if method == "alipay.user.info.share" {
pubBody.Set("auth_token", authToken[0])
}
if bodyStr != util.NULL {
pubBody.Set("biz_content", bodyStr)
}
sign, err := GetRsaSign(pubBody, pubBody.GetString("sign_type"), a.privateKey)
if err != nil {
return nil, fmt.Errorf("GetRsaSign Error: %v", err)
}
pubBody.Set("sign", sign)
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())
}
param := pubBody.EncodeURLParams()
switch method {
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze":
return []byte(param), nil
case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify":
if !a.IsProd {
return []byte(sandboxBaseUrl + "?" + param), nil
}
return []byte(baseUrl + "?" + param), nil
default:
httpClient := xhttp.NewClient()
url = baseUrlUtf8
if !a.IsProd {
url = sandboxBaseUrlUtf8
}
res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
}
return bs, nil
}
}
// 公共参数检查
func (a *Client) checkPublicParam(bm gopay.BodyMap) {
bm.Set("format", "JSON").
Set("charset", a.Charset).
Set("sign_type", a.SignType).
Set("version", "1.0").
Set("timestamp", time.Now().Format(util.TimeLayout))
if bm.GetString("app_id") == "" && a.AppId != util.NULL {
bm.Set("app_id", a.AppId)
}
if bm.GetString("app_cert_sn") == "" && a.AppCertSN != util.NULL {
bm.Set("app_cert_sn", a.AppCertSN)
}
if bm.GetString("alipay_root_cert_sn") == "" && a.AliPayRootCertSN != util.NULL {
bm.Set("alipay_root_cert_sn", a.AliPayRootCertSN)
}
if bm.GetString("return_url") == "" && a.ReturnUrl != util.NULL {
bm.Set("return_url", a.ReturnUrl)
}
if a.location != nil {
bm.Set("timestamp", time.Now().In(a.location).Format(util.TimeLayout))
}
if bm.GetString("notify_url") == "" && a.NotifyUrl != util.NULL {
bm.Set("notify_url", a.NotifyUrl)
}
if bm.GetString("app_auth_token") == "" && a.AppAuthToken != util.NULL {
bm.Set("app_auth_token", a.AppAuthToken)
}
}