Skip to content

Commit

Permalink
v1.5.76 (#240)
Browse files Browse the repository at this point in the history
* v1.5.76
  • Loading branch information
iGoogle-ink authored Apr 5, 2022
1 parent dcdabbe commit 9d7c9f8
Show file tree
Hide file tree
Showing 75 changed files with 976 additions and 835 deletions.
54 changes: 24 additions & 30 deletions alipay/ant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions alipay/ant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion alipay/cert/cert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cert

var (
Appid = "2021000117673683"
Appid = ""

// 私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czy18Ie+gMsnuuJO9XPTz7cBR8lRwPBslg0KLoaUM91uZ0ltoHCfPToAuA9NAbPon4J/5wsSXOwKyfGb0qTguwjq3hhiGNQ5yvFyzehpCnNu8uTETH3+h9VWXR3IKlAtK+F3uQuqs36BzS2oeiPLm0DWpSMK5cI8r6ECgYAVKxOHbN9ifZmU9f6pQoKoAY7Iy9cliR/G2JLTKNywlmQPA3TSn+5Tlk/PLyxufseT+4gkLRqZPbZWKWFCeSi05+vFK/FDeae2ivMr2ocMxpq8i4+60dnoaRHSPPmFi+UqkE5Vi7X+f7jjnbqlb2P3CDr/xoDum2hBiC+QeQ5TrwKBgEHBg3tMAdDkoC3S6gCxqG2g2nGRJufomFC6a6Nuoi91pn50fP9yode3wO7j/Cc669iA5sWliaPd57ol7mTOcDTQgrkoTJsC0KunJ4141ysU99HSPRdm5ykAfumOoVcN9Vdy8yEeq9zxVk2NGaKy8p+tpnz6N8DDg/8p8tRXuuqU"
Expand Down
9 changes: 6 additions & 3 deletions alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions alipay/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ var (
func TestMain(m *testing.M) {

// 初始化支付宝客户端
// appId:应用ID
// appid:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境
client, err = NewClient(cert.Appid, cert.PrivateKey, false)
if err != nil {
xlog.Error(err)
return
}
// 打开Debug开关,输出日志
client.DebugSwitch = gopay.DebugOn
// Debug开关,输出/关闭日志
client.DebugSwitch = gopay.DebugOff

// 配置公共参数
client.SetCharset("utf-8").
Expand Down
13 changes: 8 additions & 5 deletions alipay/common_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
9 changes: 4 additions & 5 deletions alipay/customs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions alipay/data_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions alipay/error.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions alipay/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
Loading

0 comments on commit 9d7c9f8

Please sign in to comment.