Skip to content

Commit 23ed6c4

Browse files
committed
v1.5.103
1 parent 1a66685 commit 23ed6c4

22 files changed

+252
-168
lines changed

alipay/client.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Client struct {
3333
aliPayPublicKey *rsa.PublicKey // 支付宝证书公钥内容 alipayPublicCert.crt
3434
autoSign bool
3535
DebugSwitch gopay.DebugSwitch
36+
logger xlog.XLogger
3637
location *time.Location
3738
hc *xhttp.Client
3839
}
@@ -51,13 +52,16 @@ func NewClient(appid, privateKey string, isProd bool) (client *Client, err error
5152
if err != nil {
5253
return nil, err
5354
}
55+
logger := xlog.NewLogger()
56+
logger.SetLevel(xlog.DebugLevel)
5457
client = &Client{
5558
AppId: appid,
5659
Charset: UTF8,
5760
SignType: RSA2,
5861
IsProd: isProd,
5962
privateKey: priKey,
6063
DebugSwitch: gopay.DebugOff,
64+
logger: logger,
6165
hc: xhttp.NewClient(),
6266
}
6367
return client, nil
@@ -69,7 +73,7 @@ func NewClient(appid, privateKey string, isProd bool) (client *Client, err error
6973
func (a *Client) AutoVerifySign(alipayPublicKeyContent []byte) {
7074
pubKey, err := xpem.DecodePublicKey(alipayPublicKeyContent)
7175
if err != nil {
72-
xlog.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
76+
a.logger.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
7377
}
7478
if pubKey != nil {
7579
a.aliPayPublicKey = pubKey
@@ -84,6 +88,19 @@ func (a *Client) SetBodySize(sizeMB int) {
8488
}
8589
}
8690

91+
// SetHttpClient 设置自定义的xhttp.Client
92+
func (a *Client) SetHttpClient(client *xhttp.Client) {
93+
if client != nil {
94+
a.hc = client
95+
}
96+
}
97+
98+
func (a *Client) SetLogger(logger xlog.XLogger) {
99+
if logger != nil {
100+
a.logger = logger
101+
}
102+
}
103+
87104
// SetAESKey 设置 biz_content 的AES加密key,设置此参数默认开启 biz_content 参数加密
88105
// 注意:目前不可用,设置后会报错
89106
func (a *Client) SetAESKey(aesKey string) {
@@ -128,7 +145,7 @@ func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {
128145
}
129146

130147
if a.DebugSwitch == gopay.DebugOn {
131-
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
148+
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
132149
}
133150
return bm.EncodeURLParams(), nil
134151
}
@@ -192,8 +209,8 @@ func (a *Client) pubParamsHandle(bm gopay.BodyMap, method, bizContent string, au
192209
return "", fmt.Errorf("EncryptBizContent Error: %w", err)
193210
}
194211
if a.DebugSwitch == gopay.DebugOn {
195-
xlog.Debugf("Alipay_Origin_BizContent: %s", bizContent)
196-
xlog.Debugf("Alipay_Encrypt_BizContent: %s", encryptBizContent)
212+
a.logger.Debugf("Alipay_Origin_BizContent: %s", bizContent)
213+
a.logger.Debugf("Alipay_Encrypt_BizContent: %s", encryptBizContent)
197214
}
198215
pubBody.Set("biz_content", encryptBizContent)
199216
}
@@ -205,7 +222,7 @@ func (a *Client) pubParamsHandle(bm gopay.BodyMap, method, bizContent string, au
205222
}
206223
pubBody.Set("sign", sign)
207224
if a.DebugSwitch == gopay.DebugOn {
208-
xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())
225+
a.logger.Debugf("Alipay_Request: %s", pubBody.JsonBody())
209226
}
210227
param = pubBody.EncodeURLParams()
211228
return
@@ -249,8 +266,3 @@ func (a *Client) encryptBizContent(originData string) (string, error) {
249266
}
250267
return base64.StdEncoding.EncodeToString(encryptData), nil
251268
}
252-
253-
// SetHttpClient 设置自定义的xhttp.Client
254-
func (a *Client) SetHttpClient(client *xhttp.Client) {
255-
a.hc = client
256-
}

alipay/client_request.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/go-pay/gopay"
1010
"github.com/go-pay/gopay/pkg/xhttp"
11-
"github.com/go-pay/xlog"
1211
"github.com/go-pay/xtime"
1312
)
1413

