Skip to content

Commit 0b26439

Browse files
committed
udpate
1 parent 7454003 commit 0b26439

35 files changed

+504
-63
lines changed

alipay/ant.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,11 @@ func (a *Client) AntMerchantShopClose(ctx context.Context, bm gopay.BodyMap) (al
137137
// ant.merchant.expand.indirect.image.upload(图片上传)
138138
// bm参数中 image_content 可不传,file为必传参数
139139
// 文档地址:https://opendocs.alipay.com/open/04fgwt
140-
func (a *Client) AntMerchantExpandIndirectImageUpload(ctx context.Context, bm gopay.BodyMap, file *gopay.File) (aliRsp *AntMerchantExpandIndirectImageUploadRsp, err error) {
141-
if file == nil {
142-
return nil, fmt.Errorf("file is nil")
143-
}
144-
err = bm.CheckEmptyError("image_type")
140+
func (a *Client) AntMerchantExpandIndirectImageUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *AntMerchantExpandIndirectImageUploadRsp, err error) {
141+
err = bm.CheckEmptyError("image_type", "image_content")
145142
if err != nil {
146143
return nil, err
147144
}
148-
bm.Set("image_content", file)
149145
var bs []byte
150146
if bs, err = a.FileUploadRequest(ctx, bm, "ant.merchant.expand.indirect.image.upload"); err != nil {
151147
return nil, err

alipay/ant_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ func TestAntMerchantExpandIndirectImageUpload(t *testing.T) {
129129
return
130130
}
131131
f := &gopay.File{
132-
Name: "logo",
132+
Name: "logo.png",
133133
Content: allBs,
134134
}
135135
bm := make(gopay.BodyMap)
136136
bm.Set("image_type", "png")
137-
aliRsp, err := client.AntMerchantExpandIndirectImageUpload(ctx, bm, f)
137+
bm.SetFormFile("image_content", f)
138+
aliRsp, err := client.AntMerchantExpandIndirectImageUpload(ctx, bm)
138139
if err != nil {
139140
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
140141
return

alipay/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/go-pay/crypto/xpem"
1212
"github.com/go-pay/crypto/xrsa"
1313
"github.com/go-pay/gopay"
14-
"github.com/go-pay/xhttp"
14+
"github.com/go-pay/gopay/pkg/xhttp"
1515
"github.com/go-pay/xlog"
1616
"github.com/go-pay/xtime"
1717
)

alipay/request.go alipay/client_request.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/go-pay/gopay"
10-
"github.com/go-pay/xhttp"
10+
"github.com/go-pay/gopay/pkg/xhttp"
1111
"github.com/go-pay/xlog"
1212
"github.com/go-pay/xtime"
1313
)
@@ -321,9 +321,11 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
321321
if aat != gopay.NULL {
322322
pubBody.Set("app_auth_token", aat)
323323
}
324-
// 文件也需要签名
324+
// 文件上传除文件外其他参数也需要签名
325325
for k, v := range bm {
326-
pubBody.Set(k, v)
326+
if _, ok := v.(*gopay.File); !ok {
327+
pubBody.Set(k, v)
328+
}
327329
}
328330
// sign
329331
sign, err := a.getRsaSign(pubBody, pubBody.GetString("sign_type"))
@@ -336,10 +338,9 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
336338
}
337339
pubBody.Set("sign", sign)
338340
if a.DebugSwitch == gopay.DebugOn {
339-
xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())
341+
xlog.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
340342
}
341-
param := pubBody.EncodeURLParams()
342-
url := baseUrlUtf8 + "&" + param
343+
url := baseUrlUtf8 + "&" + pubBody.EncodeURLParams()
343344

344345
res, bs, err := a.hc.Req(xhttp.TypeMultipartFormData).Post(url).
345346
SendMultipartBodyMap(bm).EndBytes(ctx)

alipay/common_api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/go-pay/crypto/xpem"
1818
"github.com/go-pay/crypto/xrsa"
1919
"github.com/go-pay/gopay"
20-
"github.com/go-pay/xhttp"
20+
"github.com/go-pay/gopay/pkg/xhttp"
2121
"github.com/go-pay/xtime"
2222
)
2323

