From f1b0cc3b19a0efd8c50af788027ccca3c84f76b0 Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Sat, 1 Jun 2024 13:44:21 +0800 Subject: [PATCH] udpate --- alipay/ant.go | 29 +++++++++++++++++++++ alipay/goods_api.go | 6 ++++- alipay/marketing_voucher.go | 10 +++++++- alipay/model_ant.go | 12 +++++++++ alipay/request.go | 50 ++++++++++++++++--------------------- doc/alipay.md | 2 +- release_note.txt | 2 +- 7 files changed, 78 insertions(+), 33 deletions(-) diff --git a/alipay/ant.go b/alipay/ant.go index 4d47c962..4de46fed 100644 --- a/alipay/ant.go +++ b/alipay/ant.go @@ -133,3 +133,32 @@ func (a *Client) AntMerchantShopClose(ctx context.Context, bm gopay.BodyMap) (al aliRsp.SignData = signData return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr) } + +// 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") + if err != nil { + return nil, err + } + bm.Set("image_content", file) + var bs []byte + if bs, err = a.FileUploadRequest(ctx, bm, file, "ant.merchant.expand.indirect.image.upload"); err != nil { + return nil, err + } + aliRsp = new(AntMerchantExpandIndirectImageUploadRsp) + if err = json.Unmarshal(bs, aliRsp); err != nil { + return nil, err + } + if aliRsp.Response != nil && aliRsp.Response.Code != "10000" { + info := aliRsp.Response + return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg) + } + signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) + aliRsp.SignData = signData + return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr) +} diff --git a/alipay/goods_api.go b/alipay/goods_api.go index 9ceacca8..17fa8467 100644 --- a/alipay/goods_api.go +++ b/alipay/goods_api.go @@ -11,11 +11,15 @@ 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") + } bm := make(gopay.BodyMap) bm.Set("scene", "SYNC_ORDER") //素材固定值 + bm.Set("file_content", file) //素材固定值 var bs []byte - if bs, err = a.FileRequest(ctx, bm, file, "alipay.merchant.item.file.upload"); err != nil { + if bs, err = a.FileUploadRequest(ctx, bm, file, "alipay.merchant.item.file.upload"); err != nil { return nil, err } aliRsp = new(MerchantItemFileUploadRsp) diff --git a/alipay/marketing_voucher.go b/alipay/marketing_voucher.go index 582db02d..7586cd37 100644 --- a/alipay/marketing_voucher.go +++ b/alipay/marketing_voucher.go @@ -9,10 +9,18 @@ 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 { + return nil, err + } + bm.Set("file_content", file) var bs []byte - if bs, err = a.FileRequest(ctx, bm, file, "alipay.marketing.material.image.upload"); err != nil { + if bs, err = a.FileUploadRequest(ctx, bm, file, "alipay.marketing.material.image.upload"); err != nil { return nil, err } aliRsp = new(MarketingMaterialImageUploadRsp) diff --git a/alipay/model_ant.go b/alipay/model_ant.go index e9ea95d5..a464ae37 100644 --- a/alipay/model_ant.go +++ b/alipay/model_ant.go @@ -42,6 +42,13 @@ type AntMerchantShopCloseRsp struct { Sign string `json:"sign"` } +type AntMerchantExpandIndirectImageUploadRsp struct { + Response *AntMerchantExpandIndirectImageUpload `json:"ant_merchant_expand_indirect_image_upload_response"` + AlipayCertSn string `json:"alipay_cert_sn,omitempty"` + SignData string `json:"-"` + Sign string `json:"sign"` +} + // =========================================================分割========================================================= type AntMerchantShopModify struct { @@ -130,3 +137,8 @@ type AntMerchantShopQuery struct { type AntMerchantShopClose struct { ErrorResponse } + +type AntMerchantExpandIndirectImageUpload struct { + ErrorResponse + ImageId string `json:"image_id"` +} diff --git a/alipay/request.go b/alipay/request.go index f4233952..a8365b3b 100644 --- a/alipay/request.go +++ b/alipay/request.go @@ -140,20 +140,20 @@ func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string, ) if bm != nil { _, has := appAuthTokenInBizContent[method] - if has { + if !has { + aat := bm.GetString("app_auth_token") + bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return nil, fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Remove("app_auth_token") + bm.Set("app_auth_token", aat) } else { - aat := bm.GetString("app_auth_token") - bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return nil, fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Set("app_auth_token", aat) + bm.Remove("app_auth_token") } } // 处理公共参数 @@ -196,20 +196,20 @@ func (a *Client) DoAliPay(ctx context.Context, bm gopay.BodyMap, method string, ) if bm != nil { _, has := appAuthTokenInBizContent[method] - if has { + if !has { + aat := bm.GetString("app_auth_token") + bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return nil, fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Remove("app_auth_token") + bm.Set("app_auth_token", aat) } else { - aat := bm.GetString("app_auth_token") - bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return nil, fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Set("app_auth_token", aat) + bm.Remove("app_auth_token") } } // 处理公共参数 @@ -252,20 +252,20 @@ func (a *Client) PageExecute(ctx context.Context, bm gopay.BodyMap, method strin ) if bm != nil { _, has := appAuthTokenInBizContent[method] - if has { + if !has { + aat := bm.GetString("app_auth_token") + bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return "", fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Remove("app_auth_token") + bm.Set("app_auth_token", aat) } else { - aat := bm.GetString("app_auth_token") - bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return "", fmt.Errorf("json.Marshal:%w", err) } bizContent = string(bodyBs) - bm.Set("app_auth_token", aat) + bm.Remove("app_auth_token") } } // 处理公共参数 @@ -281,19 +281,13 @@ func (a *Client) PageExecute(ctx context.Context, bm gopay.BodyMap, method strin } // 文件上传 -func (a *Client) FileRequest(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, file *gopay.File, method string) (bs []byte, err error) { var ( - bodyStr string - bodyBs []byte - aat string + aat string ) if bm != nil { aat = bm.GetString("app_auth_token") bm.Remove("app_auth_token") - if bodyBs, err = json.Marshal(bm); err != nil { - return nil, fmt.Errorf("json.Marshal:%w", err) - } - bodyStr = string(bodyBs) } pubBody := make(gopay.BodyMap) pubBody.Set("app_id", a.AppId). @@ -302,7 +296,6 @@ func (a *Client) FileRequest(ctx context.Context, bm gopay.BodyMap, file *gopay. Set("charset", a.Charset). Set("sign_type", a.SignType). Set("version", "1.0"). - Set("scene", "SYNC_ORDER"). Set("timestamp", time.Now().Format(xtime.TimeLayout)) if a.AppCertSN != gopay.NULL { @@ -320,15 +313,15 @@ func (a *Client) FileRequest(ctx context.Context, bm gopay.BodyMap, file *gopay. if a.NotifyUrl != gopay.NULL { pubBody.Set("notify_url", a.NotifyUrl) } + // default use app_auth_token if a.AppAuthToken != gopay.NULL { pubBody.Set("app_auth_token", a.AppAuthToken) } + // if user set app_auth_token in body_map, use this if aat != gopay.NULL { pubBody.Set("app_auth_token", aat) } - if bodyStr != gopay.NULL { - pubBody.Set("biz_content", bodyStr) - } + // sign sign, err := a.getRsaSign(pubBody, pubBody.GetString("sign_type")) if err != nil { return nil, fmt.Errorf("GetRsaSign Error: %w", err) @@ -340,8 +333,7 @@ func (a *Client) FileRequest(ctx context.Context, bm gopay.BodyMap, file *gopay. } param := pubBody.EncodeURLParams() url := baseUrlUtf8 + "&" + param - bm.Reset() - bm.SetFormFile("file_content", file) + res, bs, err := a.hc.Req(xhttp.TypeMultipartFormData).Post(url). SendMultipartBodyMap(bm).EndBytes(ctx) if err != nil { diff --git a/doc/alipay.md b/doc/alipay.md index b500aab2..d0ebdba4 100644 --- a/doc/alipay.md +++ b/doc/alipay.md @@ -312,7 +312,7 @@ xlog.Infof("%+v", phone) * 店铺分页查询接口:TODO:https://opendocs.alipay.com/open/04fgwq * 店铺查询接口:`client.AntMerchantShopQuery()` * 蚂蚁店铺关闭接口:`client.AntMerchantShopClose()` - * 图片上传接口:TODO:https://opendocs.alipay.com/open/04fgwt + * 图片上传接口:`client.AntMerchantExpandIndirectImageUpload()` * 商户mcc信息查询接口:TODO:https://opendocs.alipay.com/open/04fgwu * 店铺增加收单账号接口:TODO:https://opendocs.alipay.com/open/54b69b89_ant.merchant.expand.shop.receiptaccount.save * 商家券 2.0 diff --git a/release_note.txt b/release_note.txt index ff2d6e2c..c069cf84 100644 --- a/release_note.txt +++ b/release_note.txt @@ -14,9 +14,9 @@ (12) 支付宝:新增 client.MarketingActivityDeliveryQuery(),查询推广计划接口。 (13) 支付宝:新增 client.MarketingActivityDeliveryStop(),停止推广计划接口。 (14) 支付宝:新增 client.MarketingMaterialImageUpload(),营销图片资源上传接口。 + (15) 支付宝:新增 client.AntMerchantExpandIndirectImageUpload(),图片上传接口。 - (15) 支付宝:新增 client.ZolozAuthenticationCustomerSmilepayQuery(),刷脸支付初始化接口。 (16) 支付宝:新增 client.ZolozAuthenticationCustomerSmilepayQuery(),刷脸支付初始化接口。 (17) 支付宝:新增 client.ZolozAuthenticationCustomerSmilepayQuery(),刷脸支付初始化接口。 (18) 支付宝:新增 client.ZolozAuthenticationCustomerSmilepayQuery(),刷脸支付初始化接口。