From 74540030c92523fa0038ed4198d7bb11187a3676 Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Sun, 2 Jun 2024 20:27:05 +0800 Subject: [PATCH] udpate --- alipay/ant.go | 2 +- alipay/ant_test.go | 29 +++++++++++++++++++++++++++++ alipay/goods_api.go | 2 +- alipay/goods_api_test.go | 35 +++++++++++++++++++++++++++++++++++ alipay/marketing_api_test.go | 30 ++++++++++++++++++++++++++++++ alipay/marketing_voucher.go | 2 +- alipay/request.go | 11 +++++++++-- 7 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 alipay/goods_api_test.go diff --git a/alipay/ant.go b/alipay/ant.go index 4de46fed..0ef080aa 100644 --- a/alipay/ant.go +++ b/alipay/ant.go @@ -147,7 +147,7 @@ func (a *Client) AntMerchantExpandIndirectImageUpload(ctx context.Context, bm go } bm.Set("image_content", file) var bs []byte - if bs, err = a.FileUploadRequest(ctx, bm, file, "ant.merchant.expand.indirect.image.upload"); err != nil { + if bs, err = a.FileUploadRequest(ctx, bm, "ant.merchant.expand.indirect.image.upload"); err != nil { return nil, err } aliRsp = new(AntMerchantExpandIndirectImageUploadRsp) diff --git a/alipay/ant_test.go b/alipay/ant_test.go index 8f011533..d826076c 100644 --- a/alipay/ant_test.go +++ b/alipay/ant_test.go @@ -1,6 +1,8 @@ package alipay import ( + "io" + "os" "testing" "github.com/go-pay/gopay" @@ -112,3 +114,30 @@ func TestAntMerchantShopClose(t *testing.T) { } xlog.Debug("aliRsp:", *aliRsp) } + +func TestAntMerchantExpandIndirectImageUpload(t *testing.T) { + // 请求参数 + logo, err := os.Open("../logo.png") + if err != nil { + xlog.Errorf("os.Open(%s),error:%+v", "../logo.png", err) + return + } + xlog.Warnf("fileName: %s", logo.Name()) + allBs, err := io.ReadAll(logo) + if err != nil { + xlog.Errorf("io.ReadAll(%s),error:%+v", logo.Name(), err) + return + } + f := &gopay.File{ + Name: "logo", + Content: allBs, + } + bm := make(gopay.BodyMap) + bm.Set("image_type", "png") + aliRsp, err := client.AntMerchantExpandIndirectImageUpload(ctx, bm, f) + if err != nil { + xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err) + return + } + xlog.Debug("aliRsp:", *aliRsp) +} diff --git a/alipay/goods_api.go b/alipay/goods_api.go index 17fa8467..c8706033 100644 --- a/alipay/goods_api.go +++ b/alipay/goods_api.go @@ -19,7 +19,7 @@ func (a *Client) MerchantItemFileUpload(ctx context.Context, file *gopay.File) ( bm.Set("file_content", file) //素材固定值 var bs []byte - if bs, err = a.FileUploadRequest(ctx, bm, file, "alipay.merchant.item.file.upload"); err != nil { + if bs, err = a.FileUploadRequest(ctx, bm, "alipay.merchant.item.file.upload"); err != nil { return nil, err } aliRsp = new(MerchantItemFileUploadRsp) diff --git a/alipay/goods_api_test.go b/alipay/goods_api_test.go new file mode 100644 index 00000000..5dff884a --- /dev/null +++ b/alipay/goods_api_test.go @@ -0,0 +1,35 @@ +package alipay + +import ( + "io" + "os" + "testing" + + "github.com/go-pay/gopay" + "github.com/go-pay/xlog" +) + +func TestMerchantItemFileUpload(t *testing.T) { + // 请求参数 + logo, err := os.Open("../logo.png") + if err != nil { + xlog.Errorf("os.Open(%s),error:%+v", "../logo.png", err) + return + } + xlog.Warnf("fileName: %s", logo.Name()) + allBs, err := io.ReadAll(logo) + if err != nil { + xlog.Errorf("io.ReadAll(%s),error:%+v", logo.Name(), err) + return + } + f := &gopay.File{ + Name: "logo", + Content: allBs, + } + aliRsp, err := client.MerchantItemFileUpload(ctx, f) + if err != nil { + xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err) + return + } + xlog.Debug("aliRsp:", *aliRsp) +} diff --git a/alipay/marketing_api_test.go b/alipay/marketing_api_test.go index 65022c08..8a89a965 100644 --- a/alipay/marketing_api_test.go +++ b/alipay/marketing_api_test.go @@ -1,6 +1,8 @@ package alipay import ( + "io" + "os" "testing" "github.com/go-pay/gopay" @@ -104,3 +106,31 @@ func TestMarketingCampaignCashDetailQuery(t *testing.T) { } xlog.Debug("aliRsp.Response:", aliRsp.Response) } + +func TestMarketingMaterialImageUpload(t *testing.T) { + // 请求参数 + logo, err := os.Open("../logo.png") + if err != nil { + xlog.Errorf("os.Open(%s),error:%+v", "../logo.png", err) + return + } + xlog.Warnf("fileName: %s", logo.Name()) + allBs, err := io.ReadAll(logo) + if err != nil { + xlog.Errorf("io.ReadAll(%s),error:%+v", logo.Name(), err) + return + } + f := &gopay.File{ + Name: "logo", + 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) + if err != nil { + xlog.Errorf("client.AntMerchantExpandIndirectImageUpload(),error:%+v", err) + return + } + xlog.Debug("aliRsp:", *aliRsp) +} diff --git a/alipay/marketing_voucher.go b/alipay/marketing_voucher.go index 7586cd37..67ad45b2 100644 --- a/alipay/marketing_voucher.go +++ b/alipay/marketing_voucher.go @@ -20,7 +20,7 @@ func (a *Client) MarketingMaterialImageUpload(ctx context.Context, bm gopay.Body } bm.Set("file_content", file) var bs []byte - if bs, err = a.FileUploadRequest(ctx, bm, file, "alipay.marketing.material.image.upload"); err != nil { + if bs, err = a.FileUploadRequest(ctx, bm, "alipay.marketing.material.image.upload"); err != nil { return nil, err } aliRsp = new(MarketingMaterialImageUploadRsp) diff --git a/alipay/request.go b/alipay/request.go index a8365b3b..0d2a0347 100644 --- a/alipay/request.go +++ b/alipay/request.go @@ -281,7 +281,7 @@ func (a *Client) PageExecute(ctx context.Context, bm gopay.BodyMap, method strin } // 文件上传 -func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, file *gopay.File, method string) (bs []byte, err error) { +func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method string) (bs []byte, err error) { var ( aat string ) @@ -321,12 +321,19 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, file * if aat != gopay.NULL { pubBody.Set("app_auth_token", aat) } + // 文件也需要签名 + for k, v := range bm { + pubBody.Set(k, v) + } // sign sign, err := a.getRsaSign(pubBody, pubBody.GetString("sign_type")) if err != nil { return nil, fmt.Errorf("GetRsaSign Error: %w", err) } - //pubBody.Set("file_content", file.Content) + // 文件签名完移除query params + for k, _ := range bm { + pubBody.Remove(k) + } pubBody.Set("sign", sign) if a.DebugSwitch == gopay.DebugOn { xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())