Skip to content

Commit

Permalink
udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Jun 2, 2024
1 parent 7454003 commit 0b26439
Show file tree
Hide file tree
Showing 35 changed files with 504 additions and 63 deletions.
8 changes: 2 additions & 6 deletions alipay/ant.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,11 @@ func (a *Client) AntMerchantShopClose(ctx context.Context, bm gopay.BodyMap) (al
// ant.merchant.expand.indirect.image.upload(图片上传)
// bm参数中 image_content 可不传,file为必传参数
// 文档地址:https://opendocs.alipay.com/open/04fgwt
func (a *Client) AntMerchantExpandIndirectImageUpload(ctx context.Context, bm gopay.BodyMap, file *gopay.File) (aliRsp *AntMerchantExpandIndirectImageUploadRsp, err error) {
if file == nil {
return nil, fmt.Errorf("file is nil")
}
err = bm.CheckEmptyError("image_type")
func (a *Client) AntMerchantExpandIndirectImageUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *AntMerchantExpandIndirectImageUploadRsp, err error) {
err = bm.CheckEmptyError("image_type", "image_content")
if err != nil {
return nil, err
}
bm.Set("image_content", file)
var bs []byte
if bs, err = a.FileUploadRequest(ctx, bm, "ant.merchant.expand.indirect.image.upload"); err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions alipay/ant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ func TestAntMerchantExpandIndirectImageUpload(t *testing.T) {
return
}
f := &gopay.File{
Name: "logo",
Name: "logo.png",
Content: allBs,
}
bm := make(gopay.BodyMap)
bm.Set("image_type", "png")
aliRsp, err := client.AntMerchantExpandIndirectImageUpload(ctx, bm, f)
bm.SetFormFile("image_content", f)
aliRsp, err := client.AntMerchantExpandIndirectImageUpload(ctx, bm)
if err != nil {
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/go-pay/crypto/xpem"
"github.com/go-pay/crypto/xrsa"
"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xlog"
"github.com/go-pay/xtime"
)
Expand Down
13 changes: 7 additions & 6 deletions alipay/request.go → alipay/client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xlog"
"github.com/go-pay/xtime"
)
Expand Down Expand Up @@ -321,9 +321,11 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
if aat != gopay.NULL {
pubBody.Set("app_auth_token", aat)
}
// 文件也需要签名
// 文件上传除文件外其他参数也需要签名
for k, v := range bm {
pubBody.Set(k, v)
if _, ok := v.(*gopay.File); !ok {
pubBody.Set(k, v)
}
}
// sign
sign, err := a.getRsaSign(pubBody, pubBody.GetString("sign_type"))
Expand All @@ -336,10 +338,9 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
}
pubBody.Set("sign", sign)
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())
xlog.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
}
param := pubBody.EncodeURLParams()
url := baseUrlUtf8 + "&" + param
url := baseUrlUtf8 + "&" + pubBody.EncodeURLParams()

res, bs, err := a.hc.Req(xhttp.TypeMultipartFormData).Post(url).
SendMultipartBodyMap(bm).EndBytes(ctx)
Expand Down
2 changes: 1 addition & 1 deletion alipay/common_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/go-pay/crypto/xpem"
"github.com/go-pay/crypto/xrsa"
"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xtime"
)

Expand Down
2 changes: 1 addition & 1 deletion alipay/customs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xlog"
)

