Skip to content

Commit 64029be

Browse files
author
盛辉
committedFeb 11, 2025
更新internal
1 parent cceca96 commit 64029be

39 files changed

+366
-356
lines changed
 

‎alipay/action.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/shenghui0779/sdk-go/lib"
13-
"github.com/shenghui0779/sdk-go/lib/value"
12+
"github.com/yiigo/sdk-go/internal"
13+
"github.com/yiigo/sdk-go/internal/value"
1414
)
1515

1616
type Action struct {
1717
method string
18-
params value.V
19-
bizData lib.X
18+
params V
19+
bizData internal.X
2020
encrypt bool
2121
}
2222

@@ -31,7 +31,7 @@ func (a *Action) Encode(c *Client) (string, error) {
3131
return "", errors.New("private key is nil (forgotten configure?)")
3232
}
3333

34-
v := make(value.V)
34+
v := make(V)
3535

3636
v.Set("app_id", c.appid)
3737
v.Set("method", a.method)
@@ -135,7 +135,7 @@ func WithKVParam(k, v string) ActionOption {
135135
}
136136

137137
// WithBizContent 设置「biz_content」参数
138-
func WithBizContent(data lib.X) ActionOption {
138+
func WithBizContent(data internal.X) ActionOption {
139139
return func(a *Action) {
140140
a.bizData = data
141141
}
@@ -153,7 +153,7 @@ func WithEncrypt() ActionOption {
153153
func NewAction(method string, options ...ActionOption) *Action {
154154
action := &Action{
155155
method: method,
156-
params: make(value.V),
156+
params: make(V),
157157
}
158158

159159
for _, f := range options {

‎alipay/client.go

+44-44
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/go-resty/resty/v2"
1515
"github.com/tidwall/gjson"
1616

17-
"github.com/shenghui0779/sdk-go/lib"
18-
"github.com/shenghui0779/sdk-go/lib/value"
19-
"github.com/shenghui0779/sdk-go/lib/xcrypto"
17+
"github.com/yiigo/sdk-go/internal"
18+
"github.com/yiigo/sdk-go/internal/value"
19+
"github.com/yiigo/sdk-go/internal/xcrypto"
2020
)
2121

2222
// Client 支付宝客户端
@@ -39,46 +39,46 @@ func (c *Client) AppID() string {
3939
func (c *Client) Do(ctx context.Context, method string, options ...ActionOption) (gjson.Result, error) {
4040
reqURL := c.gateway + "?charset=utf-8"
4141

42-
log := lib.NewReqLog(http.MethodPost, reqURL)
42+
log := internal.NewReqLog(http.MethodPost, reqURL)
4343
defer log.Do(ctx, c.logger)
4444

4545
action := NewAction(method, options...)
4646

4747
body, err := action.Encode(c)
4848
if err != nil {
4949
log.SetError(err)
50-
return lib.Fail(err)
50+
return internal.Fail(err)
5151
}
5252
log.SetReqBody(body)
5353

5454
resp, err := c.client.R().
5555
SetContext(ctx).
56-
SetHeader(lib.HeaderAccept, lib.ContentJSON).
57-
SetHeader(lib.HeaderContentType, lib.ContentForm).
56+
SetHeader(internal.HeaderAccept, internal.ContentJSON).
57+
SetHeader(internal.HeaderContentType, internal.ContentForm).
5858
SetBody(body).
5959
Post(reqURL)
6060
if err != nil {
6161
log.SetError(err)
62-
return lib.Fail(err)
62+
return internal.Fail(err)
6363
}
6464
log.SetRespHeader(resp.Header())
6565
log.SetStatusCode(resp.StatusCode())
6666
log.SetRespBody(string(resp.Body()))
6767
if !resp.IsSuccess() {
68-
return lib.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
68+
return internal.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
6969
}
7070

7171
// 签名校验
7272
ret, err := c.verifyResp(action.RespKey(), resp.Body())
7373
if err != nil {
7474
log.SetError(err)
75-
return lib.Fail(err)
75+
return internal.Fail(err)
7676
}
7777

7878
// JSON串,无需解密
7979
if strings.HasPrefix(ret.String(), "{") {
8080
if code := ret.Get("code").String(); code != CodeOK {
81-
return lib.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
81+
return internal.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
8282
}
8383
return ret, nil
8484
}
@@ -87,7 +87,7 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)
8787
data, err := c.Decrypt(ret.String())
8888
if err != nil {
8989
log.SetError(err)
90-
return lib.Fail(err)
90+
return internal.Fail(err)
9191
}
9292
log.Set("decrypt", string(data))
9393

@@ -98,46 +98,46 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)
9898
//
9999
// [参考](https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload)
100100
func (c *Client) Upload(ctx context.Context, method string, fieldName, filePath string, formData map[string]string, options ...ActionOption) (gjson.Result, error) {
101-
log := lib.NewReqLog(http.MethodPost, c.gateway)
101+
log := internal.NewReqLog(http.MethodPost, c.gateway)
102102
defer log.Do(ctx, c.logger)
103103

104104
action := NewAction(method, options...)
105105

106106
query, err := action.Encode(c)
107107
if err != nil {
108108
log.SetError(err)
109-
return lib.Fail(err)
109+
return internal.Fail(err)
110110
}
111111
log.Set("query", query)
112112

113113
resp, err := c.client.R().
114114
SetContext(ctx).
115-
SetHeader(lib.HeaderAccept, lib.ContentJSON).
115+
SetHeader(internal.HeaderAccept, internal.ContentJSON).
116116
SetFile(fieldName, filePath).
117117
SetMultipartFormData(formData).
118118
Post(c.gateway + "?" + query)
119119
if err != nil {
120120
log.SetError(err)
121-
return lib.Fail(err)
121+
return internal.Fail(err)
122122
}
123123
log.SetRespHeader(resp.Header())
124124
log.SetStatusCode(resp.StatusCode())
125125
log.SetRespBody(string(resp.Body()))
126126
if !resp.IsSuccess() {
127-
return lib.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
127+
return internal.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
128128
}
129129

130130
// 签名校验
131131
ret, err := c.verifyResp(action.RespKey(), resp.Body())
132132
if err != nil {
133133
log.SetError(err)
134-
return lib.Fail(err)
134+
return internal.Fail(err)
135135
}
136136

137137
// JSON串,无需解密
138138
if strings.HasPrefix(ret.String(), "{") {
139139
if code := ret.Get("code").String(); code != CodeOK {
140-
return lib.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
140+
return internal.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
141141
}
142142
return ret, nil
143143
}
@@ -146,7 +146,7 @@ func (c *Client) Upload(ctx context.Context, method string, fieldName, filePath
146146
data, err := c.Decrypt(ret.String())
147147
if err != nil {
148148
log.SetError(err)
149-
return lib.Fail(err)
149+
return internal.Fail(err)
150150
}
151151
log.Set("decrypt", string(data))
152152

@@ -157,46 +157,46 @@ func (c *Client) Upload(ctx context.Context, method string, fieldName, filePath
157157
//
158158
// [参考](https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload)
159159
func (c *Client) UploadWithReader(ctx context.Context, method string, fieldName, fileName string, reader io.Reader, formData map[string]string, options ...ActionOption) (gjson.Result, error) {
160-
log := lib.NewReqLog(http.MethodPost, c.gateway)
160+
log := internal.NewReqLog(http.MethodPost, c.gateway)
161161
defer log.Do(ctx, c.logger)
162162

163163
action := NewAction(method, options...)
164164

165165
query, err := action.Encode(c)
166166
if err != nil {
167167
log.SetError(err)
168-
return lib.Fail(err)
168+
return internal.Fail(err)
169169
}
170170
log.Set("query", query)
171171

172172
resp, err := c.client.R().
173173
SetContext(ctx).
174-
SetHeader(lib.HeaderAccept, lib.ContentJSON).
174+
SetHeader(internal.HeaderAccept, internal.ContentJSON).
175175
SetMultipartField(fieldName, fileName, "", reader).
176176
SetMultipartFormData(formData).
177177
Post(c.gateway + "?" + query)
178178
if err != nil {
179179
log.SetError(err)
180-
return lib.Fail(err)
180+
return internal.Fail(err)
181181
}
182182
log.SetRespHeader(resp.Header())
183183
log.SetStatusCode(resp.StatusCode())
184184
log.SetRespBody(string(resp.Body()))
185185
if !resp.IsSuccess() {
186-
return lib.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
186+
return internal.Fail(fmt.Errorf("HTTP Request Error, StatusCode = %d", resp.StatusCode()))
187187
}
188188

189189
// 签名校验
190190
ret, err := c.verifyResp(action.RespKey(), resp.Body())
191191
if err != nil {
192192
log.SetError(err)
193-
return lib.Fail(err)
193+
return internal.Fail(err)
194194
}
195195

196196
// JSON串,无需解密
197197
if strings.HasPrefix(ret.String(), "{") {
198198
if code := ret.Get("code").String(); code != CodeOK {
199-
return lib.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
199+
return internal.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)", code, ret.Get("msg").String(), ret.Get("sub_code").String(), ret.Get("sub_msg").String()))
200200
}
201201
return ret, nil
202202
}
@@ -205,7 +205,7 @@ func (c *Client) UploadWithReader(ctx context.Context, method string, fieldName,
205205
data, err := c.Decrypt(ret.String())
206206
if err != nil {
207207
log.SetError(err)
208-
return lib.Fail(err)
208+
return internal.Fail(err)
209209
}
210210
log.Set("decrypt", string(data))
211211

@@ -214,27 +214,27 @@ func (c *Client) UploadWithReader(ctx context.Context, method string, fieldName,
214214

215215
func (c *Client) verifyResp(key string, body []byte) (gjson.Result, error) {
216216
if c.pubKey == nil {
217-
return lib.Fail(errors.New("public key is nil (forgotten configure?)"))
217+
return internal.Fail(errors.New("public key is nil (forgotten configure?)"))
218218
}
219219

220220
ret := gjson.ParseBytes(body)
221221

222222
signByte, err := base64.StdEncoding.DecodeString(ret.Get("sign").String())
223223
if err != nil {
224-
return lib.Fail(err)
224+
return internal.Fail(err)
225225
}
226226

227-
hash := crypto.SHA256
227+
xhash := crypto.SHA256
228228
if ret.Get("sign_type").String() == "RSA" {
229-
hash = crypto.SHA1
229+
xhash = crypto.SHA1
230230
}
231231

232232
if errResp := ret.Get("error_response"); errResp.Exists() {
233-
if err = c.pubKey.Verify(hash, []byte(errResp.Raw), signByte); err != nil {
234-
return lib.Fail(err)
233+
if err = c.pubKey.Verify(xhash, []byte(errResp.Raw), signByte); err != nil {
234+
return internal.Fail(err)
235235
}
236236

237-
return lib.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)",
237+
return internal.Fail(fmt.Errorf("%s | %s (sub_code = %s, sub_msg = %s)",
238238
errResp.Get("code").String(),
239239
errResp.Get("msg").String(),
240240
errResp.Get("sub_code").String(),
@@ -243,8 +243,8 @@ func (c *Client) verifyResp(key string, body []byte) (gjson.Result, error) {
243243
}
244244

245245
resp := ret.Get(key)
246-
if err = c.pubKey.Verify(hash, []byte(resp.Raw), signByte); err != nil {
247-
return lib.Fail(err)
246+
if err = c.pubKey.Verify(xhash, []byte(resp.Raw), signByte); err != nil {
247+
return internal.Fail(err)
248248
}
249249
return resp, nil
250250
}
@@ -302,7 +302,7 @@ func (c *Client) DecodeEncryptData(hash crypto.Hash, data, sign string) ([]byte,
302302
}
303303

304304
// VerifyNotify 验证回调通知表单数据
305-
func (c *Client) VerifyNotify(form url.Values) (value.V, error) {
305+
func (c *Client) VerifyNotify(form url.Values) (V, error) {
306306
if c.pubKey == nil {
307307
return nil, errors.New("public key is nil (forgotten configure?)")
308308
}
@@ -312,7 +312,7 @@ func (c *Client) VerifyNotify(form url.Values) (value.V, error) {
312312
return nil, err
313313
}
314314

315-
v := value.V{}
315+
v := V{}
316316
for key, vals := range form {
317317
if key == "sign_type" || key == "sign" || len(vals) == 0 {
318318
continue
@@ -321,11 +321,11 @@ func (c *Client) VerifyNotify(form url.Values) (value.V, error) {
321321
}
322322
str := v.Encode("=", "&", value.WithEmptyMode(value.EmptyIgnore))
323323

324-
hash := crypto.SHA256
324+
xhash := crypto.SHA256
325325
if form.Get("sign_type") == "RSA" {
326-
hash = crypto.SHA1
326+
xhash = crypto.SHA1
327327
}
328-
if err = c.pubKey.Verify(hash, []byte(str), sign); err != nil {
328+
if err = c.pubKey.Verify(xhash, []byte(str), sign); err != nil {
329329
return nil, err
330330
}
331331
return v, nil
@@ -368,7 +368,7 @@ func NewClient(appid, aesKey string, options ...Option) *Client {
368368
appid: appid,
369369
aesKey: aesKey,
370370
gateway: "https://openapi.alipay.com/gateway.do",
371-
client: lib.NewClient(),
371+
client: internal.NewClient(),
372372
}
373373
for _, f := range options {
374374
f(c)
@@ -382,7 +382,7 @@ func NewSandbox(appid, aesKey string, options ...Option) *Client {
382382
appid: appid,
383383
aesKey: aesKey,
384384
gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
385-
client: lib.NewClient(),
385+
client: internal.NewClient(),
386386
}
387387
for _, f := range options {
388388
f(c)

0 commit comments

Comments
 (0)
Please sign in to comment.