diff --git a/internal/pkg/wechat/srv/marketing/marketing.go b/internal/pkg/wechat/srv/marketing/marketing.go index 362776a..ea67fd5 100644 --- a/internal/pkg/wechat/srv/marketing/marketing.go +++ b/internal/pkg/wechat/srv/marketing/marketing.go @@ -62,10 +62,14 @@ func (srv *Marketing) Query(appId, openId, couponId string) (response *SendResp, return response, nil } -// Notify . func (srv *Marketing) Notify(_ context.Context, headers *http.Header, respBody []byte) (body *utils.WxNotifyBody, response string, err error) { - wxNotifyBody, bizStr, err := srv.GetDecodeBody(headers, respBody) + wxNotifyBody, err := srv.GetNotifyBody(headers, respBody) + if err != nil { + return nil, "", err + } + + bizStr, err := srv.DecodeBody(wxNotifyBody) if err != nil { return nil, "", err } diff --git a/internal/pkg/wechat/utils/wxpay_utility.go b/internal/pkg/wechat/utils/wxpay_utility.go index 1089bbe..d8718af 100644 --- a/internal/pkg/wechat/utils/wxpay_utility.go +++ b/internal/pkg/wechat/utils/wxpay_utility.go @@ -575,10 +575,10 @@ func (srv *MchConfig) Verify(request *http.Request) (string, error) { return EncryptOAEPWithPublicKey(string(respBody), srv.wechatPayPublicKey) } -func (srv *MchConfig) GetDecodeBody(headers *http.Header, respBody []byte) (*WxNotifyBody, string, error) { +func (srv *MchConfig) GetNotifyBody(headers *http.Header, respBody []byte) (*WxNotifyBody, error) { if respBody == nil { - return nil, "", fmt.Errorf("request HttpBody is nil") + return nil, fmt.Errorf("request HttpBody is nil") } err := ValidateResponse( @@ -588,25 +588,34 @@ func (srv *MchConfig) GetDecodeBody(headers *http.Header, respBody []byte) (*WxN respBody, ) if err != nil { - return nil, "", err + return nil, err } var wxNotifyBody WxNotifyBody if err = json.Unmarshal(respBody, &wxNotifyBody); err != nil { - return nil, "", err + return nil, err } + return &wxNotifyBody, nil +} + +func (srv *MchConfig) DecodeBody(wxNotifyBody *WxNotifyBody) (string, error) { + aesUtil, err := NewAesUtil(srv.aesKey) if err != nil { - return nil, "", err + return "", err } - decryptedText, err := aesUtil.DecryptToString(wxNotifyBody.Resource.AssociatedData, wxNotifyBody.Resource.Nonce, wxNotifyBody.Resource.Ciphertext) + decryptedText, err := aesUtil.DecryptToString( + wxNotifyBody.Resource.AssociatedData, + wxNotifyBody.Resource.Nonce, + wxNotifyBody.Resource.Ciphertext, + ) if err != nil { - return nil, "", err + return "", err } - return &wxNotifyBody, decryptedText, nil + return decryptedText, nil } // BuildSortedQueryString 函数接受一个 map,返回按照字段名排序后的 URL 键值对格式字符串