Skip to content

Commit

Permalink
更新微信支付(v3)证书加载&日志记录
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed May 12, 2024
1 parent 2c964b5 commit cc48adf
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 48 deletions.
11 changes: 10 additions & 1 deletion alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)

body, err := action.Encode(c)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
log.SetReqBody(body)
Expand All @@ -55,6 +56,7 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)
curl.WithHeader(curl.HeaderContentType, curl.ContentForm),
)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
defer resp.Body.Close()
Expand All @@ -68,12 +70,14 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
log.SetRespBody(string(b))

ret, err := c.verifyResp(action.RespKey(), b)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}

Expand All @@ -82,13 +86,13 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption)
if code := ret.Get("code").String(); code != CodeOK {
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()))
}

return ret, nil
}

// 非JSON串,需解密
data, err := c.Decrypt(ret.String())
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}

Expand All @@ -106,13 +110,15 @@ func (c *Client) Upload(ctx context.Context, method string, form curl.UploadForm

query, err := action.Encode(c)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}

log.Set("query", query)

resp, err := c.httpCli.Upload(ctx, c.gateway+"?"+query, form, curl.WithHeader(curl.HeaderAccept, "application/json"))
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
defer resp.Body.Close()
Expand All @@ -126,12 +132,14 @@ func (c *Client) Upload(ctx context.Context, method string, form curl.UploadForm

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
log.SetRespBody(string(b))

ret, err := c.verifyResp(action.RespKey(), b)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}

Expand All @@ -147,6 +155,7 @@ func (c *Client) Upload(ctx context.Context, method string, form curl.UploadForm
// 非JSON串,需解密
data, err := c.Decrypt(ret.String())
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}

Expand Down
11 changes: 11 additions & 0 deletions alipay/client_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values
if params != nil {
body, err = json.Marshal(params)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
log.SetReqBody(string(body))

if len(header.Get(HeaderEncryptType)) != 0 {
encryptData, err := c.Encrypt(string(body))
if err != nil {
log.Set("error", err.Error())
return nil, err
}

Expand All @@ -85,6 +87,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values

authStr, err := c.Authorization(method, path, query, body, header)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
header.Set(curl.HeaderAuthorization, authStr)
Expand All @@ -93,6 +96,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values

resp, err := c.httpCli.Do(ctx, method, reqURL, body, lib.HeaderToHttpOption(header)...)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
defer resp.Body.Close()
Expand All @@ -102,12 +106,14 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
log.SetRespBody(string(b))

// 签名校验
if err = c.Verify(resp.Header, b); err != nil {
log.Set("error", err.Error())
return nil, err
}

Expand All @@ -120,6 +126,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values
if resp.StatusCode < 400 && len(b) != 0 && !bytes.HasPrefix(b, []byte("{")) {
data, err := c.Decrypt(string(b))
if err != nil {
log.Set("error", err.Error())
return nil, err
}

Expand Down Expand Up @@ -188,6 +195,7 @@ func (c *ClientV3) Upload(ctx context.Context, path string, form curl.UploadForm

authStr, err := c.Authorization(http.MethodPost, path, nil, []byte(form.Field("data")), reqHeader)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
reqHeader.Set(curl.HeaderAuthorization, authStr)
Expand All @@ -196,6 +204,7 @@ func (c *ClientV3) Upload(ctx context.Context, path string, form curl.UploadForm

resp, err := c.httpCli.Upload(ctx, reqURL, form, lib.HeaderToHttpOption(reqHeader)...)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
defer resp.Body.Close()
Expand All @@ -205,12 +214,14 @@ func (c *ClientV3) Upload(ctx context.Context, path string, form curl.UploadForm

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
log.SetRespBody(string(b))

// 签名校验
if err = c.Verify(resp.Header, b); err != nil {
log.Set("error", err.Error())
return nil, err
}

Expand Down
3 changes: 3 additions & 0 deletions antchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ func (c *client) do(ctx context.Context, reqURL string, params lib.X) (string, e

body, err := json.Marshal(params)
if err != nil {
log.Set("error", err.Error())
return "", err
}
log.SetReqBody(string(body))

resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, body, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON))
if err != nil {
log.Set("error", err.Error())
return "", err
}
defer resp.Body.Close()
Expand All @@ -164,6 +166,7 @@ func (c *client) do(ctx context.Context, reqURL string, params lib.X) (string, e

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return "", err
}
log.SetRespBody(string(b))
Expand Down
8 changes: 8 additions & 0 deletions esign/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values,
if params != nil {
body, err = json.Marshal(params)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
log.SetReqBody(string(body))
Expand All @@ -93,6 +94,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values,

resp, err := c.httpCli.Do(ctx, method, reqURL, body, lib.HeaderToHttpOption(header)...)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
defer resp.Body.Close()
Expand All @@ -106,6 +108,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values,

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return lib.Fail(err)
}
log.SetRespBody(string(b))
Expand All @@ -123,6 +126,7 @@ func (c *Client) doStream(ctx context.Context, uploadURL string, reader io.ReadS

h := md5.New()
if _, err := io.Copy(h, reader); err != nil {
log.Set("error", err.Error())
return err
}

Expand All @@ -135,16 +139,19 @@ func (c *Client) doStream(ctx context.Context, uploadURL string, reader io.ReadS

// 文件指针移动到头部
if _, err := reader.Seek(0, 0); err != nil {
log.Set("error", err.Error())
return err
}

buf := bytes.NewBuffer(make([]byte, 0, 20<<10)) // 20kb
if _, err := io.Copy(buf, reader); err != nil {
log.Set("error", err.Error())
return err
}

resp, err := c.httpCli.Do(ctx, http.MethodPut, uploadURL, buf.Bytes(), lib.HeaderToHttpOption(header)...)
if err != nil {
log.Set("error", err.Error())
return err
}
defer resp.Body.Close()
Expand All @@ -157,6 +164,7 @@ func (c *Client) doStream(ctx context.Context, uploadURL string, reader io.ReadS

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return err
}
log.SetRespBody(string(b))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/google/uuid v1.6.0
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
golang.org/x/crypto v0.23.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
5 changes: 5 additions & 0 deletions sandpay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ func (c *Client) Do(ctx context.Context, reqURL string, form *Form) (*Form, erro

body, err := form.URLEncode(c.mchID, c.prvKey)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
log.SetReqBody(body)

resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(body), curl.WithHeader(curl.HeaderContentType, curl.ContentForm))
if err != nil {
log.Set("error", err.Error())
return nil, err
}
defer resp.Body.Close()
Expand All @@ -57,16 +59,19 @@ func (c *Client) Do(ctx context.Context, reqURL string, form *Form) (*Form, erro

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
log.SetRespBody(string(b))

query, err := url.QueryUnescape(string(b))
if err != nil {
log.Set("error", err.Error())
return nil, err
}
v, err := url.ParseQuery(query)
if err != nil {
log.Set("error", err.Error())
return nil, err
}
return c.Verify(v)
Expand Down
2 changes: 2 additions & 0 deletions wechat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
- 公众号
- 小程序
- 企业微信

> 注意:若使用支付(v3),记的加载平台证书!!!
Loading

0 comments on commit cc48adf

Please sign in to comment.