切换主体

This commit is contained in:
ziming 2026-03-26 15:13:02 +08:00
parent 417e813b84
commit f34a27fbf2
2 changed files with 23 additions and 10 deletions

View File

@ -62,10 +62,14 @@ func (srv *Marketing) Query(appId, openId, couponId string) (response *SendResp,
return response, nil return response, nil
} }
// Notify .
func (srv *Marketing) Notify(_ context.Context, headers *http.Header, respBody []byte) (body *utils.WxNotifyBody, response string, err error) { 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 { if err != nil {
return nil, "", err return nil, "", err
} }

View File

@ -575,10 +575,10 @@ func (srv *MchConfig) Verify(request *http.Request) (string, error) {
return EncryptOAEPWithPublicKey(string(respBody), srv.wechatPayPublicKey) 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 { if respBody == nil {
return nil, "", fmt.Errorf("request HttpBody is nil") return nil, fmt.Errorf("request HttpBody is nil")
} }
err := ValidateResponse( err := ValidateResponse(
@ -588,25 +588,34 @@ func (srv *MchConfig) GetDecodeBody(headers *http.Header, respBody []byte) (*WxN
respBody, respBody,
) )
if err != nil { if err != nil {
return nil, "", err return nil, err
} }
var wxNotifyBody WxNotifyBody var wxNotifyBody WxNotifyBody
if err = json.Unmarshal(respBody, &wxNotifyBody); err != nil { 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) aesUtil, err := NewAesUtil(srv.aesKey)
if err != nil { 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 { if err != nil {
return nil, "", err return "", err
} }
return &wxNotifyBody, decryptedText, nil return decryptedText, nil
} }
// BuildSortedQueryString 函数接受一个 map返回按照字段名排序后的 URL 键值对格式字符串 // BuildSortedQueryString 函数接受一个 map返回按照字段名排序后的 URL 键值对格式字符串