alipay/customs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77

88
"github.com/go-pay/gopay"
9-
"github.com/go-pay/xhttp"
9+
"github.com/go-pay/gopay/pkg/xhttp"
1010
"github.com/go-pay/xlog"
1111
)
1212

alipay/goods_api.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ import (
1010

1111
// alipay.merchant.item.file.upload(商品文件上传接口)
1212
// 文档地址:https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload
13-
func (a *Client) MerchantItemFileUpload(ctx context.Context, file *gopay.File) (aliRsp *MerchantItemFileUploadRsp, err error) {
14-
if file == nil {
15-
return nil, fmt.Errorf("file is nil")
13+
func (a *Client) MerchantItemFileUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *MerchantItemFileUploadRsp, err error) {
14+
err = bm.CheckEmptyError("scene", "file_content")
15+
if err != nil {
16+
return nil, err
1617
}
17-
bm := make(gopay.BodyMap)
18-
bm.Set("scene", "SYNC_ORDER") //素材固定值
19-
bm.Set("file_content", file) //素材固定值
20-
2118
var bs []byte
2219
if bs, err = a.FileUploadRequest(ctx, bm, "alipay.merchant.item.file.upload"); err != nil {
2320
return nil, err

alipay/goods_api_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ func TestMerchantItemFileUpload(t *testing.T) {
2323
return
2424
}
2525
f := &gopay.File{
26-
Name: "logo",
26+
Name: "logo.png",
2727
Content: allBs,
2828
}
29-
aliRsp, err := client.MerchantItemFileUpload(ctx, f)
29+
bm := make(gopay.BodyMap)
30+
bm.Set("scene", "SYNC_ORDER") // 素材固定值
31+
bm.SetFormFile("file_content", f)
32+
aliRsp, err := client.MerchantItemFileUpload(ctx, bm)
3033
if err != nil {
31-
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
34+
xlog.Errorf("client.MerchantItemFileUpload(),error:%+v", err)
3235
return
3336
}
3437
xlog.Debug("aliRsp:", *aliRsp)

alipay/marketing_api_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ func TestMarketingMaterialImageUpload(t *testing.T) {
121121
return
122122
}
123123
f := &gopay.File{
124-
Name: "logo",
124+
Name: "logo.png",
125125
Content: allBs,
126126
}
127127
bm := make(gopay.BodyMap)
128128
bm.Set("file_key", "PROMO_VOUCHER_IMAGE").
129-
Set("merchant_access_mode", "SELF_MODE")
130-
aliRsp, err := client.MarketingMaterialImageUpload(ctx, bm, f)
129+
SetFormFile("file_content", f)
130+
aliRsp, err := client.MarketingMaterialImageUpload(ctx, bm)
131131
if err != nil {
132-
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
132+
xlog.Errorf("client.MarketingMaterialImageUpload(),error:%+v", err)
133133
return
134134
}
135135
xlog.Debug("aliRsp:", *aliRsp)

alipay/marketing_voucher.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ import (
1111
// alipay.marketing.material.image.upload(营销图片资源上传接口)
1212
// bm参数中 file_content 可不传,file为必传参数
1313
// 文档地址:https://opendocs.alipay.com/open/389b24b6_alipay.marketing.material.image.upload
14-
func (a *Client) MarketingMaterialImageUpload(ctx context.Context, bm gopay.BodyMap, file *gopay.File) (aliRsp *MarketingMaterialImageUploadRsp, err error) {
15-
if file == nil {
16-
return nil, fmt.Errorf("file is nil")
17-
}
18-
if err = bm.CheckEmptyError("file_key"); err != nil {
14+
func (a *Client) MarketingMaterialImageUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *MarketingMaterialImageUploadRsp, err error) {
15+
if err = bm.CheckEmptyError("file_key", "file_content"); err != nil {
1916
return nil, err
2017
}
21-
bm.Set("file_content", file)
2218
var bs []byte
2319
if bs, err = a.FileUploadRequest(ctx, bm, "alipay.marketing.material.image.upload"); err != nil {
2420
return nil, err

allinpay/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"github.com/go-pay/crypto/xpem"
1616
"github.com/go-pay/crypto/xrsa"
1717
"github.com/go-pay/gopay"
18+
"github.com/go-pay/gopay/pkg/xhttp"
1819
"github.com/go-pay/util"
19-
"github.com/go-pay/xhttp"
2020
)
2121

2222
type Client struct {

apple/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77

88
"github.com/go-pay/gopay"
9-
"github.com/go-pay/xhttp"
9+
"github.com/go-pay/gopay/pkg/xhttp"
1010
)
1111

1212
// Client AppleClient

apple/verify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package apple
33
import (
44
"context"
55

6-
"github.com/go-pay/xhttp"
6+
"github.com/go-pay/gopay/pkg/xhttp"
77
)
88

99
const (

go.mod

-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ 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/xhttp v0.0.2
109
github.com/go-pay/xlog v0.0.2
1110
github.com/go-pay/xtime v0.0.2
1211
golang.org/x/crypto v0.23.0
1312
)
14-
15-
require github.com/go-pay/bm v0.0.2 // indirect

go.sum

-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
github.com/go-pay/bm v0.0.2 h1:CLa5hNTIm6uv8HSWGWd4moZbuOzQqJo5HcScXBBSY/E=
2-
github.com/go-pay/bm v0.0.2/go.mod h1:nyglxC5YS4+gVaO9TxLMI0I/naFUqwffdt5n6llM8uM=
31
github.com/go-pay/crypto v0.0.1 h1:B6InT8CLfSLc6nGRVx9VMJRBBazFMjr293+jl0lLXUY=
42
github.com/go-pay/crypto v0.0.1/go.mod h1:41oEIvHMKbNcYlWUlRWtsnC6+ASgh7u29z0gJXe5bes=
53
github.com/go-pay/errgroup v0.0.2 h1:5mZMdm0TDClDm2S3G0/sm0f8AuQRtz0dOrTHDR9R8Cc=
64
github.com/go-pay/errgroup v0.0.2/go.mod h1:0+4b8mvFMS71MIzsaC+gVvB4x37I93lRb2dqrwuU8x8=
75
github.com/go-pay/util v0.0.2 h1:goJ4f6kNY5zzdtg1Cj8oWC+Cw7bfg/qq2rJangMAb9U=
86
github.com/go-pay/util v0.0.2/go.mod h1:qM8VbyF1n7YAPZBSJONSPMPsPedhUTktewUAdf1AjPg=
9-
github.com/go-pay/xhttp v0.0.2 h1:O8rnd/d03WsboFtUthwFMg61ikHRfYHyD1m0JiUx60g=
10-
github.com/go-pay/xhttp v0.0.2/go.mod h1:BnuvXpLKkXTFMOBc5MTb0hxdrstwunbzQPJUZOsNbt4=
117
github.com/go-pay/xlog v0.0.2 h1:kUg5X8/5VZAPDg1J5eGjA3MG0/H5kK6Ew0dW/Bycsws=
128
github.com/go-pay/xlog v0.0.2/go.mod h1:DbjMADPK4+Sjxj28ekK9goqn4zmyY4hql/zRiab+S9E=
139
github.com/go-pay/xtime v0.0.2 h1:7YR4/iuELsEHpJ6LUO0SVK80hQxDO9MLCfuVYIiTCRM=
1410
github.com/go-pay/xtime v0.0.2/go.mod h1:W1yRbJaSt4CSBcdAtLBQ8xajiN/Pl5hquGczUcUE9xE=
15-
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
16-
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
1711
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
1812
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=

lakala/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"time"
1313

1414
"github.com/go-pay/gopay"
15+
"github.com/go-pay/gopay/pkg/xhttp"
1516
"github.com/go-pay/util"
16-
"github.com/go-pay/xhttp"
1717
"github.com/go-pay/xlog"
1818
)
1919

paypal/access_token.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"time"
1010

1111
"github.com/go-pay/gopay"
12+
"github.com/go-pay/gopay/pkg/xhttp"
1213
"github.com/go-pay/util/retry"
13-
"github.com/go-pay/xhttp"
1414
"github.com/go-pay/xlog"
1515
)
1616

paypal/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55

66
"github.com/go-pay/gopay"
7-
"github.com/go-pay/xhttp"
7+
"github.com/go-pay/gopay/pkg/xhttp"
88
)
99

1010
// Client PayPal支付客户端

pkg/xhttp/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## xhttp
2+
3+
http request library for Go

pkg/xhttp/client.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package xhttp
2+
3+
import (
4+
"crypto/tls"
5+
"net"
6+
"net/http"
7+
"time"
8+
)
9+
10+
type Client struct {
11+
HttpClient *http.Client
12+
bodySize int // body size limit(MB), default is 10MB
13+
}
14+
15+
func defaultClient() *Client {
16+
return &Client{
17+
HttpClient: &http.Client{
18+
Timeout: 60 * time.Second,
19+
Transport: &http.Transport{
20+
Proxy: http.ProxyFromEnvironment,
21+
DialContext: defaultTransportDialContext(&net.Dialer{
22+
Timeout: 30 * time.Second,
23+
KeepAlive: 30 * time.Second,
24+
}),
25+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
26+
MaxIdleConns: 100,
27+
IdleConnTimeout: 90 * time.Second,
28+
TLSHandshakeTimeout: 10 * time.Second,
29+
ExpectContinueTimeout: 1 * time.Second,
30+
DisableKeepAlives: true,
31+
ForceAttemptHTTP2: true,
32+
},
33+
},
34+
bodySize: 10, // default is 10MB
35+
}
36+
}
37+
38+
// NewClient , default tls.Config{InsecureSkipVerify: true}
39+
func NewClient() (client *Client) {
40+
return defaultClient()
41+
}
42+
43+
func (c *Client) SetTransport(transport *http.Transport) (client *Client) {
44+
c.HttpClient.Transport = transport
45+
return c
46+
}
47+
48+
func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
49+
c.HttpClient.Transport.(*http.Transport).TLSClientConfig = tlsCfg
50+
return c
51+
}
52+
53+
func (c *Client) SetTimeout(timeout time.Duration) (client *Client) {
54+
c.HttpClient.Timeout = timeout
55+
return c
56+
}
57+
58+
// set body size (MB), default is 10MB
59+
func (c *Client) SetBodySize(sizeMB int) (client *Client) {
60+
c.bodySize = sizeMB
61+
return c
62+
}
63+
64+
// typeStr is request type and response type
65+
// default is TypeJSON
66+
// first param is request type
67+
// second param is response data type
68+
func (c *Client) Req(typeStr ...string) *Request {
69+
var (
70+
reqTp = TypeJSON // default
71+
resTp = ResTypeJSON // default
72+
tLen = len(typeStr)
73+
)
74+
switch {
75+
case tLen == 1:
76+
tpp := typeStr[0]
77+
if _, ok := _ReqContentTypeMap[tpp]; ok {
78+
reqTp = tpp
79+
}
80+
case tLen > 1:
81+
// first param is request type
82+
tpp := typeStr[0]
83+
if _, ok := _ReqContentTypeMap[tpp]; ok {
84+
reqTp = tpp
85+
}
86+
// second param is response data type
87+
stpp := typeStr[1]
88+
if _, ok := _ResTypeMap[stpp]; ok {
89+
resTp = stpp
90+
}
91+
}
92+
if c == nil {
93+
c = defaultClient()
94+
}
95+
r := &Request{
96+
client: c,
97+
Header: make(http.Header),
98+
requestType: reqTp,
99+
responseType: resTp,
100+
}
101+
r.Header.Set("Content-Type", _ReqContentTypeMap[reqTp])
102+
return r
103+
}

0 commit comments

Comments
 (0)