@@ -71,7 +70,7 @@ func (a *Client) PostFileAliPayAPISelfV2(ctx context.Context, bm gopay.BodyMap,
7170
bm.Set(k, v)
7271
}
7372
if a.DebugSwitch == gopay.DebugOn {
74-
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
73+
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
7574
}
7675
if a.IsProd {
7776
url = baseUrlUtf8
@@ -84,7 +83,7 @@ func (a *Client) PostFileAliPayAPISelfV2(ctx context.Context, bm gopay.BodyMap,
8483
return nil
8584
}
8685
if a.DebugSwitch == gopay.DebugOn {
87-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
86+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
8887
}
8988
if res.StatusCode != 200 {
9089
return fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
@@ -112,7 +111,7 @@ func (a *Client) doAliPaySelf(ctx context.Context, bm gopay.BodyMap, method stri
112111
bm.Set("sign", sign)
113112
}
114113
if a.DebugSwitch == gopay.DebugOn {
115-
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
114+
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
116115
}
117116
if a.IsProd {
118117
url = baseUrlUtf8
@@ -124,7 +123,7 @@ func (a *Client) doAliPaySelf(ctx context.Context, bm gopay.BodyMap, method stri
124123
return nil, err
125124
}
126125
if a.DebugSwitch == gopay.DebugOn {
127-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
126+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
128127
}
129128
if res.StatusCode != 200 {
130129
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
@@ -179,7 +178,7 @@ func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string,
179178
return nil, err
180179
}
181180
if a.DebugSwitch == gopay.DebugOn {
182-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
181+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
183182
}
184183
if res.StatusCode != 200 {
185184
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
@@ -235,7 +234,7 @@ func (a *Client) DoAliPay(ctx context.Context, bm gopay.BodyMap, method string,
235234
return nil, err
236235
}
237236
if a.DebugSwitch == gopay.DebugOn {
238-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
237+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
239238
}
240239
if res.StatusCode != 200 {
241240
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
@@ -338,7 +337,7 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
338337
}
339338
pubBody.Set("sign", sign)
340339
if a.DebugSwitch == gopay.DebugOn {
341-
xlog.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
340+
a.logger.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
342341
}
343342
url := baseUrlUtf8 + "&" + pubBody.EncodeURLParams()
344343

@@ -348,7 +347,7 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
348347
return nil, err
349348
}
350349
if a.DebugSwitch == gopay.DebugOn {
351-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
350+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
352351
}
353352
if res.StatusCode != 200 {
354353
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)

alipay/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
)
2323

2424
func TestMain(m *testing.M) {
25-
xlog.Level = xlog.DebugLevel
25+
xlog.SetLevel(xlog.DebugLevel)
2626
// 初始化支付宝客户端
2727
// appid:应用ID
2828
// privateKey:应用私钥,支持PKCS1和PKCS8

alipay/customs.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/go-pay/gopay"
99
"github.com/go-pay/gopay/pkg/xhttp"
10-
"github.com/go-pay/xlog"
1110
)
1211

1312
// alipay.trade.customs.declare(统一收单报关接口)
@@ -76,15 +75,15 @@ func (a *Client) doAliPayCustoms(ctx context.Context, bm gopay.BodyMap, service
7675

7776
bm.Set("sign_type", RSA).Set("sign", sign)
7877
if a.DebugSwitch == gopay.DebugOn {
79-
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
78+
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
8079
}
8180
// request
8281
res, bs, err := a.hc.Req(xhttp.TypeFormData).Post("https://mapi.alipay.com/gateway.do").SendString(bm.EncodeURLParams()).EndBytes(ctx)
8382
if err != nil {
8483
return nil, err
8584
}
8685
if a.DebugSwitch == gopay.DebugOn {
87-
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
86+
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
8887
}
8988
if res.StatusCode != 200 {
9089
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)

alipay/sign.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/go-pay/crypto/xpem"
2323
"github.com/go-pay/crypto/xrsa"
2424
"github.com/go-pay/gopay"
25-
"github.com/go-pay/xlog"
2625
)
2726

2827
// 允许进行 sn 提取的证书签名算法
@@ -192,7 +191,7 @@ func (a *Client) getRsaSign(bm gopay.BodyMap, signType string) (sign string, err
192191
}
193192
signParams := bm.EncodeAliPaySignParams()
194193
if a.DebugSwitch == gopay.DebugOn {
195-
xlog.Debugf("Alipay_Request_SignStr: %s", signParams)
194+
a.logger.Debugf("Alipay_Request_SignStr: %s", signParams)
196195
}
197196
if _, err = h.Write([]byte(signParams)); err != nil {
198197
return
@@ -283,7 +282,7 @@ func VerifySyncSignWithCert(alipayPublicKeyCert any, signData, sign string) (ok
283282
func (a *Client) autoVerifySignByCert(sign, signData string, signDataErr error) (err error) {
284283
if a.autoSign && a.aliPayPublicKey != nil {
285284
if a.DebugSwitch == gopay.DebugOn {
286-
xlog.Debugf("Alipay_SyncSignData: %s, Sign=[%s]", signData, sign)
285+
a.logger.Debugf("Alipay_SyncSignData: %s, Sign=[%s]", signData, sign)
287286
}
288287
// 只有证书验签时,才可能出现此error
289288
if signDataErr != nil {

apple/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
)
2020

2121
func TestMain(m *testing.M) {
22-
xlog.Level = xlog.DebugLevel
22+
xlog.SetLevel(xlog.DebugLevel)
2323
// 初始化客户端
2424
// iss:issuer ID
2525
// bid:bundle ID

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/go-pay/gopay
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/go-pay/crypto v0.0.1
77
github.com/go-pay/errgroup v0.0.2
88
github.com/go-pay/util v0.0.2
9-
github.com/go-pay/xlog v0.0.2
9+
github.com/go-pay/xlog v0.0.3
1010
github.com/go-pay/xtime v0.0.2
1111
golang.org/x/crypto v0.24.0
1212
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/go-pay/errgroup v0.0.2 h1:5mZMdm0TDClDm2S3G0/sm0f8AuQRtz0dOrTHDR9R8Cc
44
github.com/go-pay/errgroup v0.0.2/go.mod h1:0+4b8mvFMS71MIzsaC+gVvB4x37I93lRb2dqrwuU8x8=
55
github.com/go-pay/util v0.0.2 h1:goJ4f6kNY5zzdtg1Cj8oWC+Cw7bfg/qq2rJangMAb9U=
66
github.com/go-pay/util v0.0.2/go.mod h1:qM8VbyF1n7YAPZBSJONSPMPsPedhUTktewUAdf1AjPg=
7-
github.com/go-pay/xlog v0.0.2 h1:kUg5X8/5VZAPDg1J5eGjA3MG0/H5kK6Ew0dW/Bycsws=
8-
github.com/go-pay/xlog v0.0.2/go.mod h1:DbjMADPK4+Sjxj28ekK9goqn4zmyY4hql/zRiab+S9E=
7+
github.com/go-pay/xlog v0.0.3 h1:avyMhCL/JgBHreoGx/am/kHxfs1udDOAeVqbmzP/Yes=
8+
github.com/go-pay/xlog v0.0.3/go.mod h1:mH47xbobrdsSHWsmFtSF5agWbMHFP+tK0ZbVCk5OAEw=
99
github.com/go-pay/xtime v0.0.2 h1:7YR4/iuELsEHpJ6LUO0SVK80hQxDO9MLCfuVYIiTCRM=
1010
github.com/go-pay/xtime v0.0.2/go.mod h1:W1yRbJaSt4CSBcdAtLBQ8xajiN/Pl5hquGczUcUE9xE=
1111
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=

lakala/client.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Client struct {
2424
credentialCode string // credential_code:系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露
2525
IsProd bool // 是否生产环境
2626
DebugSwitch gopay.DebugSwitch // 调试开关,是否打印日志
27+
logger xlog.XLogger
2728
hc *xhttp.Client
2829
sha256Hash hash.Hash
2930
mu sync.Mutex
@@ -37,12 +38,15 @@ func NewClient(partnerCode, credentialCode string, isProd bool) (client *Client,
3738
if partnerCode == gopay.NULL || credentialCode == gopay.NULL {
3839
return nil, gopay.MissLakalaInitParamErr
3940
}
41+
logger := xlog.NewLogger()
42+
logger.SetLevel(xlog.DebugLevel)
4043
client = &Client{
4144
ctx: context.Background(),
4245
PartnerCode: partnerCode,
4346
credentialCode: credentialCode,
4447
IsProd: isProd,
4548
DebugSwitch: gopay.DebugOff,
49+
logger: logger,
4650
hc: xhttp.NewClient(),
4751
sha256Hash: sha256.New(),
4852
}
@@ -56,6 +60,19 @@ func (c *Client) SetBodySize(sizeMB int) {
5660
}
5761
}
5862

63+
// SetHttpClient 设置自定义的xhttp.Client
64+
func (c *Client) SetHttpClient(client *xhttp.Client) {
65+
if client != nil {
66+
c.hc = client
67+
}
68+
}
69+
70+
func (c *Client) SetLogger(logger xlog.XLogger) {
71+
if logger != nil {
72+
c.logger = logger
73+
}
74+
}
75+
5976
// 公共参数处理 Query Params
6077
func (c *Client) pubParamsHandle() (param string, err error) {
6178
bm := make(gopay.BodyMap)
@@ -115,9 +132,9 @@ func (c *Client) doPut(ctx context.Context, path string, bm gopay.BodyMap) (bs [
115132
req.Header.Add("Accept", "application/json")
116133
uri := url + "?" + param
117134
if c.DebugSwitch == gopay.DebugOn {
118-
xlog.Debugf("Lakala_Url: %s", uri)
119-
xlog.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
120-
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
135+
c.logger.Debugf("Lakala_Url: %s", uri)
136+
c.logger.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
137+
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
121138
}
122139
res, bs, err := req.Put(uri).SendBodyMap(bm).EndBytes(ctx)
123140
if err != nil {
@@ -140,9 +157,9 @@ func (c *Client) doPost(ctx context.Context, path string, bm gopay.BodyMap) (bs
140157
req.Header.Add("Accept", "application/json")
141158
uri := url + "?" + param
142159
if c.DebugSwitch == gopay.DebugOn {
143-
xlog.Debugf("Lakala_Url: %s", uri)
144-
xlog.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
145-
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
160+
c.logger.Debugf("Lakala_Url: %s", uri)
161+
c.logger.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
162+
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
146163
}
147164
res, bs, err := req.Post(uri).SendBodyMap(bm).EndBytes(ctx)
148165
if err != nil {
@@ -168,8 +185,8 @@ func (c *Client) doGet(ctx context.Context, path, queryParams string) (bs []byte
168185
req.Header.Add("Accept", "application/json")
169186
uri := url + "?" + param
170187
if c.DebugSwitch == gopay.DebugOn {
171-
xlog.Debugf("Lakala_Url: %s", uri)
172-
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
188+
c.logger.Debugf("Lakala_Url: %s", uri)
189+
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
173190
}
174191
res, bs, err := req.Get(uri).EndBytes(ctx)
175192
if err != nil {

paypal/access_token.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import (
1111
"github.com/go-pay/gopay"
1212
"github.com/go-pay/gopay/pkg/xhttp"
1313
"github.com/go-pay/util/retry"
14-
"github.com/go-pay/xlog"
1514
)
1615

1716
func (c *Client) goAuthRefreshToken() {
1817
defer func() {
1918
if r := recover(); r != nil {
2019
buf := make([]byte, 64<<10)
2120
buf = buf[:runtime.Stack(buf, false)]
22-
xlog.Errorf("paypal_goAuthRefreshToken: panic recovered: %s\n%s", r, buf)
21+
c.logger.Errorf("paypal_goAuthRefreshToken: panic recovered: %s\n%s", r, buf)
2322
}
2423
}()
2524
for {
@@ -32,7 +31,7 @@ func (c *Client) goAuthRefreshToken() {
3231
return nil
3332
}, 3, time.Second)
3433
if err != nil {
35-
xlog.Errorf("PayPal GetAccessToken Error: %s", err.Error())
34+
c.logger.Errorf("PayPal GetAccessToken Error: %s", err.Error())
3635
}
3736
}
3837
}
@@ -59,17 +58,17 @@ func (c *Client) GetAccessToken() (token *AccessToken, err error) {
5958
bm := make(gopay.BodyMap)
6059
bm.Set("grant_type", "client_credentials")
6160
if c.DebugSwitch == gopay.DebugOn {
62-
xlog.Debugf("PayPal_Url: %s", url)
63-
xlog.Debugf("PayPal_Req_Body: %s", bm.JsonBody())
64-
xlog.Debugf("PayPal_Req_Headers: %#v", req.Header)
61+
c.logger.Debugf("PayPal_Url: %s", url)
62+
c.logger.Debugf("PayPal_Req_Body: %s", bm.JsonBody())
63+
c.logger.Debugf("PayPal_Req_Headers: %#v", req.Header)
6564
}
6665
res, bs, err := req.Post(url).SendBodyMap(bm).EndBytes(c.ctx)
6766
if err != nil {
6867
return nil, err
6968
}
7069
if c.DebugSwitch == gopay.DebugOn {
71-
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
72-
xlog.Debugf("PayPal_Rsp_Headers: %#v", res.Header)
70+
c.logger.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
71+
c.logger.Debugf("PayPal_Rsp_Headers: %#v", res.Header)
7372
}
7473
if res.StatusCode != http.StatusOK {
7574
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)

0 commit comments

Comments
 (0)