From 9d7c9f86e1660f0d82d34981d0440bc885497708 Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Tue, 5 Apr 2022 16:21:40 +0800 Subject: [PATCH] v1.5.76 (#240) * v1.5.76 --- alipay/ant.go | 54 +++--- alipay/ant_test.go | 5 + ...1000117673683.crt => appCertPublicKey.crt} | 0 alipay/cert/cert.go | 2 +- alipay/client.go | 9 +- alipay/client_test.go | 6 +- alipay/common_api.go | 13 +- alipay/customs.go | 9 +- alipay/data_api.go | 18 +- alipay/error.go | 22 ++- alipay/error_test.go | 5 +- alipay/funds_api.go | 147 +++++++-------- alipay/funds_api_test.go | 10 +- alipay/koubei_api.go | 90 ++++----- alipay/marketing_api.go | 9 +- alipay/member_api.go | 173 ++++++++--------- alipay/member_api_test.go | 102 ++++++++-- alipay/payment_api.go | 177 ++++++++---------- alipay/payment_api_test.go | 126 ++++++++++--- alipay/sign.go | 22 ++- alipay/util_api.go | 29 ++- alipay/util_api_test.go | 26 ++- alipay/zhima_api.go | 171 ++++++++--------- alipay/zhima_api_test.go | 39 ++-- body_map.go | 4 +- constant.go | 2 +- doc/alipay.md | 22 ++- error.go | 16 ++ go.mod | 2 +- go.sum | 4 +- paypal/client.go | 4 + paypal/order.go | 10 +- paypal/payment.go | 12 +- pkg/jwt/rsa.go | 20 +- pkg/xhttp/client.go | 13 +- qq/client.go | 14 +- qq/oplatform_api.go | 8 +- qq/payment_api.go | 6 +- qq/red.go | 4 +- release_note.txt | 6 + wechat/base_api.go | 8 +- wechat/client.go | 4 +- wechat/customs.go | 6 +- wechat/entrust.go | 8 +- wechat/merchant.go | 24 +-- wechat/oplatform_api.go | 3 - wechat/payment_api.go | 4 +- wechat/public_api.go | 6 - wechat/red.go | 8 +- wechat/v3/applyment_for_sub.go | 23 ++- wechat/v3/bank.go | 13 +- wechat/v3/bill.go | 8 +- wechat/v3/business_circle.go | 2 +- wechat/v3/cert.go | 2 +- wechat/v3/client.go | 5 +- wechat/v3/complaint.go | 14 +- wechat/v3/discount_card.go | 4 +- wechat/v3/ecommerce.go | 40 ++-- wechat/v3/gold_plan.go | 4 +- wechat/v3/market_busifavor.go | 32 ++-- wechat/v3/market_favor.go | 28 +-- wechat/v3/market_media.go | 2 +- wechat/v3/market_partner.go | 6 +- wechat/v3/media.go | 4 +- wechat/v3/merchant.go | 10 +- wechat/v3/pay.go | 10 +- wechat/v3/pay_combine.go | 10 +- wechat/v3/pay_partner.go | 10 +- wechat/v3/profit_share.go | 20 +- wechat/v3/refund.go | 14 +- wechat/v3/score.go | 22 +-- wechat/v3/sign.go | 8 +- wechat/v3/smart_guide.go | 4 +- wechat/v3/transfer.go | 28 +-- wechat/v3/withdraw.go | 6 +- 75 files changed, 976 insertions(+), 835 deletions(-) rename alipay/cert/{appCertPublicKey_2021000117673683.crt => appCertPublicKey.crt} (100%) create mode 100644 error.go delete mode 100644 wechat/public_api.go diff --git a/alipay/ant.go b/alipay/ant.go index 56b0beab..cf7c6e93 100644 --- a/alipay/ant.go +++ b/alipay/ant.go @@ -16,12 +16,11 @@ func (a *Client) AntMerchantShopModify(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(AntMerchantShopModifyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -40,12 +39,11 @@ func (a *Client) AntMerchantShopCreate(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(AntMerchantShopCreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -64,12 +62,11 @@ func (a *Client) AntMerchantShopConsult(ctx context.Context, bm gopay.BodyMap) ( return nil, err } aliRsp = new(AntMerchantShopConsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -88,12 +85,11 @@ func (a *Client) AntMerchantOrderQuery(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(AntMerchantOrderQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -108,12 +104,11 @@ func (a *Client) AntMerchantShopQuery(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(AntMerchantShopQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -128,12 +123,11 @@ func (a *Client) AntMerchantShopClose(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(AntMerchantShopCloseRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/ant_test.go b/alipay/ant_test.go index 55c0621b..a9f330f9 100644 --- a/alipay/ant_test.go +++ b/alipay/ant_test.go @@ -36,6 +36,11 @@ func TestAntMerchantShopCreate(t *testing.T) { aliRsp, err := client.AntMerchantShopCreate(ctx, bm) if err != nil { + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } xlog.Errorf("client.AntMerchantShopCreate(%+v),error:%+v", bm, err) return } diff --git a/alipay/cert/appCertPublicKey_2021000117673683.crt b/alipay/cert/appCertPublicKey.crt similarity index 100% rename from alipay/cert/appCertPublicKey_2021000117673683.crt rename to alipay/cert/appCertPublicKey.crt diff --git a/alipay/cert/cert.go b/alipay/cert/cert.go index 4688cd22..77675b01 100644 --- a/alipay/cert/cert.go +++ b/alipay/cert/cert.go @@ -1,7 +1,7 @@ package cert var ( - Appid = "2021000117673683" + Appid = "" // 私钥 PrivateKey = "MIIEowIBAAKCAQEAnG3YbVuCU0QptQAzHnscy1W3a5pehEoGjPdhvpUu2DRtybapSw16d3rVTnMUdZctvMSmU0fXfuOjPFvzuhJGwkwJBnGqPONAt1ourlUzEzl3sIRbcyXMxVFXOcV2DsKFcCATGLhWev48jNfoLNP0AD3H63g70NuLg/Cx1v6fWE8mZBSSINNNK2xDyP/rDRKbCGBKXxYKD6tJXNpc7GOaT8G6IDQTfHzDQSAoMzcW3SultLcp6UdyLP+pibovW410IAL1vCrD6nmunT1eXKzZFHbKPsGC9OVxyYbJIPu6e/K24LcD5s51OP9bmKjlGsj0TeVW1vBnQrfPiyJ1+mxz8QIDAQABAoIBAHJrgpBTDBvd0uf7MmkMQj6U/yos6w+2md4IoS1nL7Gjf964xlIIlNjKNzY/xOpSX3khGkv2U7ETzvW6iKdE8Q7z0PIuuAf7bf3vcA/CcC6SrXKnDcFHF+YInlW6kqUvMa2XOICvbUpfpmibEM+hZWu7xPz+oDogLIegymRpMKLFcl50vd/mpChJifN7k3/4pC/n8SlAB3K1xakRWvsfEyj2DgzSB/CDCSOsVi1J1P9YfZ1dU0E3GOIRFtrbrtagBaOPeYiS1qSkwlCyy9UXo3RAsooSjbUZtU+IhuqOFXIySG9gV6gMLFzsaGoRRPCnI9yS/cosSHXo8CTDW8urBqUCgYEA6Dn3wI/nTGceUfD+yaqSV159pwWptSRfhbY5iduJCsbEkI3MhP6WW0vHPL5inixm1Gz2c8/0Xr+5fiwK73vPApQu8tj33wjUYOLsZdi2gXEHyi1e341UwIBUubTO13ZlUtAt9pMaYBusAbGV6JXF5/KJzZ5SnnENWuMBo2OlwqcCgYEArHFuqFiNQ9eGpwv8B802BEVU0HKNxkkKBc11MDLoqF4VXWEUp4eenMVFYrVNyrTfeKszcf2bM7gSMpYc89Nsq7njVQxkaJfS8p/cEV1cGYJrNbEHbSofUfhezfiL1oztFMMnrRNtkP/SSLLn8tPIEnZgCTZXSd4K6aTV/EPN36cCgYEArTQPBSBPczy18Ie+gMsnuuJO9XPTz7cBR8lRwPBslg0KLoaUM91uZ0ltoHCfPToAuA9NAbPon4J/5wsSXOwKyfGb0qTguwjq3hhiGNQ5yvFyzehpCnNu8uTETH3+h9VWXR3IKlAtK+F3uQuqs36BzS2oeiPLm0DWpSMK5cI8r6ECgYAVKxOHbN9ifZmU9f6pQoKoAY7Iy9cliR/G2JLTKNywlmQPA3TSn+5Tlk/PLyxufseT+4gkLRqZPbZWKWFCeSi05+vFK/FDeae2ivMr2ocMxpq8i4+60dnoaRHSPPmFi+UqkE5Vi7X+f7jjnbqlb2P3CDr/xoDum2hBiC+QeQ5TrwKBgEHBg3tMAdDkoC3S6gCxqG2g2nGRJufomFC6a6Nuoi91pn50fP9yode3wO7j/Cc669iA5sWliaPd57ol7mTOcDTQgrkoTJsC0KunJ4141ysU99HSPRdm5ykAfumOoVcN9Vdy8yEeq9zxVk2NGaKy8p+tpnz6N8DDg/8p8tRXuuqU" diff --git a/alipay/client.go b/alipay/client.go index e49d665f..255da762 100644 --- a/alipay/client.go +++ b/alipay/client.go @@ -35,17 +35,20 @@ type Client struct { // 初始化支付宝客户端 // 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN()) -// appId:应用ID +// appid:应用ID // privateKey:应用私钥,支持PKCS1和PKCS8 // isProd:是否是正式环境 -func NewClient(appId, privateKey string, isProd bool) (client *Client, err error) { +func NewClient(appid, privateKey string, isProd bool) (client *Client, err error) { + if appid == util.NULL || privateKey == util.NULL { + return nil, gopay.MissAlipayInitParamErr + } key := xrsa.FormatAlipayPrivateKey(privateKey) priKey, err := xpem.DecodePrivateKey([]byte(key)) if err != nil { return nil, err } client = &Client{ - AppId: appId, + AppId: appid, Charset: UTF8, SignType: RSA2, IsProd: isProd, diff --git a/alipay/client_test.go b/alipay/client_test.go index 2d56b229..3635a534 100644 --- a/alipay/client_test.go +++ b/alipay/client_test.go @@ -22,7 +22,7 @@ var ( func TestMain(m *testing.M) { // 初始化支付宝客户端 - // appId:应用ID + // appid:应用ID // privateKey:应用私钥,支持PKCS1和PKCS8 // isProd:是否是正式环境 client, err = NewClient(cert.Appid, cert.PrivateKey, false) @@ -30,8 +30,8 @@ func TestMain(m *testing.M) { xlog.Error(err) return } - // 打开Debug开关,输出日志 - client.DebugSwitch = gopay.DebugOn + // Debug开关,输出/关闭日志 + client.DebugSwitch = gopay.DebugOff // 配置公共参数 client.SetCharset("utf-8"). diff --git a/alipay/common_api.go b/alipay/common_api.go index f0ae2dda..75f2b9b5 100644 --- a/alipay/common_api.go +++ b/alipay/common_api.go @@ -185,7 +185,7 @@ func systemOauthToken(ctx context.Context, appId string, privateKey *rsa.Private // signType:签名方式 alipay.RSA 或 alipay.RSA2,默认 RSA2 // bizContent:验签时该参数不做任何处理,{任意值},此参数具体看文档 // 文档:https://opendocs.alipay.com/apis/api_9/monitor.heartbeat.syn -func MonitorHeartbeatSyn(ctx context.Context, appId string, privateKey, signType, bizContent string) (rsp *MonitorHeartbeatSynResponse, err error) { +func MonitorHeartbeatSyn(ctx context.Context, appId string, privateKey, signType, bizContent string) (aliRsp *MonitorHeartbeatSynResponse, err error) { key := xrsa.FormatAlipayPrivateKey(privateKey) priKey, err := xpem.DecodePrivateKey([]byte(key)) if err != nil { @@ -216,9 +216,12 @@ func MonitorHeartbeatSyn(ctx context.Context, appId string, privateKey, signType if err != nil { return nil, err } - rsp = new(MonitorHeartbeatSynResponse) - if err = json.Unmarshal(bs, rsp); err != nil { - return nil, err + aliRsp = new(MonitorHeartbeatSynResponse) + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) + } + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } - return rsp, nil + return aliRsp, nil } diff --git a/alipay/customs.go b/alipay/customs.go index 00bb14a6..fd38de86 100644 --- a/alipay/customs.go +++ b/alipay/customs.go @@ -22,12 +22,11 @@ func (a *Client) TradeCustomsDeclare(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(TradeCustomsDeclareRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/data_api.go b/alipay/data_api.go index cbabacba..f802dbd0 100644 --- a/alipay/data_api.go +++ b/alipay/data_api.go @@ -18,12 +18,11 @@ func (a *Client) DataBillBalanceQuery(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(DataBillBalanceQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -42,12 +41,11 @@ func (a *Client) DataBillDownloadUrlQuery(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(DataBillDownloadUrlQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/error.go b/alipay/error.go index 9c2480c9..2632e1ee 100644 --- a/alipay/error.go +++ b/alipay/error.go @@ -1,13 +1,15 @@ package alipay -import "fmt" +import ( + "fmt" +) // BizErr 用于判断支付宝的业务逻辑是否有错误 type BizErr struct { - Code string - Msg string - SubCode string - SubMsg string + Code string `json:"code"` + Msg string `json:"msg"` + SubCode string `json:"sub_code"` + SubMsg string `json:"sub_msg"` } // bizErrCheck 检查业务码是否为10000 否则返回一个BizErr @@ -24,12 +26,12 @@ func bizErrCheck(errRsp ErrorResponse) error { } func (e *BizErr) Error() string { - return fmt.Sprintf(`{"code": "%s","msg": "%s","sub_code": "%s","sub_msg": "%s"}`, e.Code, e.Msg, e.SubCode, e.SubMsg) + return fmt.Sprintf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, e.Code, e.Msg, e.SubCode, e.SubMsg) } -func AsBizError(err error) *BizErr { - if bizerr, ok := err.(*BizErr); ok && bizerr != nil { - return bizerr +func IsBizError(err error) (*BizErr, bool) { + if bizErr, ok := err.(*BizErr); ok { + return bizErr, true } - return nil + return nil, false } diff --git a/alipay/error_test.go b/alipay/error_test.go index b607b0b9..9306c67c 100644 --- a/alipay/error_test.go +++ b/alipay/error_test.go @@ -34,13 +34,12 @@ func TestBizErr_AsBizError(t *testing.T) { } var err error err = bizErrCheck(bizErrRsp) - if bizErr := AsBizError(err); bizErr == nil { + if _, ok := IsBizError(err); !ok { t.Fail() } err = bizErrCheck(noBizErrRsp) - if bizErr := AsBizError(err); bizErr != nil { + if _, ok := IsBizError(err); !ok { t.Fail() } - } diff --git a/alipay/funds_api.go b/alipay/funds_api.go index 5b9fa935..d3bd01ce 100644 --- a/alipay/funds_api.go +++ b/alipay/funds_api.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/go-pay/gopay" + "github.com/go-pay/gopay/pkg/util" ) // alipay.fund.trans.uni.transfer(单笔转账接口) @@ -20,12 +21,11 @@ func (a *Client) FundTransUniTransfer(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(FundTransUniTransferResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -44,12 +44,11 @@ func (a *Client) FundAccountQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(FundAccountQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -64,12 +63,11 @@ func (a *Client) FundTransCommonQuery(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(FundTransCommonQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -92,12 +90,11 @@ func (a *Client) FundTransOrderQuery(ctx context.Context, bm gopay.BodyMap) (ali } aliRsp = new(FundTransOrderQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -116,12 +113,11 @@ func (a *Client) FundTransRefund(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(FundTransRefundResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -140,12 +136,11 @@ func (a *Client) FundAuthOrderFreeze(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(FundAuthOrderFreezeResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -164,12 +159,11 @@ func (a *Client) FundAuthOrderVoucherCreate(ctx context.Context, bm gopay.BodyMa return nil, err } aliRsp = new(FundAuthOrderVoucherCreateResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -181,7 +175,7 @@ func (a *Client) FundAuthOrderVoucherCreate(ctx context.Context, bm gopay.BodyMa func (a *Client) FundAuthOrderAppFreeze(ctx context.Context, bm gopay.BodyMap) (payParam string, err error) { err = bm.CheckEmptyError("out_order_no", "out_request_no", "order_title", "amount", "product_code") if err != nil { - return "", err + return util.NULL, err } var bs []byte if bs, err = a.doAliPay(ctx, bm, "alipay.fund.auth.order.app.freeze"); err != nil { @@ -203,12 +197,11 @@ func (a *Client) FundAuthOrderUnfreeze(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(FundAuthOrderUnfreezeResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -223,12 +216,11 @@ func (a *Client) FundAuthOperationDetailQuery(ctx context.Context, bm gopay.Body return nil, err } aliRsp = new(FundAuthOperationDetailQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -247,12 +239,11 @@ func (a *Client) FundAuthOperationCancel(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(FundAuthOperationCancelResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -271,12 +262,11 @@ func (a *Client) FundBatchCreate(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(FundBatchCreateResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -295,12 +285,11 @@ func (a *Client) FundBatchClose(ctx context.Context, bm gopay.BodyMap) (aliRsp * return nil, err } aliRsp = new(FundBatchCloseResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -319,12 +308,11 @@ func (a *Client) FundBatchDetailQuery(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(FundBatchDetailQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -343,12 +331,11 @@ func (a *Client) FundTransAppPay(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(FundTransAppPayResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -367,12 +354,11 @@ func (a *Client) FundTransPayeeBindQuery(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(FundTransPayeeBindQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -391,12 +377,11 @@ func (a *Client) FundTransPagePay(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(FundTransPagePayRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/funds_api_test.go b/alipay/funds_api_test.go index dfac8e3e..2ea8a3d6 100644 --- a/alipay/funds_api_test.go +++ b/alipay/funds_api_test.go @@ -11,6 +11,7 @@ func TestFundTransUniTransfer(t *testing.T) { bm := make(gopay.BodyMap) bm.Set("out_biz_no", "201806300011232301"). Set("trans_amount", "0.01"). + Set("biz_scene", "DIRECT_TRANSFER"). Set("product_code", "TRANS_ACCOUNT_NO_PWD"). SetBodyMap("payee_info", func(bm gopay.BodyMap) { bm.Set("identity", "85411418@qq.com") @@ -88,11 +89,16 @@ func TestClient_FundTransPagePay(t *testing.T) { bm.Set("out_biz_no", "2018062800001"). Set("trans_amount", "8.88"). Set("product_code", "STD_APP_TRANSFER"). - Set("biz_scene", "PARTY_MEMBERSHIP_DUES") + Set("biz_scene", "PARTY_MEMBERSHIP_DUES"). + SetBodyMap("payee_info", func(b gopay.BodyMap) { + b.Set("identity", "208812*****41234"). + Set("identity_type", "ALIPAY_USER_ID"). + Set("name", "黄龙国际有限公司") + }) aliRsp, err := client.FundTransPagePay(ctx, bm) if err != nil { - xlog.Errorf("client.FundTransPagePay(%+v),error:%+v", bm, err) + //xlog.Errorf("client.FundTransPagePay(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) diff --git a/alipay/koubei_api.go b/alipay/koubei_api.go index 7e56baf4..5e17ed3a 100644 --- a/alipay/koubei_api.go +++ b/alipay/koubei_api.go @@ -20,12 +20,11 @@ func (a *Client) KoubeiTradeOrderAggregateConsult(ctx context.Context, bm gopay. return nil, err } aliRsp = new(KoubeiTradeOrderAggregateConsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -44,12 +43,11 @@ func (a *Client) KoubeiTradeOrderPrecreate(ctx context.Context, bm gopay.BodyMap return nil, err } aliRsp = new(KoubeiTradeOrderPrecreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -68,12 +66,11 @@ func (a *Client) KoubeiTradeItemorderBuy(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(KoubeiTradeItemorderBuyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -92,12 +89,11 @@ func (a *Client) KoubeiTradeOrderConsult(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(KoubeiTradeOrderConsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -116,12 +112,11 @@ func (a *Client) KoubeiTradeItemorderRefund(ctx context.Context, bm gopay.BodyMa return nil, err } aliRsp = new(KoubeiTradeItemorderRefundRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -140,12 +135,11 @@ func (a *Client) KoubeiTradeItemorderQuery(ctx context.Context, bm gopay.BodyMap return nil, err } aliRsp = new(KoubeiTradeItemorderQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -164,12 +158,11 @@ func (a *Client) KoubeiTradeTicketTicketcodeSend(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(KoubeiTradeTicketTicketcodeSendRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -188,12 +181,11 @@ func (a *Client) KoubeiTradeTicketTicketcodeDelay(ctx context.Context, bm gopay. return nil, err } aliRsp = new(KoubeiTradeTicketTicketcodeDelayRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -212,12 +204,11 @@ func (a *Client) KoubeiTradeTicketTicketcodeQuery(ctx context.Context, bm gopay. return nil, err } aliRsp = new(KoubeiTradeTicketTicketcodeQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -236,12 +227,11 @@ func (a *Client) KoubeiTradeTicketTicketcodeCancel(ctx context.Context, bm gopay return nil, err } aliRsp = new(KoubeiTradeTicketTicketcodeCancelRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/marketing_api.go b/alipay/marketing_api.go index 7a07f96b..8997266e 100644 --- a/alipay/marketing_api.go +++ b/alipay/marketing_api.go @@ -20,12 +20,11 @@ func (a *Client) OpenAppQrcodeCreate(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(OpenAppQrcodeCreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/member_api.go b/alipay/member_api.go index 7c01579f..6e88d23e 100644 --- a/alipay/member_api.go +++ b/alipay/member_api.go @@ -22,12 +22,11 @@ func (a *Client) UserInfoShare(ctx context.Context, authToken string) (aliRsp *U return nil, err } aliRsp = new(UserInfoShareResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -46,12 +45,11 @@ func (a *Client) UserCertifyOpenInit(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(UserCertifyOpenInitResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -86,12 +84,11 @@ func (a *Client) UserCertifyOpenQuery(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(UserCertifyOpenQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -103,7 +100,7 @@ func (a *Client) UserCertifyOpenQuery(ctx context.Context, bm gopay.BodyMap) (al func (a *Client) UserAgreementPageSign(ctx context.Context, bm gopay.BodyMap) (ret string, err error) { err = bm.CheckEmptyError("personal_product_code") if err != nil { - return "", err + return util.NULL, err } var bs []byte if bs, err = a.doAliPay(ctx, bm, "alipay.user.agreement.page.sign"); err != nil { @@ -120,12 +117,11 @@ func (a *Client) UserAgreementPageUnSign(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(UserAgreementPageUnSignRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(*aliRsp.Response); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -140,12 +136,11 @@ func (a *Client) UserAgreementQuery(ctx context.Context, bm gopay.BodyMap) (aliR return nil, err } aliRsp = new(UserAgreementQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -164,12 +159,11 @@ func (a *Client) UserAgreementExecutionplanModify(ctx context.Context, bm gopay. return nil, err } aliRsp = new(UserAgreementExecutionplanModifyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -188,12 +182,11 @@ func (a *Client) UserAgreementTransfer(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(UserAgreementTransferRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -212,12 +205,11 @@ func (a *Client) UserTwostageCommonUse(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(UserTwostageCommonUseRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -236,12 +228,11 @@ func (a *Client) UserAuthZhimaorgIdentityApply(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(UserAuthZhimaorgIdentityApplyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -260,12 +251,11 @@ func (a *Client) UserCharityRecordexistQuery(ctx context.Context, bm gopay.BodyM return nil, err } aliRsp = new(UserCharityRecordexistQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -284,12 +274,11 @@ func (a *Client) UserAlipaypointSend(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(UserAlipaypointSendRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -308,12 +297,11 @@ func (a *Client) MemberDataIsvCreate(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(MemberDataIsvCreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -332,12 +320,11 @@ func (a *Client) UserFamilyArchiveQuery(ctx context.Context, bm gopay.BodyMap) ( return nil, err } aliRsp = new(UserFamilyArchiveQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -356,12 +343,11 @@ func (a *Client) UserFamilyArchiveInitialize(ctx context.Context, bm gopay.BodyM return nil, err } aliRsp = new(UserFamilyArchiveInitializeRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -380,12 +366,11 @@ func (a *Client) UserCertdocCertverifyPreconsult(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(UserCertdocCertverifyPreconsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -400,12 +385,11 @@ func (a *Client) UserCertdocCertverifyConsult(ctx context.Context, bm gopay.Body return nil, err } aliRsp = new(UserCertdocCertverifyConsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -424,12 +408,11 @@ func (a *Client) UserFamilyShareZmgoInitialize(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(UserFamilyShareZmgoInitializeRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -448,12 +431,11 @@ func (a *Client) UserDtbankQrcodedataQuery(ctx context.Context, bm gopay.BodyMap return nil, err } aliRsp = new(UserDtbankQrcodedataQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -472,12 +454,11 @@ func (a *Client) UserAlipaypointBudgetlibQuery(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(UserAlipaypointBudgetlibQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/member_api_test.go b/alipay/member_api_test.go index e355794b..d328259f 100644 --- a/alipay/member_api_test.go +++ b/alipay/member_api_test.go @@ -28,7 +28,11 @@ func TestClient_UserCertifyOpenInit(t *testing.T) { // 发起请求 aliRsp, err := client.UserCertifyOpenInit(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -43,7 +47,11 @@ func TestClient_UserCertifyOpenCertify(t *testing.T) { // 发起请求 certifyUrl, err := client.UserCertifyOpenCertify(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("certifyUrl:", certifyUrl) @@ -58,7 +66,11 @@ func TestClient_UserCertifyOpenQuery(t *testing.T) { // 发起请求 aliRsp, err := client.UserCertifyOpenQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -75,7 +87,11 @@ func TestUserAgreementExecutionplanModify(t *testing.T) { // 发起请求 aliRsp, err := client.UserAgreementExecutionplanModify(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -96,7 +112,11 @@ func TestUserAgreementTransfer(t *testing.T) { // 发起请求 aliRsp, err := client.UserAgreementTransfer(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -112,7 +132,11 @@ func TestUserTwostageCommonUse(t *testing.T) { // 发起请求 aliRsp, err := client.UserTwostageCommonUse(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -128,7 +152,11 @@ func TestUserAuthZhimaorgIdentityApply(t *testing.T) { // 发起请求 aliRsp, err := client.UserAuthZhimaorgIdentityApply(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -143,7 +171,11 @@ func TestUserCharityRecordexistQuery(t *testing.T) { // 发起请求 aliRsp, err := client.UserCharityRecordexistQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -159,7 +191,11 @@ func TestUserAlipaypointSend(t *testing.T) { // 发起请求 aliRsp, err := client.UserAlipaypointSend(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -177,7 +213,11 @@ func TestMemberDataIsvCreate(t *testing.T) { // 发起请求 aliRsp, err := client.MemberDataIsvCreate(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -191,7 +231,11 @@ func TestUserFamilyArchiveQuery(t *testing.T) { // 发起请求 aliRsp, err := client.UserFamilyArchiveQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -207,7 +251,11 @@ func TestUserFamilyArchiveInitialize(t *testing.T) { // 发起请求 aliRsp, err := client.UserFamilyArchiveInitialize(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -223,7 +271,11 @@ func TestUserCertdocCertverifyPreconsult(t *testing.T) { // 发起请求 aliRsp, err := client.UserCertdocCertverifyPreconsult(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -236,7 +288,11 @@ func TestUserCertdocCertverifyConsult(t *testing.T) { // 发起请求 aliRsp, err := client.UserCertdocCertverifyConsult(ctx, bm, "auth_token") if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -252,7 +308,11 @@ func TestUserFamilyShareZmgoInitialize(t *testing.T) { // 发起请求 aliRsp, err := client.UserFamilyShareZmgoInitialize(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -267,7 +327,11 @@ func TestUserDtbankQrcodedataQuery(t *testing.T) { // 发起请求 aliRsp, err := client.UserDtbankQrcodedataQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -280,7 +344,11 @@ func TestUserAlipaypointBudgetlibQuery(t *testing.T) { // 发起请求 aliRsp, err := client.UserAlipaypointBudgetlibQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) diff --git a/alipay/payment_api.go b/alipay/payment_api.go index 1f00bc56..3bd8d737 100644 --- a/alipay/payment_api.go +++ b/alipay/payment_api.go @@ -22,12 +22,11 @@ func (a *Client) TradePay(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeP return nil, err } aliRsp = new(TradePayResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -46,12 +45,11 @@ func (a *Client) TradePrecreate(ctx context.Context, bm gopay.BodyMap) (aliRsp * return nil, err } aliRsp = new(TradePrecreateResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } if aliRsp.NullResponse != nil { info := aliRsp.NullResponse @@ -121,11 +119,11 @@ func (a *Client) TradeCreate(ctx context.Context, bm gopay.BodyMap) (aliRsp *Tra return nil, err } aliRsp = new(TradeCreateResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - if bizErr := bizErrCheck(aliRsp.Response.ErrorResponse); bizErr != nil { - return aliRsp, bizErr + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -143,8 +141,11 @@ func (a *Client) TradeQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *Trad return nil, err } aliRsp = new(TradeQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) + } + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -162,12 +163,11 @@ func (a *Client) TradeCancel(ctx context.Context, bm gopay.BodyMap) (aliRsp *Tra return nil, err } aliRsp = new(TradeCancelResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -185,12 +185,11 @@ func (a *Client) TradeClose(ctx context.Context, bm gopay.BodyMap) (aliRsp *Trad return nil, err } aliRsp = new(TradeCloseResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -212,12 +211,11 @@ func (a *Client) TradeRefund(ctx context.Context, bm gopay.BodyMap) (aliRsp *Tra return nil, err } aliRsp = new(TradeRefundResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -239,12 +237,11 @@ func (a *Client) TradePageRefund(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(TradePageRefundResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -266,12 +263,11 @@ func (a *Client) TradeFastPayRefundQuery(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(TradeFastpayRefundQueryResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -290,12 +286,11 @@ func (a *Client) TradeOrderSettle(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(TradeOrderSettleResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -314,12 +309,11 @@ func (a *Client) TradeOrderInfoSync(ctx context.Context, bm gopay.BodyMap) (aliR return nil, err } aliRsp = new(TradeOrderInfoSyncRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -334,12 +328,11 @@ func (a *Client) TradeAdvanceConsult(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(TradeAdvanceConsultRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -358,12 +351,11 @@ func (a *Client) PcreditHuabeiAuthSettleApply(ctx context.Context, bm gopay.Body return nil, err } aliRsp = new(PcreditHuabeiAuthSettleApplyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -382,12 +374,11 @@ func (a *Client) CommerceTransportNfccardSend(ctx context.Context, bm gopay.Body return nil, err } aliRsp = new(CommerceTransportNfccardSendRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -406,12 +397,11 @@ func (a *Client) DataDataserviceAdDataQuery(ctx context.Context, bm gopay.BodyMa return nil, err } aliRsp = new(DataDataserviceAdDataQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -430,12 +420,11 @@ func (a *Client) CommerceAirCallcenterTradeApply(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(CommerceAirCallcenterTradeApplyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -454,12 +443,11 @@ func (a *Client) PaymentTradeOrderCreate(ctx context.Context, bm gopay.BodyMap) return nil, err } aliRsp = new(PaymentTradeOrderCreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -478,12 +466,11 @@ func (a *Client) CommerceBenefitApply(ctx context.Context, bm gopay.BodyMap) (al return nil, err } aliRsp = new(CommerceBenefitApplyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -502,12 +489,11 @@ func (a *Client) CommerceBenefitVerify(ctx context.Context, bm gopay.BodyMap) (a return nil, err } aliRsp = new(CommerceBenefitVerifyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -522,12 +508,11 @@ func (a *Client) TradeRepaybillQuery(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(TradeRepaybillQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/payment_api_test.go b/alipay/payment_api_test.go index e85683bc..e7e3a3ee 100644 --- a/alipay/payment_api_test.go +++ b/alipay/payment_api_test.go @@ -19,7 +19,11 @@ func TestClient_TradePrecreate(t *testing.T) { // 创建订单 aliRsp, err := client.TradePrecreate(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debugf("aliRsp:%+v", aliRsp.Response) @@ -44,16 +48,15 @@ func TestClient_TradeCreate(t *testing.T) { // 创建订单 aliRsp, err := client.TradeCreate(ctx, bm) - bizErr := AsBizError(err) - if err != nil && bizErr == nil { - // 这种情况是非业务逻辑错误 - xlog.Error(err) + if err != nil { + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%s, %s", bizErr.Code, bizErr.Msg) + // do something + return + } + xlog.Errorf("%s", err) return } - if bizErr != nil { - // 在这里处理业务逻辑 - xlog.Infof("biz error: code: %v, msg: %v", bizErr.Code, bizErr.Msg) - } xlog.Debug("aliRsp:", *aliRsp) xlog.Debug("aliRsp.TradeNo:", aliRsp.Response.TradeNo) } @@ -68,7 +71,11 @@ func TestClient_TradeAppPay(t *testing.T) { // 手机APP支付参数请求 payParam, err := client.TradeAppPay(ctx, bm) if err != nil { - xlog.Errorf("client.TradeAppPay(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("payParam:", payParam) @@ -82,7 +89,11 @@ func TestClient_TradeCancel(t *testing.T) { // 撤销支付订单 aliRsp, err := client.TradeCancel(ctx, bm) if err != nil { - xlog.Errorf("client.TradeCancel(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -96,7 +107,11 @@ func TestClient_TradeClose(t *testing.T) { // 条码支付 aliRsp, err := client.TradeClose(ctx, bm) if err != nil { - xlog.Errorf("client.TradeClose(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -115,7 +130,11 @@ func TestClient_TradePay(t *testing.T) { // 条码支付 aliRsp, err := client.TradePay(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -137,7 +156,11 @@ func TestClient_TradeQuery(t *testing.T) { // 查询订单 aliRsp, err := client.TradeQuery(ctx, bm) if err != nil { - xlog.Error(err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debugf("aliRsp:%+v", aliRsp.Response) @@ -195,7 +218,11 @@ func TestClient_TradeRefund(t *testing.T) { // 发起退款请求 aliRsp, err := client.TradeRefund(ctx, bm) if err != nil { - xlog.Errorf("client.TradeRefund(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -211,7 +238,11 @@ func TestClient_TradePageRefund(t *testing.T) { // 发起退款请求 aliRsp, err := client.TradePageRefund(ctx, bm) if err != nil { - xlog.Errorf("client.TradePageRefund(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -226,7 +257,11 @@ func TestClient_TradeFastPayRefundQuery(t *testing.T) { // 发起退款查询请求 aliRsp, err := client.TradeFastPayRefundQuery(ctx, bm) if err != nil { - xlog.Errorf("client.TradeFastPayRefundQuery(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -247,7 +282,11 @@ func TestClient_TradeOrderSettle(t *testing.T) { // 发起交易结算接口 aliRsp, err := client.TradeOrderSettle(ctx, bm) if err != nil { - xlog.Errorf("client.TradeOrderSettle(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -262,7 +301,11 @@ func TestClient_TradeAdvanceConsult(t *testing.T) { aliRsp, err := client.TradeAdvanceConsult(ctx, bm) if err != nil { - xlog.Errorf("client.TradeAdvanceConsult(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -278,7 +321,11 @@ func TestClient_PcreditHuabeiAuthSettleApply(t *testing.T) { aliRsp, err := client.PcreditHuabeiAuthSettleApply(ctx, bm) if err != nil { - xlog.Errorf("client.PcreditHuabeiAuthSettleApply(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -293,7 +340,11 @@ func TestClient_CommerceTransportNfccardSend(t *testing.T) { aliRsp, err := client.CommerceTransportNfccardSend(ctx, bm) if err != nil { - xlog.Errorf("client.CommerceTransportNfccardSend(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -311,7 +362,11 @@ func TestClient_DataDataserviceAdDataQuery(t *testing.T) { aliRsp, err := client.DataDataserviceAdDataQuery(ctx, bm) if err != nil { - xlog.Errorf("client.DataDataserviceAdDataQuery(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -337,7 +392,11 @@ func TestClient_CommerceAirCallcenterTradeApply(t *testing.T) { aliRsp, err := client.CommerceAirCallcenterTradeApply(ctx, bm) if err != nil { - xlog.Errorf("client.CommerceAirCallcenterTradeApply(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -363,6 +422,11 @@ func TestClient_PaymentTradeOrderCreate(t *testing.T) { aliRsp, err := client.PaymentTradeOrderCreate(ctx, bm) if err != nil { + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } xlog.Errorf("client.PaymentTradeOrderCreate(%+v),error:%+v", bm, err) return } @@ -379,6 +443,11 @@ func TestClient_CommerceBenefitApply(t *testing.T) { aliRsp, err := client.CommerceBenefitApply(ctx, bm) if err != nil { + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } xlog.Errorf("client.CommerceBenefitApply(%+v),error:%+v", bm, err) return } @@ -395,6 +464,11 @@ func TestClient_CommerceBenefitVerify(t *testing.T) { aliRsp, err := client.CommerceBenefitVerify(ctx, bm) if err != nil { + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } xlog.Errorf("client.CommerceBenefitVerify(%+v),error:%+v", bm, err) return } @@ -406,7 +480,11 @@ func TestClient_TradeRepaybillQuery(t *testing.T) { bm := make(gopay.BodyMap) aliRsp, err := client.TradeRepaybillQuery(ctx, bm) if err != nil { - xlog.Errorf("client.TradeRepaybillQuery(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) diff --git a/alipay/sign.go b/alipay/sign.go index e3688fd2..d95b9469 100644 --- a/alipay/sign.go +++ b/alipay/sign.go @@ -166,7 +166,7 @@ func GetRsaSign(bm gopay.BodyMap, signType string, privateKey *rsa.PrivateKey) ( return } if encryptedBytes, err = rsa.SignPKCS1v15(rand.Reader, privateKey, hashs, h.Sum(nil)); err != nil { - return + return util.NULL, fmt.Errorf("[%w]: %+v", gopay.SignatureErr, err) } sign = base64.StdEncoding.EncodeToString(encryptedBytes) return @@ -187,14 +187,14 @@ func (a *Client) getSignData(bs []byte, alipayCertSN string) (signData string, e if alipayCertSN != "" { // 公钥证书模式 if alipayCertSN != a.AliPayPublicCertSN { - return gopay.NULL, fmt.Errorf("当前使用的支付宝公钥证书SN[%s]与网关响应报文中的SN[%s]不匹配", a.AliPayPublicCertSN, alipayCertSN) + return gopay.NULL, fmt.Errorf("[%w], 当前使用的支付宝公钥证书SN[%s]与网关响应报文中的SN[%s]不匹配", gopay.CertNotMatchErr, a.AliPayPublicCertSN, alipayCertSN) } indexEnd = strings.Index(str, `,"alipay_cert_sn":`) if indexEnd > indexStart && bsLen > indexStart { signData = str[indexStart:indexEnd] return } - return gopay.NULL, fmt.Errorf("[%s] parse error", str) + return gopay.NULL, fmt.Errorf("[%w], value: %s", gopay.GetSignDataErr, str) } // 普通公钥模式 indexEnd = strings.Index(str, `,"sign":`) @@ -202,7 +202,7 @@ func (a *Client) getSignData(bs []byte, alipayCertSN string) (signData string, e signData = str[indexStart:indexEnd] return } - return gopay.NULL, fmt.Errorf("[%s] parse error", str) + return gopay.NULL, fmt.Errorf("[%w], value: %s", gopay.GetSignDataErr, str) } // =============================== 同步验签 =============================== @@ -262,7 +262,9 @@ func (a *Client) autoVerifySignByCert(sign, signData string, signDataErr error) hashs := crypto.SHA256 h := hashs.New() h.Write([]byte(signData)) - return rsa.VerifyPKCS1v15(a.aliPayPublicKey, hashs, h.Sum(nil), signBytes) + if err = rsa.VerifyPKCS1v15(a.aliPayPublicKey, hashs, h.Sum(nil), signBytes); err != nil { + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) + } } return nil } @@ -392,7 +394,10 @@ func verifySign(signData, sign, signType, alipayPublicKey string) (err error) { } h = hashs.New() h.Write([]byte(signData)) - return rsa.VerifyPKCS1v15(publicKey, hashs, h.Sum(nil), signBytes) + if err = rsa.VerifyPKCS1v15(publicKey, hashs, h.Sum(nil), signBytes); err != nil { + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) + } + return nil } func verifySignCert(signData, sign, signType string, alipayPublicKeyCert interface{}) (err error) { @@ -427,5 +432,8 @@ func verifySignCert(signData, sign, signType string, alipayPublicKeyCert interfa } h = hashs.New() h.Write([]byte(signData)) - return rsa.VerifyPKCS1v15(publicKey, hashs, h.Sum(nil), signBytes) + if err = rsa.VerifyPKCS1v15(publicKey, hashs, h.Sum(nil), signBytes); err != nil { + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) + } + return nil } diff --git a/alipay/util_api.go b/alipay/util_api.go index 018dcc83..8393838f 100644 --- a/alipay/util_api.go +++ b/alipay/util_api.go @@ -27,12 +27,11 @@ func (a *Client) UserInfoAuth(ctx context.Context, bm gopay.BodyMap) (aliRsp *Us return nil, errors.New(string(bs)) } aliRsp = new(UserInfoAuthResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(*aliRsp.Response); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -63,7 +62,7 @@ func (a *Client) SystemOauthToken(ctx context.Context, bm gopay.BodyMap) (aliRsp } aliRsp = new(SystemOauthTokenResponse) if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } if aliRsp.ErrorResponse != nil { info := aliRsp.ErrorResponse @@ -89,12 +88,11 @@ func (a *Client) OpenAuthTokenApp(ctx context.Context, bm gopay.BodyMap) (aliRsp return nil, err } aliRsp = new(OpenAuthTokenAppResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -113,12 +111,11 @@ func (a *Client) PublicCertDownload(ctx context.Context, bm gopay.BodyMap) (aliR return nil, err } aliRsp = new(PublicCertDownloadRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } certBs, err := base64.StdEncoding.DecodeString(aliRsp.Response.AlipayCertContent) if err != nil { diff --git a/alipay/util_api_test.go b/alipay/util_api_test.go index 6efa93a6..f6dbcda8 100644 --- a/alipay/util_api_test.go +++ b/alipay/util_api_test.go @@ -17,7 +17,11 @@ func TestClient_SystemOauthToken(t *testing.T) { // 发起请求 aliRsp, err := client.SystemOauthToken(ctx, bm) if err != nil { - xlog.Errorf("client.SystemOauthToken(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -34,7 +38,11 @@ func TestClient_OpenAuthTokenApp(t *testing.T) { // 发起请求 aliRsp, err := client.OpenAuthTokenApp(ctx, bm) if err != nil { - xlog.Errorf("client.OpenAuthTokenApp(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -50,7 +58,11 @@ func TestClient_UserInfoAuth(t *testing.T) { // 发起请求 aliRsp, err := client.UserInfoAuth(ctx, bm) if err != nil { - xlog.Errorf("client.UserInfoAuth(%+v),error:%+v", bm, err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -60,7 +72,11 @@ func TestClient_UserInfoShare(t *testing.T) { // 发起请求 aliRsp, err := client.UserInfoShare(ctx, "auth_token") if err != nil { - xlog.Errorf("client.UserInfoShare(),error:%+v", err) + if bizErr, ok := IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } return } xlog.Debug("aliRsp:", *aliRsp) @@ -81,7 +97,7 @@ func TestClient_PublicCertDownload(t *testing.T) { // 发起请求 aliRsp, err := client.PublicCertDownload(ctx, bm) if err != nil { - xlog.Errorf("client.UserInfoShare(),error:%+v", err) + //xlog.Errorf("client.UserInfoShare(),error:%+v", err) return } xlog.Debugf("aliRsp.Response.AlipayCertContent:\n %s", aliRsp.Response.AlipayCertContent) diff --git a/alipay/zhima_api.go b/alipay/zhima_api.go index ea546da4..9882354c 100644 --- a/alipay/zhima_api.go +++ b/alipay/zhima_api.go @@ -25,12 +25,11 @@ func (a *Client) ZhimaCreditScoreGet(ctx context.Context, bm gopay.BodyMap) (ali return nil, err } aliRsp = new(ZhimaCreditScoreGetResponse) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -52,12 +51,11 @@ func (a *Client) ZhimaCreditEpSceneRatingInitialize(ctx context.Context, bm gopa return nil, err } aliRsp = new(ZhimaCreditEpSceneRatingInitializeRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -76,12 +74,11 @@ func (a *Client) ZhimaCreditEpSceneFulfillmentSync(ctx context.Context, bm gopay return nil, err } aliRsp = new(ZhimaCreditEpSceneFulfillmentSyncRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -100,12 +97,11 @@ func (a *Client) ZhimaCreditEpSceneAgreementUse(ctx context.Context, bm gopay.Bo return nil, err } aliRsp = new(ZhimaCreditEpSceneAgreementUseRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -124,12 +120,11 @@ func (a *Client) ZhimaCreditEpSceneAgreementCancel(ctx context.Context, bm gopay return nil, err } aliRsp = new(ZhimaCreditEpSceneAgreementCancelRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -148,12 +143,11 @@ func (a *Client) ZhimaCreditEpSceneFulfillmentlistSync(ctx context.Context, bm g return nil, err } aliRsp = new(ZhimaCreditEpSceneFulfillmentlistSyncRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -172,12 +166,11 @@ func (a *Client) ZhimaCreditPeZmgoCumulationSync(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(ZhimaCreditPeZmgoCumulationSyncRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -196,12 +189,11 @@ func (a *Client) ZhimaMerchantZmgoCumulateSync(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(ZhimaMerchantZmgoCumulateSyncRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -220,12 +212,11 @@ func (a *Client) ZhimaMerchantZmgoCumulateQuery(ctx context.Context, bm gopay.Bo return nil, err } aliRsp = new(ZhimaMerchantZmgoCumulateQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -244,12 +235,11 @@ func (a *Client) ZhimaCreditPeZmgoBizoptClose(ctx context.Context, bm gopay.Body return nil, err } aliRsp = new(ZhimaCreditPeZmgoBizoptCloseRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -268,12 +258,11 @@ func (a *Client) ZhimaCreditPeZmgoSettleRefund(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(ZhimaCreditPeZmgoSettleRefundRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -292,12 +281,11 @@ func (a *Client) ZhimaCreditPeZmgoPreorderCreate(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(ZhimaCreditPeZmgoPreorderCreateRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -316,12 +304,11 @@ func (a *Client) ZhimaCreditPeZmgoAgreementUnsign(ctx context.Context, bm gopay. return nil, err } aliRsp = new(ZhimaCreditPeZmgoAgreementUnsignRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -340,12 +327,11 @@ func (a *Client) ZhimaCreditPeZmgoAgreementQuery(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(ZhimaCreditPeZmgoAgreementQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -364,12 +350,11 @@ func (a *Client) ZhimaCreditPeZmgoSettleUnfreeze(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(ZhimaCreditPeZmgoSettleUnfreezeRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -388,12 +373,11 @@ func (a *Client) ZhimaCreditPeZmgoPaysignApply(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(ZhimaCreditPeZmgoPaysignApplyRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -412,12 +396,11 @@ func (a *Client) ZhimaCreditPeZmgoPaysignConfirm(ctx context.Context, bm gopay.B return nil, err } aliRsp = new(ZhimaCreditPeZmgoPaysignConfirmRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -432,12 +415,11 @@ func (a *Client) ZhimaCustomerJobworthAdapterQuery(ctx context.Context, bm gopay return nil, err } aliRsp = new(ZhimaCustomerJobworthAdapterQueryRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData @@ -452,12 +434,11 @@ func (a *Client) ZhimaCustomerJobworthSceneUse(ctx context.Context, bm gopay.Bod return nil, err } aliRsp = new(ZhimaCustomerJobworthSceneUseRsp) - if err = json.Unmarshal(bs, aliRsp); err != nil { - return nil, err + if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil { + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) } - 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) + if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil { + return aliRsp, err } signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn) aliRsp.SignData = signData diff --git a/alipay/zhima_api_test.go b/alipay/zhima_api_test.go index fbf63cde..55873f9f 100644 --- a/alipay/zhima_api_test.go +++ b/alipay/zhima_api_test.go @@ -1,6 +1,7 @@ package alipay import ( + "errors" "testing" "github.com/go-pay/gopay" @@ -19,7 +20,7 @@ func TestZhimaCreditEpSceneRatingInitialize(t *testing.T) { aliRsp, err := client.ZhimaCreditEpSceneRatingInitialize(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditEpSceneRatingInitialize(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditEpSceneRatingInitialize(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -36,7 +37,7 @@ func TestZhimaCreditEpSceneFulfillmentSync(t *testing.T) { aliRsp, err := client.ZhimaCreditEpSceneFulfillmentSync(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditEpSceneFulfillmentSync(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditEpSceneFulfillmentSync(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -54,7 +55,7 @@ func TestZhimaCreditEpSceneAgreementUse(t *testing.T) { aliRsp, err := client.ZhimaCreditEpSceneAgreementUse(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditEpSceneAgreementUse(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditEpSceneAgreementUse(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -70,7 +71,7 @@ func TestZhimaCreditEpSceneAgreementCancel(t *testing.T) { aliRsp, err := client.ZhimaCreditEpSceneAgreementCancel(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditEpSceneAgreementCancel(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditEpSceneAgreementCancel(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -88,7 +89,7 @@ func TestZhimaCreditEpSceneFulfillmentlistSync(t *testing.T) { }) aliRsp, err := client.ZhimaCreditEpSceneFulfillmentlistSync(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditEpSceneFulfillmentlistSync(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditEpSceneFulfillmentlistSync(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -127,7 +128,7 @@ func TestZhimaCreditPeZmgoCumulationSync(t *testing.T) { }) aliRsp, err := client.ZhimaCreditPeZmgoCumulationSync(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoCumulationSync(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoCumulationSync(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -170,7 +171,7 @@ func TestZhimaMerchantZmgoCumulateSync(t *testing.T) { aliRsp, err := client.ZhimaMerchantZmgoCumulateSync(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaMerchantZmgoCumulateSync(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaMerchantZmgoCumulateSync(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -191,7 +192,7 @@ func TestZhimaMerchantZmgoCumulateQuery(t *testing.T) { aliRsp, err := client.ZhimaMerchantZmgoCumulateQuery(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaMerchantZmgoCumulateQuery(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaMerchantZmgoCumulateQuery(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -209,7 +210,7 @@ func TestZhimaCreditPeZmgoBizoptClose(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoBizoptClose(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoBizoptClose(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoBizoptClose(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -232,7 +233,7 @@ func TestZhimaCreditPeZmgoSettleRefund(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoSettleRefund(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoSettleRefund(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoSettleRefund(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -263,7 +264,7 @@ func TestZhimaCreditPeZmgoPreorderCreate(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoPreorderCreate(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoPreorderCreate(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoPreorderCreate(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -282,7 +283,7 @@ func TestZhimaCreditPeZmgoAgreementUnsign(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoAgreementUnsign(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoAgreementUnsign(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoAgreementUnsign(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -298,7 +299,7 @@ func TestZhimaCreditPeZmgoAgreementQuery(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoAgreementQuery(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoAgreementQuery(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoAgreementQuery(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -326,7 +327,7 @@ func TestZhimaCreditPeZmgoSettleUnfreeze(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoSettleUnfreeze(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoSettleUnfreeze(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoSettleUnfreeze(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -348,7 +349,7 @@ func TestZhimaCreditPeZmgoPaysignApply(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoPaysignApply(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoPaysignApply(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoPaysignApply(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -368,7 +369,7 @@ func TestZhimaCreditPeZmgoPaysignConfirm(t *testing.T) { aliRsp, err := client.ZhimaCreditPeZmgoPaysignConfirm(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCreditPeZmgoPaysignConfirm(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCreditPeZmgoPaysignConfirm(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -396,7 +397,7 @@ func TestZhimaCustomerJobworthAdapterQuery(t *testing.T) { aliRsp, err := client.ZhimaCustomerJobworthAdapterQuery(ctx, bm) if err != nil { - xlog.Errorf("client.ZhimaCustomerJobworthAdapterQuery(%+v),error:%+v", bm, err) + //xlog.Errorf("client.ZhimaCustomerJobworthAdapterQuery(%+v),error:%+v", bm, err) return } xlog.Debug("aliRsp:", *aliRsp) @@ -426,6 +427,10 @@ func TestZhimaCustomerJobworthSceneUse(t *testing.T) { aliRsp, err := client.ZhimaCustomerJobworthSceneUse(ctx, bm) if err != nil { + if errors.Is(err, gopay.UnmarshalErr) { + //xlog.Errorf("%v", err) + return + } xlog.Errorf("client.ZhimaCustomerJobworthSceneUse(%+v),error:%+v", bm, err) return } diff --git a/body_map.go b/body_map.go index 25d29b93..c70edcc4 100644 --- a/body_map.go +++ b/body_map.go @@ -3,7 +3,7 @@ package gopay import ( "encoding/json" "encoding/xml" - "errors" + "fmt" "io" "net/url" "sort" @@ -221,7 +221,7 @@ func (bm BodyMap) CheckEmptyError(keys ...string) error { } } if len(emptyKeys) > 0 { - return errors.New(strings.Join(emptyKeys, ", ") + " : cannot be empty") + return fmt.Errorf("[%w], %v", MissParamErr, strings.Join(emptyKeys, ", ")) } return nil } diff --git a/constant.go b/constant.go index a27f812d..da7398ca 100644 --- a/constant.go +++ b/constant.go @@ -7,7 +7,7 @@ const ( OK = "OK" DebugOff = 0 DebugOn = 1 - Version = "1.5.75" + Version = "1.5.76" ) type DebugSwitch int8 diff --git a/doc/alipay.md b/doc/alipay.md index c997678a..d18360f6 100644 --- a/doc/alipay.md +++ b/doc/alipay.md @@ -25,7 +25,7 @@ import ( ) // 初始化支付宝客户端 -// appId:应用ID +// appid:应用ID // privateKey:应用私钥,支持PKCS1和PKCS8 // isProd:是否是正式环境 client, err := alipay.NewClient("2016091200494382", privateKey, false) @@ -60,7 +60,11 @@ err := client.SetCertSnByContent("appCertPublicKey bytes", "alipayRootCert bytes > 具体参数请根据不同接口查看:[支付宝支付API接口文档](https://opendocs.alipay.com/apis) -- 统一收单交易支付接口 示例 +> 业务错误处理:当 `err != nil` 时,可通过 `alipay.IsBizError()` 捕获业务错误状态码和说明。 +> 不在乎 `BizError` 的可忽略统一判错处理 + +- 统一收单交易支付接口 - 示例 + ```go import ( "github.com/go-pay/gopay" @@ -77,8 +81,13 @@ bm.Set("subject", "条码支付"). aliRsp, err := client.TradePay(bm) if err != nil { - xlog.Error("err:", err) - return + if bizErr, ok := alipay.IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } + xlog.Errorf("client.TradePay(%+v),err:%+v", bm, err) + return } ``` @@ -139,8 +148,8 @@ if err != nil { // value:url.Values notifyReq, err = alipay.ParseNotifyByURLValues() if err != nil { -xlog.Error(err) -return + xlog.Error(err) + return } // 支付宝异步通知验签(公钥模式) @@ -321,6 +330,7 @@ xlog.Infof("%+v", phone) ### 支付宝公共 API +* `alipay.IsBizError()` => 判断并捕获业务错误 BizError * `alipay.GetCertSN()` => 获取证书SN号(app_cert_sn、alipay_cert_sn) * `alipay.GetRootCertSN()` => 获取证书SN号(alipay_root_cert_sn) * `alipay.GetRsaSign()` => 获取支付宝参数签名(参数sign值) diff --git a/error.go b/error.go new file mode 100644 index 00000000..67712b21 --- /dev/null +++ b/error.go @@ -0,0 +1,16 @@ +package gopay + +import "errors" + +var ( + MissWechatInitParamErr = errors.New("missing wechat init parameter") + MissAlipayInitParamErr = errors.New("missing alipay init parameter") + MissPayPalInitParamErr = errors.New("missing paypal init parameter") + MissParamErr = errors.New("missing required parameter") + MarshalErr = errors.New("marshal error") + UnmarshalErr = errors.New("unmarshal error") + SignatureErr = errors.New("signature error") + VerifySignatureErr = errors.New("verify signature error") + CertNotMatchErr = errors.New("cert not match error") + GetSignDataErr = errors.New("get signature data error") +) diff --git a/go.mod b/go.mod index 28c52029..d47b3ebe 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/go-pay/gopay go 1.16 -require golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd +require golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 diff --git a/go.sum b/go.sum index 85890862..9f866ee2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38= -golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o= +golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/paypal/client.go b/paypal/client.go index 414b3a26..380acd67 100644 --- a/paypal/client.go +++ b/paypal/client.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/go-pay/gopay" + "github.com/go-pay/gopay/pkg/util" "github.com/go-pay/gopay/pkg/xhttp" "github.com/go-pay/gopay/pkg/xlog" ) @@ -24,6 +25,9 @@ type Client struct { // NewClient 初始化PayPal支付客户端 func NewClient(clientid, secret string, isProd bool) (client *Client, err error) { + if clientid == util.NULL || secret == util.NULL { + return nil, gopay.MissPayPalInitParamErr + } client = &Client{ Clientid: clientid, Secret: secret, diff --git a/paypal/order.go b/paypal/order.go index 505ee946..33c171e8 100644 --- a/paypal/order.go +++ b/paypal/order.go @@ -24,7 +24,7 @@ func (c *Client) CreateOrder(ctx context.Context, bm gopay.BodyMap) (ppRsp *Crea ppRsp = &CreateOrderRsp{Code: Success} ppRsp.Response = new(OrderDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated { ppRsp.Code = res.StatusCode @@ -72,7 +72,7 @@ func (c *Client) OrderDetail(ctx context.Context, orderId string, bm gopay.BodyM ppRsp = &OrderDetailRsp{Code: Success} ppRsp.Response = new(OrderDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode @@ -98,7 +98,7 @@ func (c *Client) OrderAuthorize(ctx context.Context, orderId string, bm gopay.Bo ppRsp = &OrderAuthorizeRsp{Code: Success} ppRsp.Response = new(OrderDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode @@ -124,7 +124,7 @@ func (c *Client) OrderCapture(ctx context.Context, orderId string, bm gopay.Body ppRsp = &OrderCaptureRsp{Code: Success} ppRsp.Response = new(OrderDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode @@ -150,7 +150,7 @@ func (c *Client) OrderConfirm(ctx context.Context, orderId string, bm gopay.Body ppRsp = &OrderConfirmRsp{Code: Success} ppRsp.Response = new(OrderDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode diff --git a/paypal/payment.go b/paypal/payment.go index 2490a2d2..9173c8c7 100644 --- a/paypal/payment.go +++ b/paypal/payment.go @@ -25,7 +25,7 @@ func (c *Client) PaymentAuthorizeDetail(ctx context.Context, authorizationId str ppRsp = &PaymentAuthorizeDetailRsp{Code: Success} ppRsp.Response = new(PaymentAuthorizeDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode @@ -51,7 +51,7 @@ func (c *Client) PaymentReauthorize(ctx context.Context, authorizationId string, ppRsp = &PaymentReauthorizeRsp{Code: Success} ppRsp.Response = new(PaymentAuthorizeDetail) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated { ppRsp.Code = res.StatusCode @@ -99,7 +99,7 @@ func (c *Client) PaymentAuthorizeCapture(ctx context.Context, authorizationId st ppRsp = &PaymentAuthorizeCaptureRsp{Code: Success} ppRsp.Response = new(PaymentAuthorizeCapture) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated { ppRsp.Code = res.StatusCode @@ -125,7 +125,7 @@ func (c *Client) PaymentCaptureDetail(ctx context.Context, captureId string) (pp ppRsp = &PaymentCaptureDetailRsp{Code: Success} ppRsp.Response = new(PaymentAuthorizeCapture) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode @@ -151,7 +151,7 @@ func (c *Client) PaymentCaptureRefund(ctx context.Context, captureId string, bm ppRsp = &PaymentCaptureRefundRsp{Code: Success} ppRsp.Response = new(PaymentCaptureRefund) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusCreated { ppRsp.Code = res.StatusCode @@ -177,7 +177,7 @@ func (c *Client) PaymentRefundDetail(ctx context.Context, refundId string) (ppRs ppRsp = &PaymentRefundDetailRsp{Code: Success} ppRsp.Response = new(PaymentCaptureRefund) if err = json.Unmarshal(bs, ppRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { ppRsp.Code = res.StatusCode diff --git a/pkg/jwt/rsa.go b/pkg/jwt/rsa.go index e4caf1ca..c9b30cb3 100644 --- a/pkg/jwt/rsa.go +++ b/pkg/jwt/rsa.go @@ -4,6 +4,9 @@ import ( "crypto" "crypto/rand" "crypto/rsa" + "fmt" + + "github.com/go-pay/gopay" ) // Implements the RSA family of signing methods signing methods @@ -62,15 +65,18 @@ func (m *SigningMethodRSA) Verify(signingString, signature string, key interface return ErrInvalidKeyType } - // Create hasher + // Create h if !m.Hash.Available() { return ErrHashUnavailable } - hasher := m.Hash.New() - hasher.Write([]byte(signingString)) + h := m.Hash.New() + h.Write([]byte(signingString)) // Verify the signature - return rsa.VerifyPKCS1v15(rsaKey, m.Hash, hasher.Sum(nil), sig) + if err = rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, h.Sum(nil), sig); err != nil { + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) + } + return nil } // Implements the Sign method from SigningMethod @@ -93,9 +99,9 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, hasher.Write([]byte(signingString)) // Sign the string and return the encoded bytes - if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil { + sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)) + if err == nil { return EncodeSegment(sigBytes), nil - } else { - return "", err } + return "", err } diff --git a/pkg/xhttp/client.go b/pkg/xhttp/client.go index 64a1d858..5c33a463 100644 --- a/pkg/xhttp/client.go +++ b/pkg/xhttp/client.go @@ -17,6 +17,7 @@ import ( "strings" "time" + "github.com/go-pay/gopay" "github.com/go-pay/gopay/pkg/util" ) @@ -119,7 +120,7 @@ func (c *Client) SendStruct(v interface{}) (client *Client) { } bs, err := json.Marshal(v) if err != nil { - c.err = fmt.Errorf("json.Marshal(%+v):%w", v, err) + c.err = fmt.Errorf("[%w]: %v, value: %v", gopay.MarshalErr, err, v) return c } switch c.requestType { @@ -128,7 +129,7 @@ func (c *Client) SendStruct(v interface{}) (client *Client) { case TypeXML, TypeUrlencoded, TypeForm, TypeFormData: body := make(map[string]interface{}) if err = json.Unmarshal(bs, &body); err != nil { - c.err = fmt.Errorf("json.Unmarshal(%s, %+v):%w", string(bs), body, err) + c.err = fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) return c } c.FormString = FormatURLParam(body) @@ -144,7 +145,7 @@ func (c *Client) SendBodyMap(bm map[string]interface{}) (client *Client) { case TypeJSON: bs, err := json.Marshal(bm) if err != nil { - c.err = fmt.Errorf("json.Marshal(%+v):%w", bm, err) + c.err = fmt.Errorf("[%w]: %v, value: %v", gopay.MarshalErr, err, bm) return c } c.jsonByte = bs @@ -162,7 +163,7 @@ func (c *Client) SendMultipartBodyMap(bm map[string]interface{}) (client *Client case TypeJSON: bs, err := json.Marshal(bm) if err != nil { - c.err = fmt.Errorf("json.Marshal(%+v):%w", bm, err) + c.err = fmt.Errorf("[%w]: %v, value: %v", gopay.MarshalErr, err, bm) return c } c.jsonByte = bs @@ -198,13 +199,13 @@ func (c *Client) EndStruct(ctx context.Context, v interface{}) (res *http.Respon case string(TypeJSON): err = json.Unmarshal(bs, &v) if err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s, %+v):%w", string(bs), v, err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return res, nil case string(TypeXML): err = xml.Unmarshal(bs, &v) if err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s, %+v):%w", string(bs), v, err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return res, nil default: diff --git a/qq/client.go b/qq/client.go index 07567ea9..2ee8f001 100644 --- a/qq/client.go +++ b/qq/client.go @@ -57,7 +57,7 @@ func (q *Client) MicroPay(ctx context.Context, bm gopay.BodyMap) (qqRsp *MicroPa } qqRsp = new(MicroPayResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -75,7 +75,7 @@ func (q *Client) Reverse(ctx context.Context, bm gopay.BodyMap) (qqRsp *ReverseR } qqRsp = new(ReverseResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -93,7 +93,7 @@ func (q *Client) UnifiedOrder(ctx context.Context, bm gopay.BodyMap) (qqRsp *Uni } qqRsp = new(UnifiedOrderResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -114,7 +114,7 @@ func (q *Client) OrderQuery(ctx context.Context, bm gopay.BodyMap) (qqRsp *Order } qqRsp = new(OrderQueryResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -132,7 +132,7 @@ func (q *Client) CloseOrder(ctx context.Context, bm gopay.BodyMap) (qqRsp *Close } qqRsp = new(CloseOrderResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -161,7 +161,7 @@ func (q *Client) Refund(ctx context.Context, bm gopay.BodyMap, certFilePath, key } qqRsp = new(RefundResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -182,7 +182,7 @@ func (q *Client) RefundQuery(ctx context.Context, bm gopay.BodyMap) (qqRsp *Refu } qqRsp = new(RefundQueryResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } diff --git a/qq/oplatform_api.go b/qq/oplatform_api.go index 7659a3b0..0be0f5b8 100644 --- a/qq/oplatform_api.go +++ b/qq/oplatform_api.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/go-pay/gopay" "github.com/go-pay/gopay/pkg/xhttp" ) @@ -108,11 +109,12 @@ func GetOpenId(ctx context.Context, accessToken string, lang ...string) (openid if len(ListBefore) > 1 { ListAfter = strings.Split(ListBefore[1], MarkAfter) } - JoinStr := fmt.Sprintf("%v%v%v", MarkBefore, ListAfter[0], MarkAfter) - err = json.Unmarshal([]byte(JoinStr), openid) + joinStr := fmt.Sprintf("%v%v%v", MarkBefore, ListAfter[0], MarkAfter) + err = json.Unmarshal([]byte(joinStr), openid) if err != nil { - return nil, err + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, joinStr) } + return } diff --git a/qq/payment_api.go b/qq/payment_api.go index 1638a808..80851d9f 100644 --- a/qq/payment_api.go +++ b/qq/payment_api.go @@ -31,7 +31,7 @@ func ParseNotifyToBodyMap(req *http.Request) (bm gopay.BodyMap, err error) { } bm = make(gopay.BodyMap) if err = xml.Unmarshal(bs, &bm); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return } @@ -69,11 +69,11 @@ func VerifySign(apiKey, signType string, bean interface{}) (ok bool, err error) bs, err := json.Marshal(bean) if err != nil { - return false, fmt.Errorf("json.Marshal(%s):%w", string(bs), err) + return false, fmt.Errorf("[%w]: %v, value: %v", gopay.MarshalErr, err, bean) } bm := make(gopay.BodyMap) if err = json.Unmarshal(bs, &bm); err != nil { - return false, fmt.Errorf("json.Marshal(%s):%w", string(bs), err) + return false, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } bodySign := bm.GetString("sign") bm.Remove("sign") diff --git a/qq/red.go b/qq/red.go index 0934a8f7..6d867c80 100644 --- a/qq/red.go +++ b/qq/red.go @@ -36,7 +36,7 @@ func (q *Client) SendCashRed(ctx context.Context, bm gopay.BodyMap, certFilePath } qqRsp = new(SendCashRedResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } @@ -69,7 +69,7 @@ func (q *Client) QueryRedInfo(ctx context.Context, bm gopay.BodyMap) (qqRsp *Que } qqRsp = new(QueryRedInfoResponse) if err = xml.Unmarshal(bs, qqRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return qqRsp, nil } diff --git a/release_note.txt b/release_note.txt index 06726179..b97efb56 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1,3 +1,9 @@ +版本号:Release 1.5.76 +修改记录: + (1) gopay:大量优化error处理和返回,统一部分通用错误到 error.go 中 + (2) 支付宝:新增 alipay.IsBizError(),判断并捕获业务错误 + (3) 微信、支付宝:优化部分 error 返回格式以及透传,优化参数校验返回 + 版本号:Release 1.5.75 修改记录: (1) 微信V3:client.V3Apply4SubModifySettlement(),sub_mchid 问题处理 diff --git a/wechat/base_api.go b/wechat/base_api.go index ca915ef9..9615a07e 100644 --- a/wechat/base_api.go +++ b/wechat/base_api.go @@ -30,7 +30,7 @@ func (w *Client) UnifiedOrder(ctx context.Context, bm gopay.BodyMap) (wxRsp *Uni } wxRsp = new(UnifiedOrderResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -54,7 +54,7 @@ func (w *Client) Micropay(ctx context.Context, bm gopay.BodyMap) (wxRsp *Micropa } wxRsp = new(MicropayResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -107,7 +107,7 @@ func (w *Client) CloseOrder(ctx context.Context, bm gopay.BodyMap) (wxRsp *Close } wxRsp = new(CloseOrderResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -204,7 +204,7 @@ func (w *Client) Reverse(ctx context.Context, bm gopay.BodyMap) (wxRsp *ReverseR } wxRsp = new(ReverseResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/client.go b/wechat/client.go index e677f518..7bc26934 100644 --- a/wechat/client.go +++ b/wechat/client.go @@ -63,7 +63,7 @@ func (w *Client) AuthCodeToOpenId(ctx context.Context, bm gopay.BodyMap) (wxRsp } wxRsp = new(AuthCodeToOpenIdResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -140,7 +140,7 @@ func (w *Client) Report(ctx context.Context, bm gopay.BodyMap) (wxRsp *ReportRes } wxRsp = new(ReportResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/customs.go b/wechat/customs.go index 4c472429..cb650eb4 100644 --- a/wechat/customs.go +++ b/wechat/customs.go @@ -22,7 +22,7 @@ func (w *Client) CustomsDeclareOrder(ctx context.Context, bm gopay.BodyMap) (wxR } wxRsp = new(CustomsDeclareOrderResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -41,7 +41,7 @@ func (w *Client) CustomsDeclareQuery(ctx context.Context, bm gopay.BodyMap) (wxR } wxRsp = new(CustomsDeclareQueryResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -60,7 +60,7 @@ func (w *Client) CustomsReDeclareOrder(ctx context.Context, bm gopay.BodyMap) (w } wxRsp = new(CustomsReDeclareOrderResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/entrust.go b/wechat/entrust.go index 8eef7f6c..245215f6 100644 --- a/wechat/entrust.go +++ b/wechat/entrust.go @@ -21,7 +21,7 @@ func (w *Client) EntrustPublic(ctx context.Context, bm gopay.BodyMap) (wxRsp *En } wxRsp = new(EntrustPublicResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -39,7 +39,7 @@ func (w *Client) EntrustAppPre(ctx context.Context, bm gopay.BodyMap) (wxRsp *En } wxRsp = new(EntrustAppPreResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -57,7 +57,7 @@ func (w *Client) EntrustH5(ctx context.Context, bm gopay.BodyMap) (wxRsp *Entrus } wxRsp = new(EntrustH5Response) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -78,7 +78,7 @@ func (w *Client) EntrustPaying(ctx context.Context, bm gopay.BodyMap) (wxRsp *En } wxRsp = new(EntrustPayingResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/merchant.go b/wechat/merchant.go index bc58aa7e..6fe455cc 100644 --- a/wechat/merchant.go +++ b/wechat/merchant.go @@ -54,7 +54,7 @@ func (w *Client) Transfer(ctx context.Context, bm gopay.BodyMap) (wxRsp *Transfe } wxRsp = new(TransfersResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -100,7 +100,7 @@ func (w *Client) GetTransferInfo(ctx context.Context, bm gopay.BodyMap) (wxRsp * } wxRsp = new(TransfersInfoResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -148,7 +148,7 @@ func (w *Client) PayBank(ctx context.Context, bm gopay.BodyMap) (wxRsp *PayBankR } wxRsp = new(PayBankResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -192,7 +192,7 @@ func (w *Client) QueryBank(ctx context.Context, bm gopay.BodyMap) (wxRsp *QueryB } wxRsp = new(QueryBankResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -231,7 +231,7 @@ func (w *Client) GetRSAPublicKey(ctx context.Context, bm gopay.BodyMap) (wxRsp * } wxRsp = new(RSAPublicKeyResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -275,7 +275,7 @@ func (w *Client) profitSharing(ctx context.Context, bm gopay.BodyMap, uri string } wxRsp = new(ProfitSharingResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -301,7 +301,7 @@ func (w *Client) ProfitSharingQuery(ctx context.Context, bm gopay.BodyMap) (wxRs } wxRsp = new(ProfitSharingQueryResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -322,7 +322,7 @@ func (w *Client) ProfitSharingAddReceiver(ctx context.Context, bm gopay.BodyMap) } wxRsp = new(ProfitSharingAddReceiverResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -343,7 +343,7 @@ func (w *Client) ProfitSharingRemoveReceiver(ctx context.Context, bm gopay.BodyM } wxRsp = new(ProfitSharingAddReceiverResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -371,7 +371,7 @@ func (w *Client) ProfitSharingFinish(ctx context.Context, bm gopay.BodyMap) (wxR } wxRsp = new(ProfitSharingResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -404,7 +404,7 @@ func (w *Client) ProfitSharingReturn(ctx context.Context, bm gopay.BodyMap) (wxR } wxRsp = new(ProfitSharingReturnResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -430,7 +430,7 @@ func (w *Client) ProfitSharingReturnQuery(ctx context.Context, bm gopay.BodyMap) } wxRsp = new(ProfitSharingReturnResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/oplatform_api.go b/wechat/oplatform_api.go index 33a1fdea..d428123b 100644 --- a/wechat/oplatform_api.go +++ b/wechat/oplatform_api.go @@ -21,7 +21,6 @@ import ( func GetOauth2AccessToken(ctx context.Context, appId, appSecret, code string) (accessToken *Oauth2AccessToken, err error) { accessToken = new(Oauth2AccessToken) url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code" - _, err = xhttp.NewClient().Get(url).EndStruct(ctx, accessToken) if err != nil { return nil, err @@ -36,7 +35,6 @@ func GetOauth2AccessToken(ctx context.Context, appId, appSecret, code string) (a func RefreshOauth2AccessToken(ctx context.Context, appId, refreshToken string) (accessToken *Oauth2AccessToken, err error) { accessToken = new(Oauth2AccessToken) url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appId + "&grant_type=refresh_token&refresh_token=" + refreshToken - _, err = xhttp.NewClient().Get(url).EndStruct(ctx, accessToken) if err != nil { return nil, err @@ -51,7 +49,6 @@ func RefreshOauth2AccessToken(ctx context.Context, appId, refreshToken string) ( func CheckOauth2AccessToken(ctx context.Context, accessToken, openid string) (result *CheckAccessTokenRsp, err error) { result = new(CheckAccessTokenRsp) url := "https://api.weixin.qq.com/sns/auth?access_token=" + accessToken + "&openid=" + openid - _, err = xhttp.NewClient().Get(url).EndStruct(ctx, result) if err != nil { return nil, err diff --git a/wechat/payment_api.go b/wechat/payment_api.go index 50fc556a..b0ea4bb3 100644 --- a/wechat/payment_api.go +++ b/wechat/payment_api.go @@ -38,7 +38,7 @@ func ParseNotifyToBodyMap(req *http.Request) (bm gopay.BodyMap, err error) { } bm = make(gopay.BodyMap) if err = xml.Unmarshal(bs, &bm); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return } @@ -119,7 +119,7 @@ func DecryptRefundNotifyReqInfo(reqInfo, apiKey string) (refundNotify *RefundNot bs = xaes.PKCS7UnPadding(encryptionB) refundNotify = new(RefundNotify) if err = xml.Unmarshal(bs, refundNotify); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return } diff --git a/wechat/public_api.go b/wechat/public_api.go deleted file mode 100644 index 2925f3dd..00000000 --- a/wechat/public_api.go +++ /dev/null @@ -1,6 +0,0 @@ -/* - 微信公众号 - 文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html -*/ - -package wechat diff --git a/wechat/red.go b/wechat/red.go index ff4def47..b43608ba 100644 --- a/wechat/red.go +++ b/wechat/red.go @@ -45,7 +45,7 @@ func (w *Client) SendCashRed(ctx context.Context, bm gopay.BodyMap) (wxRsp *Send } wxRsp = new(SendCashRedResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -82,7 +82,7 @@ func (w *Client) SendGroupCashRed(ctx context.Context, bm gopay.BodyMap) (wxRsp } wxRsp = new(SendCashRedResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -119,7 +119,7 @@ func (w *Client) SendAppletRed(ctx context.Context, bm gopay.BodyMap) (wxRsp *Se } wxRsp = new(SendAppletRedResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } @@ -156,7 +156,7 @@ func (w *Client) QueryRedRecord(ctx context.Context, bm gopay.BodyMap) (wxRsp *Q } wxRsp = new(QueryRedRecordResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { - return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } return wxRsp, nil } diff --git a/wechat/v3/applyment_for_sub.go b/wechat/v3/applyment_for_sub.go index ef3a283b..e418d5bf 100644 --- a/wechat/v3/applyment_for_sub.go +++ b/wechat/v3/applyment_for_sub.go @@ -28,18 +28,17 @@ func (c *ClientV3) V3Apply4SubSubmit(ctx context.Context, bm gopay.BodyMap) (*Ap if err != nil { return nil, err } - wxResp := &Apply4SubSubmitRsp{Code: Success, SignInfo: si} - wxResp.Response = new(Apply4SubSubmit) - if err = json.Unmarshal(bs, wxResp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + wxRsp := &Apply4SubSubmitRsp{Code: Success, SignInfo: si} + wxRsp.Response = new(Apply4SubSubmit) + if err = json.Unmarshal(bs, wxRsp.Response); err != nil { + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } - if res.StatusCode != http.StatusOK { - wxResp.Code = res.StatusCode - wxResp.Error = string(bs) - return wxResp, nil + wxRsp.Code = res.StatusCode + wxRsp.Error = string(bs) + return wxRsp, nil } - return wxResp, c.verifySyncSign(si) + return wxRsp, c.verifySyncSign(si) } // 通过业务申请编号查询申请状态API @@ -59,7 +58,7 @@ func (c *ClientV3) V3Apply4SubQueryByBusinessCode(ctx context.Context, businessC wxRsp := &Apply4SubQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Apply4SubQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -86,7 +85,7 @@ func (c *ClientV3) V3Apply4SubQueryByApplyId(ctx context.Context, applyId string wxRsp := &Apply4SubQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Apply4SubQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -144,7 +143,7 @@ func (c *ClientV3) V3Apply4SubQuerySettlement(ctx context.Context, subMchId stri wxRsp := &Apply4SubQuerySettlementRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Apply4SubQuerySettlement) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/bank.go b/wechat/v3/bank.go index de02717d..7e078f4a 100644 --- a/wechat/v3/bank.go +++ b/wechat/v3/bank.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" + "github.com/go-pay/gopay" "github.com/go-pay/gopay/pkg/util" ) @@ -26,7 +27,7 @@ func (c *ClientV3) V3BankSearchBank(ctx context.Context, accountNo string) (wxRs wxRsp = &BankSearchBankRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchBank) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -55,7 +56,7 @@ func (c *ClientV3) V3BankSearchPersonalList(ctx context.Context, limit, offset i wxRsp = &BankSearchPersonalListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchList) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -84,7 +85,7 @@ func (c *ClientV3) V3BankSearchCorporateList(ctx context.Context, limit, offset wxRsp = &BankSearchCorporateListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchList) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -109,7 +110,7 @@ func (c *ClientV3) V3BankSearchProvinceList(ctx context.Context) (wxRsp *BankSea wxRsp = &BankSearchProvinceListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchProvince) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -135,7 +136,7 @@ func (c *ClientV3) V3BankSearchCityList(ctx context.Context, provinceCode int) ( wxRsp = &BankSearchCityListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchCity) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -164,7 +165,7 @@ func (c *ClientV3) V3BankSearchBranchList(ctx context.Context, bankAliasCode str wxRsp = &BankSearchBranchListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BankSearchBranch) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/bill.go b/wechat/v3/bill.go index fd6b8b10..ded27aa4 100644 --- a/wechat/v3/bill.go +++ b/wechat/v3/bill.go @@ -39,7 +39,7 @@ func (c *ClientV3) V3BillTradeBill(ctx context.Context, bm gopay.BodyMap) (wxRsp wxRsp = &BillRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TradeBill) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -75,7 +75,7 @@ func (c *ClientV3) V3BillFundFlowBill(ctx context.Context, bm gopay.BodyMap) (wx wxRsp = &BillRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TradeBill) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -116,7 +116,7 @@ func (c *ClientV3) V3BillEcommerceFundFlowBill(ctx context.Context, bm gopay.Bod wxRsp = &EcommerceFundFlowBillRsp{Code: Success, SignInfo: si} wxRsp.Response = new(DownloadBill) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -151,7 +151,7 @@ func (c *ClientV3) V3BillSubFundFlowBill(ctx context.Context, bm gopay.BodyMap) wxRsp = &BillRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TradeBill) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/business_circle.go b/wechat/v3/business_circle.go index 84b32281..e96bfd55 100644 --- a/wechat/v3/business_circle.go +++ b/wechat/v3/business_circle.go @@ -48,7 +48,7 @@ func (c *ClientV3) V3BusinessAuthPointsQuery(ctx context.Context, appid, openid wxRsp := &BusinessAuthPointsQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusinessAuthPointsQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/cert.go b/wechat/v3/cert.go index 8e73b6d5..371602e2 100644 --- a/wechat/v3/cert.go +++ b/wechat/v3/cert.go @@ -53,7 +53,7 @@ func GetPlatformCerts(ctx context.Context, mchid, apiV3Key, serialNo, privateKey h.Write([]byte(_str)) result, err := rsa.SignPKCS1v15(rand.Reader, priKey, crypto.SHA256, h.Sum(nil)) if err != nil { - return nil, fmt.Errorf("rsa.SignPKCS1v15(),err:%+v", err) + return nil, fmt.Errorf("[%w]: %+v", gopay.SignatureErr, err) } sign := base64.StdEncoding.EncodeToString(result) // Authorization diff --git a/wechat/v3/client.go b/wechat/v3/client.go index ad46d9fc..fb7ee39c 100644 --- a/wechat/v3/client.go +++ b/wechat/v3/client.go @@ -30,9 +30,12 @@ type ClientV3 struct { // NewClientV3 初始化微信客户端 V3 // mchid:商户ID 或者服务商模式的 sp_mchid // serialNo:商户API证书的证书序列号 -// ApiV3Key:APIv3Key,商户平台获取 +// apiV3Key:APIv3Key,商户平台获取 // privateKey:商户API证书下载后,私钥 apiclient_key.pem 读取后的字符串内容 func NewClientV3(mchid, serialNo, apiV3Key, privateKey string) (client *ClientV3, err error) { + if mchid == util.NULL || serialNo == util.NULL || apiV3Key == util.NULL || privateKey == util.NULL { + return nil, gopay.MissWechatInitParamErr + } priKey, err := xpem.DecodePrivateKey([]byte(privateKey)) if err != nil { return nil, err diff --git a/wechat/v3/complaint.go b/wechat/v3/complaint.go index fc06e8c9..a81fc327 100644 --- a/wechat/v3/complaint.go +++ b/wechat/v3/complaint.go @@ -28,7 +28,7 @@ func (c *ClientV3) V3ComplaintNotifyUrlCreate(ctx context.Context, url string) ( wxRsp = &ComplaintNotifyUrlRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintNotifyUrl) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -54,7 +54,7 @@ func (c *ClientV3) V3ComplaintNotifyUrlQuery(ctx context.Context) (wxRsp *Compla wxRsp = &ComplaintNotifyUrlRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintNotifyUrl) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -82,7 +82,7 @@ func (c *ClientV3) V3ComplaintNotifyUrlUpdate(ctx context.Context, url string) ( wxRsp = &ComplaintNotifyUrlRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintNotifyUrl) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -138,7 +138,7 @@ func (c *ClientV3) V3ComplaintUploadImage(ctx context.Context, fileName, fileSha wxRsp = &MediaUploadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MediaUpload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -165,7 +165,7 @@ func (c *ClientV3) V3ComplaintList(ctx context.Context, bm gopay.BodyMap) (wxRsp wxRsp = &ComplaintListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintList) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -192,7 +192,7 @@ func (c *ClientV3) V3ComplaintNegotiationHistory(ctx context.Context, complaintI wxRsp = &ComplaintNegotiationHistoryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintNegotiationHistory) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -219,7 +219,7 @@ func (c *ClientV3) V3ComplaintDetail(ctx context.Context, complaintId string) (w wxRsp = &ComplaintDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ComplaintDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/discount_card.go b/wechat/v3/discount_card.go index 62bd7fe6..6ef5e060 100644 --- a/wechat/v3/discount_card.go +++ b/wechat/v3/discount_card.go @@ -24,7 +24,7 @@ func (c *ClientV3) V3DiscountCardApply(ctx context.Context, bm gopay.BodyMap) (w wxRsp = &DiscountCardApplyRsp{Code: Success, SignInfo: si} wxRsp.Response = new(DiscountCardApply) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -76,7 +76,7 @@ func (c *ClientV3) V3DiscountCardQuery(ctx context.Context, outCardCode string) wxRsp = &DiscountCardQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(DiscountCardQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/ecommerce.go b/wechat/v3/ecommerce.go index be4a82c5..93752d68 100644 --- a/wechat/v3/ecommerce.go +++ b/wechat/v3/ecommerce.go @@ -22,17 +22,17 @@ func (c *ClientV3) V3EcommerceApply(ctx context.Context, bm gopay.BodyMap) (*Eco if err != nil { return nil, err } - wxResp := &EcommerceApplyRsp{Code: Success, SignInfo: si} - wxResp.Response = new(EcommerceApply) - if err = json.Unmarshal(bs, wxResp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + wxRsp := &EcommerceApplyRsp{Code: Success, SignInfo: si} + wxRsp.Response = new(EcommerceApply) + if err = json.Unmarshal(bs, wxRsp.Response); err != nil { + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { - wxResp.Code = res.StatusCode - wxResp.Error = string(bs) - return wxResp, nil + wxRsp.Code = res.StatusCode + wxRsp.Error = string(bs) + return wxRsp, nil } - return wxResp, c.verifySyncSign(si) + return wxRsp, c.verifySyncSign(si) } // 查询申请状态API @@ -60,7 +60,7 @@ func (c *ClientV3) V3EcommerceApplyStatus(ctx context.Context, applyId int64, ou wxRsp := &EcommerceApplyStatusRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceApplyStatus) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -86,7 +86,7 @@ func (c *ClientV3) V3EcommerceProfitShare(ctx context.Context, bm gopay.BodyMap) wxRsp := &EcommerceProfitShareRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShare) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -113,7 +113,7 @@ func (c *ClientV3) V3EcommerceProfitShareQuery(ctx context.Context, bm gopay.Bod wxRsp := &EcommerceProfitShareQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -139,7 +139,7 @@ func (c *ClientV3) V3EcommerceProfitShareReturn(ctx context.Context, bm gopay.Bo wxRsp := &EcommerceProfitShareReturnRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareReturn) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -166,7 +166,7 @@ func (c *ClientV3) V3EcommerceProfitShareReturnResult(ctx context.Context, bm go wxRsp := &EcommerceProfitShareReturnResultRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareReturn) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -192,7 +192,7 @@ func (c *ClientV3) V3EcommerceProfitShareFinish(ctx context.Context, bm gopay.Bo wxRsp := &EcommerceProfitShareFinishRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareFinish) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -219,7 +219,7 @@ func (c *ClientV3) V3EcommerceProfitShareUnsplitAmount(ctx context.Context, tran wxRsp := &EcommerceProfitShareUnsplitAmountRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareUnsplitAmount) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -245,7 +245,7 @@ func (c *ClientV3) V3EcommerceProfitShareAddReceiver(ctx context.Context, bm gop wxRsp := &EcommerceProfitShareAddReceiverRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareReceiver) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -271,7 +271,7 @@ func (c *ClientV3) V3EcommerceProfitShareDeleteReceiver(ctx context.Context, bm wxRsp := &EcommerceProfitShareDeleteReceiverRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceProfitShareReceiver) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -297,7 +297,7 @@ func (c *ClientV3) V3EcommerceSubsidies(ctx context.Context, bm gopay.BodyMap) ( wxRsp := &EcommerceSubsidiesRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceSubsidies) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -323,7 +323,7 @@ func (c *ClientV3) V3EcommerceSubsidiesReturn(ctx context.Context, bm gopay.Body wxRsp := &EcommerceSubsidiesReturnRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceSubsidiesReturn) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -349,7 +349,7 @@ func (c *ClientV3) V3EcommerceSubsidiesCancel(ctx context.Context, bm gopay.Body wxRsp := &EcommerceSubsidiesCancelRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceSubsidiesCancel) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/gold_plan.go b/wechat/v3/gold_plan.go index a3770ce8..3db4d425 100644 --- a/wechat/v3/gold_plan.go +++ b/wechat/v3/gold_plan.go @@ -24,7 +24,7 @@ func (c *ClientV3) V3GoldPlanManage(ctx context.Context, bm gopay.BodyMap) (wxRs wxRsp = &GoldPlanManageRsp{Code: Success, SignInfo: si} wxRsp.Response = new(GoldPlanManage) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -49,7 +49,7 @@ func (c *ClientV3) V3GoldPlanBillManage(ctx context.Context, bm gopay.BodyMap) ( wxRsp = &GoldPlanManageRsp{Code: Success, SignInfo: si} wxRsp.Response = new(GoldPlanManage) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/market_busifavor.go b/wechat/v3/market_busifavor.go index 37af4eac..f0ed7531 100644 --- a/wechat/v3/market_busifavor.go +++ b/wechat/v3/market_busifavor.go @@ -25,7 +25,7 @@ func (c *ClientV3) V3BusiFavorBatchCreate(ctx context.Context, bm gopay.BodyMap) wxRsp = &BusiFavorCreateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchCreate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -52,7 +52,7 @@ func (c *ClientV3) V3BusiFavorBatchDetail(ctx context.Context, stockId string) ( wxRsp = &BusiFavorBatchDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorBatchDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -78,7 +78,7 @@ func (c *ClientV3) V3BusiFavorUse(ctx context.Context, bm gopay.BodyMap) (wxRsp wxRsp = &BusiFavorUseRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorUse) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -105,7 +105,7 @@ func (c *ClientV3) V3BusiFavorUserCoupons(ctx context.Context, openid string, bm wxRsp = &BusiFavorUserCouponsRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorUserCoupons) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -132,7 +132,7 @@ func (c *ClientV3) V3BusiFavorUserCouponDetail(ctx context.Context, openid, coup wxRsp = &BusiFavorUserCouponDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiUserCoupon) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -159,7 +159,7 @@ func (c *ClientV3) V3BusiFavorCodeUpload(ctx context.Context, stockId string, bm wxRsp = &BusiFavorCodeUploadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorCodeUpload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -185,7 +185,7 @@ func (c *ClientV3) V3BusiFavorCallbackUrlSet(ctx context.Context, bm gopay.BodyM wxRsp = &BusiFavorCallbackUrlSetRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorCallbackUrlSet) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -212,7 +212,7 @@ func (c *ClientV3) V3BusiFavorCallbackUrl(ctx context.Context, mchid string) (wx wxRsp = &BusiFavorCallbackUrlRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorCallbackUrl) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -238,7 +238,7 @@ func (c *ClientV3) V3BusiFavorAssociate(ctx context.Context, bm gopay.BodyMap) ( wxRsp = &BusiFavorAssociateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorAssociate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -264,7 +264,7 @@ func (c *ClientV3) V3BusiFavorDisassociate(ctx context.Context, bm gopay.BodyMap wxRsp = &BusiFavorDisassociateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorDisassociate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -291,7 +291,7 @@ func (c *ClientV3) V3BusiFavorBatchUpdate(ctx context.Context, stockId string, b wxRsp = &BusiFavorBatchUpdateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorBatchUpdate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -340,7 +340,7 @@ func (c *ClientV3) V3BusiFavorSend(ctx context.Context, cardId string, bm gopay. wxRsp = &BusiFavorSendRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorSend) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -366,7 +366,7 @@ func (c *ClientV3) V3BusiFavorReturn(ctx context.Context, bm gopay.BodyMap) (wxR wxRsp = &BusiFavorReturnRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorReturn) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -392,7 +392,7 @@ func (c *ClientV3) V3BusiFavorDeactivate(ctx context.Context, bm gopay.BodyMap) wxRsp = &BusiFavorDeactivateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorDeactivate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -418,7 +418,7 @@ func (c *ClientV3) V3BusiFavorSubsidyPay(ctx context.Context, bm gopay.BodyMap) wxRsp = &BusiFavorSubsidyPayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorSubsidyPay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -445,7 +445,7 @@ func (c *ClientV3) V3BusiFavorSubsidyPayDetail(ctx context.Context, subsidyRecei wxRsp = &BusiFavorSubsidyPayDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(BusiFavorSubsidyPay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/market_favor.go b/wechat/v3/market_favor.go index b64ee06f..0ac32fd3 100644 --- a/wechat/v3/market_favor.go +++ b/wechat/v3/market_favor.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3FavorBatchCreate(ctx context.Context, bm gopay.BodyMap) (wx wxRsp = &FavorBatchCreateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchCreate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -53,7 +53,7 @@ func (c *ClientV3) V3FavorBatchGrant(ctx context.Context, openid string, bm gopa wxRsp = &FavorBatchGrantRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchGrant) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -82,7 +82,7 @@ func (c *ClientV3) V3FavorBatchStart(ctx context.Context, stockId, stockCreatorM wxRsp = &FavorBatchStartRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchStart) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -109,7 +109,7 @@ func (c *ClientV3) V3FavorBatchList(ctx context.Context, bm gopay.BodyMap) (wxRs wxRsp = &FavorBatchListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchList) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -136,7 +136,7 @@ func (c *ClientV3) V3FavorBatchDetail(ctx context.Context, stockId, stockCreator wxRsp = &FavorBatchDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatch) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -163,7 +163,7 @@ func (c *ClientV3) V3FavorDetail(ctx context.Context, appid, couponId, openid st wxRsp = &FavorDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -193,7 +193,7 @@ func (c *ClientV3) V3FavorMerchant(ctx context.Context, stockId, stockCreatorMch wxRsp = &FavorMerchantRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorMerchant) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -223,7 +223,7 @@ func (c *ClientV3) V3FavorItems(ctx context.Context, stockId, stockCreatorMchid wxRsp = &FavorItemsRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorItems) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -250,7 +250,7 @@ func (c *ClientV3) V3FavorUserCoupons(ctx context.Context, openid string, bm gop wxRsp = &FavorUserCouponsRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorUserCoupons) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -277,7 +277,7 @@ func (c *ClientV3) V3FavorUseFlowDownload(ctx context.Context, stockId string) ( wxRsp = &FavorUseFlowDownloadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorFlowDownload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -304,7 +304,7 @@ func (c *ClientV3) V3FavorRefundFlowDownload(ctx context.Context, stockId string wxRsp = &FavorRefundFlowDownloadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorFlowDownload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -330,7 +330,7 @@ func (c *ClientV3) V3FavorCallbackUrlSet(ctx context.Context, bm gopay.BodyMap) wxRsp = &FavorCallbackUrlSetRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorCallbackUrl) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -359,7 +359,7 @@ func (c *ClientV3) V3FavorBatchPause(ctx context.Context, stockId, stockCreatorM wxRsp = &FavorBatchPauseRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchPause) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -388,7 +388,7 @@ func (c *ClientV3) V3FavorBatchRestart(ctx context.Context, stockId, stockCreato wxRsp = &FavorBatchRestartRsp{Code: Success, SignInfo: si} wxRsp.Response = new(FavorBatchRestart) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/market_media.go b/wechat/v3/market_media.go index 522e950d..1bd37cbf 100644 --- a/wechat/v3/market_media.go +++ b/wechat/v3/market_media.go @@ -34,7 +34,7 @@ func (c *ClientV3) V3FavorMediaUploadImage(ctx context.Context, fileName, fileSh wxRsp = &MarketMediaUploadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MarketMediaUpload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/market_partner.go b/wechat/v3/market_partner.go index 0a4e28a4..17d77cbc 100644 --- a/wechat/v3/market_partner.go +++ b/wechat/v3/market_partner.go @@ -25,7 +25,7 @@ func (c *ClientV3) V3PartnershipsBuild(ctx context.Context, idempotencyKey strin wxRsp = &PartnershipsBuildRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnershipsBuild) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -51,7 +51,7 @@ func (c *ClientV3) V3PartnershipsTerminate(ctx context.Context, idempotencyKey s wxRsp = &PartnershipsTerminateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnershipsTerminate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -78,7 +78,7 @@ func (c *ClientV3) V3PartnershipsList(ctx context.Context, bm gopay.BodyMap) (wx wxRsp = &PartnershipsListRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnershipsList) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/media.go b/wechat/v3/media.go index 6bfa32d9..88e0e7a9 100644 --- a/wechat/v3/media.go +++ b/wechat/v3/media.go @@ -34,7 +34,7 @@ func (c *ClientV3) V3MediaUploadImage(ctx context.Context, fileName, fileSha256 wxRsp = &MediaUploadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MediaUpload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -68,7 +68,7 @@ func (c *ClientV3) V3MediaUploadVideo(ctx context.Context, fileName, fileSha256 wxRsp = &MediaUploadRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MediaUpload) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/merchant.go b/wechat/v3/merchant.go index 1bc9801d..0806ab28 100644 --- a/wechat/v3/merchant.go +++ b/wechat/v3/merchant.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3EcommerceBalance(ctx context.Context, subMchid string) (*Ec wxRsp := &EcommerceBalanceRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceBalance) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -54,7 +54,7 @@ func (c *ClientV3) V3MerchantBalance(ctx context.Context, accountType string) (* wxRsp := &MerchantBalanceRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MerchantBalance) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -83,7 +83,7 @@ func (c *ClientV3) V3MerchantDayBalance(ctx context.Context, accountType, date s wxRsp := &MerchantBalanceRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MerchantBalance) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -110,7 +110,7 @@ func (c *ClientV3) V3EcommerceIncomeRecord(ctx context.Context, bm gopay.BodyMap wxRsp := &PartnerIncomeRecordRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerIncomeRecord) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -138,7 +138,7 @@ func (c *ClientV3) V3MerchantIncomeRecord(ctx context.Context, bm gopay.BodyMap) wxRsp := &MerchantIncomeRecordRsp{Code: Success, SignInfo: si} wxRsp.Response = new(MerchantIncomeRecord) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/pay.go b/wechat/v3/pay.go index 2110a493..a913e622 100644 --- a/wechat/v3/pay.go +++ b/wechat/v3/pay.go @@ -29,7 +29,7 @@ func (c *ClientV3) V3TransactionApp(ctx context.Context, bm gopay.BodyMap) (wxRs wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -58,7 +58,7 @@ func (c *ClientV3) V3TransactionJsapi(ctx context.Context, bm gopay.BodyMap) (wx wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -86,7 +86,7 @@ func (c *ClientV3) V3TransactionNative(ctx context.Context, bm gopay.BodyMap) (w wxRsp = &NativeRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Native) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -114,7 +114,7 @@ func (c *ClientV3) V3TransactionH5(ctx context.Context, bm gopay.BodyMap) (wxRsp wxRsp = &H5Rsp{Code: Success, SignInfo: si} wxRsp.Response = new(H5Url) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -149,7 +149,7 @@ func (c *ClientV3) V3TransactionQueryOrder(ctx context.Context, orderNoType Orde wxRsp = &QueryOrderRsp{Code: Success, SignInfo: si} wxRsp.Response = new(QueryOrder) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/pay_combine.go b/wechat/v3/pay_combine.go index a839278d..49f7db4e 100644 --- a/wechat/v3/pay_combine.go +++ b/wechat/v3/pay_combine.go @@ -30,7 +30,7 @@ func (c *ClientV3) V3CombineTransactionApp(ctx context.Context, bm gopay.BodyMap wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -63,7 +63,7 @@ func (c *ClientV3) V3CombineTransactionJsapi(ctx context.Context, bm gopay.BodyM wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -93,7 +93,7 @@ func (c *ClientV3) V3CombineTransactionNative(ctx context.Context, bm gopay.Body wxRsp = &NativeRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Native) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -121,7 +121,7 @@ func (c *ClientV3) V3CombineTransactionH5(ctx context.Context, bm gopay.BodyMap) wxRsp = &H5Rsp{Code: Success, SignInfo: si} wxRsp.Response = new(H5Url) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -150,7 +150,7 @@ func (c *ClientV3) V3CombineQueryOrder(ctx context.Context, traderNo string) (wx wxRsp = &CombineQueryOrderRsp{Code: Success, SignInfo: si} wxRsp.Response = new(CombineQueryOrder) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/pay_partner.go b/wechat/v3/pay_partner.go index bc5d5bf1..e460b523 100644 --- a/wechat/v3/pay_partner.go +++ b/wechat/v3/pay_partner.go @@ -30,7 +30,7 @@ func (c *ClientV3) V3PartnerTransactionApp(ctx context.Context, bm gopay.BodyMap wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -61,7 +61,7 @@ func (c *ClientV3) V3PartnerTransactionJsapi(ctx context.Context, bm gopay.BodyM wxRsp = &PrepayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Prepay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -90,7 +90,7 @@ func (c *ClientV3) V3PartnerTransactionNative(ctx context.Context, bm gopay.Body wxRsp = &NativeRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Native) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -118,7 +118,7 @@ func (c *ClientV3) V3PartnerTransactionH5(ctx context.Context, bm gopay.BodyMap) wxRsp = &H5Rsp{Code: Success, SignInfo: si} wxRsp.Response = new(H5Url) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -157,7 +157,7 @@ func (c *ClientV3) V3PartnerQueryOrder(ctx context.Context, orderNoType OrderNoT wxRsp = &PartnerQueryOrderRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerQueryOrder) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/profit_share.go b/wechat/v3/profit_share.go index 9cca4ede..342277c4 100644 --- a/wechat/v3/profit_share.go +++ b/wechat/v3/profit_share.go @@ -27,7 +27,7 @@ func (c *ClientV3) V3ProfitShareOrder(ctx context.Context, bm gopay.BodyMap) (*P wxRsp := &ProfitShareOrderRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareOrder) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -55,7 +55,7 @@ func (c *ClientV3) V3ProfitShareOrderQuery(ctx context.Context, orderNo string, wxRsp := &ProfitShareOrderQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareOrderQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -82,7 +82,7 @@ func (c *ClientV3) V3ProfitShareReturn(ctx context.Context, bm gopay.BodyMap) (* wxRsp := &ProfitShareReturnRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareReturn) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -110,7 +110,7 @@ func (c *ClientV3) V3ProfitShareReturnResult(ctx context.Context, returnNo strin wxRsp := &ProfitShareReturnResultRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareReturnResult) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -137,7 +137,7 @@ func (c *ClientV3) V3ProfitShareOrderUnfreeze(ctx context.Context, bm gopay.Body wxRsp := &ProfitShareOrderUnfreezeRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareOrderUnfreeze) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -165,7 +165,7 @@ func (c *ClientV3) V3ProfitShareUnsplitAmount(ctx context.Context, transId strin wxRsp := &ProfitShareUnsplitAmountRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareUnsplitAmount) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -192,7 +192,7 @@ func (c *ClientV3) V3ProfitShareMerchantConfigs(ctx context.Context, subMchId st wxRsp := &ProfitShareMerchantConfigsRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareMerchantConfigs) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -219,7 +219,7 @@ func (c *ClientV3) V3ProfitShareAddReceiver(ctx context.Context, bm gopay.BodyMa wxRsp := &ProfitShareAddReceiverRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareAddReceiver) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -246,7 +246,7 @@ func (c *ClientV3) V3ProfitShareDeleteReceiver(ctx context.Context, bm gopay.Bod wxRsp := &ProfitShareDeleteReceiverRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareDeleteReceiver) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -274,7 +274,7 @@ func (c *ClientV3) V3ProfitShareBills(ctx context.Context, bm gopay.BodyMap) (*P wxRsp := &ProfitShareBillsRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ProfitShareBills) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/refund.go b/wechat/v3/refund.go index 6b6ce97e..ab565d14 100644 --- a/wechat/v3/refund.go +++ b/wechat/v3/refund.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3Refund(ctx context.Context, bm gopay.BodyMap) (wxRsp *Refun wxRsp = &RefundRsp{Code: Success, SignInfo: si} wxRsp.Response = new(RefundOrderResponse) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -58,7 +58,7 @@ func (c *ClientV3) V3RefundQuery(ctx context.Context, outRefundNo string, bm gop wxRsp = &RefundQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(RefundQueryResponse) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -84,7 +84,7 @@ func (c *ClientV3) V3EcommerceRefund(ctx context.Context, bm gopay.BodyMap) (wxR wxRsp = &EcommerceRefundRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceRefund) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -111,7 +111,7 @@ func (c *ClientV3) V3EcommerceRefundQueryById(ctx context.Context, refundId stri wxRsp = &EcommerceRefundQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceRefundQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -138,7 +138,7 @@ func (c *ClientV3) V3EcommerceRefundQueryByNo(ctx context.Context, outRefundNo s wxRsp = &EcommerceRefundQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceRefundQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -165,7 +165,7 @@ func (c *ClientV3) V3EcommerceRefundAdvance(ctx context.Context, refundId string wxRsp = &EcommerceRefundAdvanceRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceRefundAdvance) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -192,7 +192,7 @@ func (c *ClientV3) V3EcommerceRefundAdvanceResult(ctx context.Context, refundId wxRsp = &EcommerceRefundAdvanceRsp{Code: Success, SignInfo: si} wxRsp.Response = new(EcommerceRefundAdvance) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/score.go b/wechat/v3/score.go index 773e3e7b..9f4b1599 100644 --- a/wechat/v3/score.go +++ b/wechat/v3/score.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3ScoreDirectComplete(ctx context.Context, bm gopay.BodyMap) wxRsp = &ScoreDirectCompleteRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreDirectComplete) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -51,7 +51,7 @@ func (c *ClientV3) V3ScorePermission(ctx context.Context, bm gopay.BodyMap) (wxR wxRsp = &ScorePermissionRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScorePermission) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -77,7 +77,7 @@ func (c *ClientV3) V3ScorePermissionQuery(ctx context.Context, authCode, service wxRsp = &ScorePermissionQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScorePermissionQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -128,7 +128,7 @@ func (c *ClientV3) V3ScorePermissionOpenidQuery(ctx context.Context, appid, open wxRsp = &ScorePermissionOpenidQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScorePermissionOpenidQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -179,7 +179,7 @@ func (c *ClientV3) V3ScoreOrderCreate(ctx context.Context, bm gopay.BodyMap) (wx wxRsp = &ScoreOrderCreateRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderCreate) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -213,7 +213,7 @@ func (c *ClientV3) V3ScoreOrderQuery(ctx context.Context, orderNoType OrderNoTyp wxRsp = &ScoreOrderQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -243,7 +243,7 @@ func (c *ClientV3) V3ScoreOrderCancel(ctx context.Context, appid, tradeNo, servi wxRsp = &ScoreOrderCancelRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderCancel) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -269,7 +269,7 @@ func (c *ClientV3) V3ScoreOrderModify(ctx context.Context, tradeNo string, bm go wxRsp = &ScoreOrderModifyRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderModify) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -295,7 +295,7 @@ func (c *ClientV3) V3ScoreOrderComplete(ctx context.Context, tradeNo string, bm wxRsp = &ScoreOrderCompleteRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderComplete) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -324,7 +324,7 @@ func (c *ClientV3) V3ScoreOrderPay(ctx context.Context, appid, tradeNo, servicei wxRsp = &ScoreOrderPayRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderPay) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -350,7 +350,7 @@ func (c *ClientV3) V3ScoreOrderSync(ctx context.Context, tradeNo string, bm gopa wxRsp = &ScoreOrderSyncRsp{Code: Success, SignInfo: si} wxRsp.Response = new(ScoreOrderSync) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/sign.go b/wechat/v3/sign.go index 62dd3137..556e0b55 100644 --- a/wechat/v3/sign.go +++ b/wechat/v3/sign.go @@ -29,7 +29,7 @@ func V3VerifySign(timestamp, nonce, signBody, sign, wxPubKeyContent string) (err h := sha256.New() h.Write([]byte(str)) if err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, h.Sum(nil), signBytes); err != nil { - return fmt.Errorf("verify sign failed: %w", err) + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) } return nil } @@ -43,7 +43,7 @@ func V3VerifySignByPK(timestamp, nonce, signBody, sign string, wxPublicKey *rsa. h := sha256.New() h.Write([]byte(str)) if err = rsa.VerifyPKCS1v15(wxPublicKey, crypto.SHA256, h.Sum(nil), signBytes); err != nil { - return fmt.Errorf("verify sign failed: %w", err) + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) } return nil } @@ -144,7 +144,7 @@ func (c *ClientV3) rsaSign(str string) (string, error) { h.Write([]byte(str)) result, err := rsa.SignPKCS1v15(rand.Reader, c.privateKey, crypto.SHA256, h.Sum(nil)) if err != nil { - return "", fmt.Errorf("rsa.SignPKCS1v15(),err:%w", err) + return util.NULL, fmt.Errorf("[%w]: %+v", gopay.SignatureErr, err) } return base64.StdEncoding.EncodeToString(result), nil } @@ -159,7 +159,7 @@ func (c *ClientV3) verifySyncSign(si *SignInfo) (err error) { h := sha256.New() h.Write([]byte(str)) if err = rsa.VerifyPKCS1v15(c.wxPublicKey, crypto.SHA256, h.Sum(nil), signBytes); err != nil { - return fmt.Errorf("verify sign failed: %w", err) + return fmt.Errorf("[%w]: %v", gopay.VerifySignatureErr, err) } return nil } diff --git a/wechat/v3/smart_guide.go b/wechat/v3/smart_guide.go index 2689652d..cdbfc629 100644 --- a/wechat/v3/smart_guide.go +++ b/wechat/v3/smart_guide.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3SmartGuideReg(ctx context.Context, bm gopay.BodyMap) (wxRsp wxRsp = &SmartGuideRegRsp{Code: Success, SignInfo: si} wxRsp.Response = new(SmartGuideReg) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -82,7 +82,7 @@ func (c *ClientV3) V3SmartGuideQuery(ctx context.Context, bm gopay.BodyMap) (wxR wxRsp = &SmartGuideQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(SmartGuideQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/transfer.go b/wechat/v3/transfer.go index 36db1299..bc9d9f86 100644 --- a/wechat/v3/transfer.go +++ b/wechat/v3/transfer.go @@ -26,7 +26,7 @@ func (c *ClientV3) V3Transfer(ctx context.Context, bm gopay.BodyMap) (*TransferR wxRsp := &TransferRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Transfer) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -53,7 +53,7 @@ func (c *ClientV3) V3PartnerTransfer(ctx context.Context, bm gopay.BodyMap) (*Tr wxRsp := &TransferRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Transfer) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -82,7 +82,7 @@ func (c *ClientV3) V3TransferQuery(ctx context.Context, batchId string, bm gopay wxRsp := &TransferQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -111,7 +111,7 @@ func (c *ClientV3) V3PartnerTransferQuery(ctx context.Context, batchId string, b wxRsp := &PartnerTransferQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerTransferQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -138,7 +138,7 @@ func (c *ClientV3) V3TransferDetail(ctx context.Context, batchId, detailId strin wxRsp := &TransferDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferDetailQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -165,7 +165,7 @@ func (c *ClientV3) V3PartnerTransferDetail(ctx context.Context, batchId, detailI wxRsp := &PartnerTransferDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerTransferDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -200,7 +200,7 @@ func (c *ClientV3) V3TransferMerchantQuery(ctx context.Context, outBatchNo strin wxRsp := &TransferMerchantQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferMerchantQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -229,7 +229,7 @@ func (c *ClientV3) V3PartnerTransferMerchantQuery(ctx context.Context, outBatchN wxRsp := &PartnerTransferMerchantQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerTransferMerchantQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -256,7 +256,7 @@ func (c *ClientV3) V3TransferMerchantDetail(ctx context.Context, outBatchNo, out wxRsp := &TransferMerchantDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferMerchantDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -283,7 +283,7 @@ func (c *ClientV3) V3PartnerTransferMerchantDetail(ctx context.Context, outBatch wxRsp := &PartnerTransferMerchantDetailRsp{Code: Success, SignInfo: si} wxRsp.Response = new(PartnerTransferMerchantDetail) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -319,7 +319,7 @@ func (c *ClientV3) V3TransferReceipt(ctx context.Context, outBatchNo string) (*T wxRsp := &TransferReceiptRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferReceipt) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -347,7 +347,7 @@ func (c *ClientV3) V3TransferReceiptQuery(ctx context.Context, outBatchNo string wxRsp := &TransferReceiptQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferReceiptQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -374,7 +374,7 @@ func (c *ClientV3) V3TransferDetailReceipt(ctx context.Context, bm gopay.BodyMap wxRsp := &TransferDetailReceiptRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferDetailReceipt) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -402,7 +402,7 @@ func (c *ClientV3) V3TransferDetailReceiptQuery(ctx context.Context, bm gopay.Bo wxRsp := &TransferDetailReceiptQueryRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TransferDetailReceiptQuery) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode diff --git a/wechat/v3/withdraw.go b/wechat/v3/withdraw.go index f96bb817..4bb05836 100644 --- a/wechat/v3/withdraw.go +++ b/wechat/v3/withdraw.go @@ -27,7 +27,7 @@ func (c *ClientV3) V3Withdraw(ctx context.Context, bm gopay.BodyMap) (*WithdrawR wxRsp := &WithdrawRsp{Code: Success, SignInfo: si} wxRsp.Response = new(Withdraw) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -62,7 +62,7 @@ func (c *ClientV3) V3WithdrawStatus(ctx context.Context, withdrawId, outRequestN wxRsp := &WithdrawStatusRsp{Code: Success, SignInfo: si} wxRsp.Response = new(WithdrawStatus) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode @@ -98,7 +98,7 @@ func (c *ClientV3) V3WithdrawDownloadErrBill(ctx context.Context, bm gopay.BodyM wxRsp = &BillRsp{Code: Success, SignInfo: si} wxRsp.Response = new(TradeBill) if err = json.Unmarshal(bs, wxRsp.Response); err != nil { - return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) } if res.StatusCode != http.StatusOK { wxRsp.Code = res.StatusCode