Expand Down
11 changes: 4 additions & 7 deletions alipay/goods_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import (

// alipay.merchant.item.file.upload(商品文件上传接口)
// 文档地址:https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload
func (a *Client) MerchantItemFileUpload(ctx context.Context, file *gopay.File) (aliRsp *MerchantItemFileUploadRsp, err error) {
if file == nil {
return nil, fmt.Errorf("file is nil")
func (a *Client) MerchantItemFileUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *MerchantItemFileUploadRsp, err error) {
err = bm.CheckEmptyError("scene", "file_content")
if err != nil {
return nil, err
}
bm := make(gopay.BodyMap)
bm.Set("scene", "SYNC_ORDER") //素材固定值
bm.Set("file_content", file) //素材固定值

var bs []byte
if bs, err = a.FileUploadRequest(ctx, bm, "alipay.merchant.item.file.upload"); err != nil {
return nil, err
Expand Down
9 changes: 6 additions & 3 deletions alipay/goods_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ func TestMerchantItemFileUpload(t *testing.T) {
return
}
f := &gopay.File{
Name: "logo",
Name: "logo.png",
Content: allBs,
}
aliRsp, err := client.MerchantItemFileUpload(ctx, f)
bm := make(gopay.BodyMap)
bm.Set("scene", "SYNC_ORDER") // 素材固定值
bm.SetFormFile("file_content", f)
aliRsp, err := client.MerchantItemFileUpload(ctx, bm)
if err != nil {
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
xlog.Errorf("client.MerchantItemFileUpload(),error:%+v", err)
return
}
xlog.Debug("aliRsp:", *aliRsp)
Expand Down
8 changes: 4 additions & 4 deletions alipay/marketing_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func TestMarketingMaterialImageUpload(t *testing.T) {
return
}
f := &gopay.File{
Name: "logo",
Name: "logo.png",
Content: allBs,
}
bm := make(gopay.BodyMap)
bm.Set("file_key", "PROMO_VOUCHER_IMAGE").
Set("merchant_access_mode", "SELF_MODE")
aliRsp, err := client.MarketingMaterialImageUpload(ctx, bm, f)
SetFormFile("file_content", f)
aliRsp, err := client.MarketingMaterialImageUpload(ctx, bm)
if err != nil {
xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err)
xlog.Errorf("client.MarketingMaterialImageUpload(),error:%+v", err)
return
}
xlog.Debug("aliRsp:", *aliRsp)
Expand Down
8 changes: 2 additions & 6 deletions alipay/marketing_voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import (
// alipay.marketing.material.image.upload(营销图片资源上传接口)
// bm参数中 file_content 可不传,file为必传参数
// 文档地址:https://opendocs.alipay.com/open/389b24b6_alipay.marketing.material.image.upload
func (a *Client) MarketingMaterialImageUpload(ctx context.Context, bm gopay.BodyMap, file *gopay.File) (aliRsp *MarketingMaterialImageUploadRsp, err error) {
if file == nil {
return nil, fmt.Errorf("file is nil")
}
if err = bm.CheckEmptyError("file_key"); err != nil {
func (a *Client) MarketingMaterialImageUpload(ctx context.Context, bm gopay.BodyMap) (aliRsp *MarketingMaterialImageUploadRsp, err error) {
if err = bm.CheckEmptyError("file_key", "file_content"); err != nil {
return nil, err
}
bm.Set("file_content", file)
var bs []byte
if bs, err = a.FileUploadRequest(ctx, bm, "alipay.marketing.material.image.upload"); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion allinpay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/go-pay/crypto/xpem"
"github.com/go-pay/crypto/xrsa"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/util"
"github.com/go-pay/xhttp"
)

type Client struct {
Expand Down
2 changes: 1 addition & 1 deletion apple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
)

// Client AppleClient
Expand Down
2 changes: 1 addition & 1 deletion apple/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package apple
import (
"context"

"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
)

const (
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ require (
github.com/go-pay/crypto v0.0.1
github.com/go-pay/errgroup v0.0.2
github.com/go-pay/util v0.0.2
github.com/go-pay/xhttp v0.0.2
github.com/go-pay/xlog v0.0.2
github.com/go-pay/xtime v0.0.2
golang.org/x/crypto v0.23.0
)

require github.com/go-pay/bm v0.0.2 // indirect
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
github.com/go-pay/bm v0.0.2 h1:CLa5hNTIm6uv8HSWGWd4moZbuOzQqJo5HcScXBBSY/E=
github.com/go-pay/bm v0.0.2/go.mod h1:nyglxC5YS4+gVaO9TxLMI0I/naFUqwffdt5n6llM8uM=
github.com/go-pay/crypto v0.0.1 h1:B6InT8CLfSLc6nGRVx9VMJRBBazFMjr293+jl0lLXUY=
github.com/go-pay/crypto v0.0.1/go.mod h1:41oEIvHMKbNcYlWUlRWtsnC6+ASgh7u29z0gJXe5bes=
github.com/go-pay/errgroup v0.0.2 h1:5mZMdm0TDClDm2S3G0/sm0f8AuQRtz0dOrTHDR9R8Cc=
github.com/go-pay/errgroup v0.0.2/go.mod h1:0+4b8mvFMS71MIzsaC+gVvB4x37I93lRb2dqrwuU8x8=
github.com/go-pay/util v0.0.2 h1:goJ4f6kNY5zzdtg1Cj8oWC+Cw7bfg/qq2rJangMAb9U=
github.com/go-pay/util v0.0.2/go.mod h1:qM8VbyF1n7YAPZBSJONSPMPsPedhUTktewUAdf1AjPg=
github.com/go-pay/xhttp v0.0.2 h1:O8rnd/d03WsboFtUthwFMg61ikHRfYHyD1m0JiUx60g=
github.com/go-pay/xhttp v0.0.2/go.mod h1:BnuvXpLKkXTFMOBc5MTb0hxdrstwunbzQPJUZOsNbt4=
github.com/go-pay/xlog v0.0.2 h1:kUg5X8/5VZAPDg1J5eGjA3MG0/H5kK6Ew0dW/Bycsws=
github.com/go-pay/xlog v0.0.2/go.mod h1:DbjMADPK4+Sjxj28ekK9goqn4zmyY4hql/zRiab+S9E=
github.com/go-pay/xtime v0.0.2 h1:7YR4/iuELsEHpJ6LUO0SVK80hQxDO9MLCfuVYIiTCRM=
github.com/go-pay/xtime v0.0.2/go.mod h1:W1yRbJaSt4CSBcdAtLBQ8xajiN/Pl5hquGczUcUE9xE=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
2 changes: 1 addition & 1 deletion lakala/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/util"
"github.com/go-pay/xhttp"
"github.com/go-pay/xlog"
)

Expand Down
2 changes: 1 addition & 1 deletion paypal/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/util/retry"
"github.com/go-pay/xhttp"
"github.com/go-pay/xlog"
)

Expand Down
2 changes: 1 addition & 1 deletion paypal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/go-pay/gopay"
"github.com/go-pay/xhttp"
"github.com/go-pay/gopay/pkg/xhttp"
)

// Client PayPal支付客户端
Expand Down
3 changes: 3 additions & 0 deletions pkg/xhttp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## xhttp

http request library for Go
103 changes: 103 additions & 0 deletions pkg/xhttp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package xhttp

import (
"crypto/tls"
"net"
"net/http"
"time"
)

type Client struct {
HttpClient *http.Client
bodySize int // body size limit(MB), default is 10MB
}

func defaultClient() *Client {
return &Client{
HttpClient: &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: defaultTransportDialContext(&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true,
ForceAttemptHTTP2: true,
},
},
bodySize: 10, // default is 10MB
}
}

// NewClient , default tls.Config{InsecureSkipVerify: true}
func NewClient() (client *Client) {
return defaultClient()
}

func (c *Client) SetTransport(transport *http.Transport) (client *Client) {
c.HttpClient.Transport = transport
return c
}

func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
c.HttpClient.Transport.(*http.Transport).TLSClientConfig = tlsCfg
return c
}

func (c *Client) SetTimeout(timeout time.Duration) (client *Client) {
c.HttpClient.Timeout = timeout
return c
}

// set body size (MB), default is 10MB
func (c *Client) SetBodySize(sizeMB int) (client *Client) {
c.bodySize = sizeMB
return c
}

// typeStr is request type and response type
// default is TypeJSON
// first param is request type
// second param is response data type
func (c *Client) Req(typeStr ...string) *Request {
var (
reqTp = TypeJSON // default
resTp = ResTypeJSON // default
tLen = len(typeStr)
)
switch {
case tLen == 1:
tpp := typeStr[0]
if _, ok := _ReqContentTypeMap[tpp]; ok {
reqTp = tpp
}
case tLen > 1:
// first param is request type
tpp := typeStr[0]
if _, ok := _ReqContentTypeMap[tpp]; ok {
reqTp = tpp
}
// second param is response data type
stpp := typeStr[1]
if _, ok := _ResTypeMap[stpp]; ok {
resTp = stpp
}
}
if c == nil {
c = defaultClient()
}
r := &Request{
client: c,
Header: make(http.Header),
requestType: reqTp,
responseType: resTp,
}
r.Header.Set("Content-Type", _ReqContentTypeMap[reqTp])
return r
}
Loading

0 comments on commit 0b26439

Please sign in to comment.