diff --git a/internal/biz/wechat.go b/internal/biz/wechat.go index e9761df..f3db1ad 100644 --- a/internal/biz/wechat.go +++ b/internal/biz/wechat.go @@ -24,3 +24,13 @@ func (biz *WechatBiz) CallBack(ctx context.Context, mchId string, headers *http. return response, nil } + +func (biz *WechatBiz) DecodeBody(ctx context.Context, mchId string, respBody []byte) (*bo.WechatVoucherNotifyBo, error) { + + response, err := biz.BankMultiActivityRepo.DecodeBody(ctx, mchId, respBody) + if err != nil { + return nil, err + } + + return response, nil +} diff --git a/internal/biz/wechatrepo/bank_multi_activity.go b/internal/biz/wechatrepo/bank_multi_activity.go index 626102e..7dcf05d 100644 --- a/internal/biz/wechatrepo/bank_multi_activity.go +++ b/internal/biz/wechatrepo/bank_multi_activity.go @@ -9,4 +9,5 @@ import ( type BankMultiActivityRepo interface { Order(order *bo.OrderBo) (couponId string, err error) Notify(ctx context.Context, mchId string, headers *http.Header, respBody []byte) (response *bo.WechatVoucherNotifyBo, err error) + DecodeBody(ctx context.Context, mchId string, respBody []byte) (*bo.WechatVoucherNotifyBo, error) } diff --git a/internal/data/wechatrepoimpl/bank_multi_activity.go b/internal/data/wechatrepoimpl/bank_multi_activity.go index 660a6bb..3767aef 100644 --- a/internal/data/wechatrepoimpl/bank_multi_activity.go +++ b/internal/data/wechatrepoimpl/bank_multi_activity.go @@ -87,3 +87,37 @@ func (w *BankMultiActivityImpl) Notify(ctx context.Context, mchId string, header PlainText: plainText, }, nil } + +func (w *BankMultiActivityImpl) DecodeBody(ctx context.Context, mchId string, respBody []byte) (*bo.WechatVoucherNotifyBo, error) { + + t, err := w.wx.Get(mchId) + if err != nil { + return nil, err + } + + var body utils.WxNotifyBody + if err = json.Unmarshal(respBody, &body); err != nil { + return nil, err + } + + decryptedText, err := t.DecodeBody(&body) + if err != nil { + return nil, err + } + + var plainText bo.PlainText + if err = json.Unmarshal([]byte(decryptedText), &plainText); err != nil { + return nil, fmt.Errorf("plainText json.Unmarshal error: %v", err) + } + + return &bo.WechatVoucherNotifyBo{ + ID: body.Id, + CreateTime: body.CreateTime, + ResourceType: body.ResourceType, + EventType: body.EventType, + Summary: body.Summary, + OriginalType: body.Resource.OriginalType, + AssociatedData: body.Resource.AssociatedData, + PlainText: plainText, + }, nil +} diff --git a/internal/server/http.go b/internal/server/http.go index cf919ee..41abba8 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -45,6 +45,7 @@ func NewHTTPServer( // https://gateway.dev.cdlsxd.cn/voucher/v1/notify/123456 123456为微信主体商户号 测试环境 // https://voucher.86698.cn/voucher/v1/notify/123456 123456为微信主体商户号 正式环境 srv.Route("/voucher/").POST("/v1/notify/{mch_id}", notifyService.Notify) + srv.Route("/voucher/").POST("/v1/notifyMock", notifyService.NotifyMock) // ---脚本-- // 订单通知重试 -- 不健全 diff --git a/internal/service/notify.go b/internal/service/notify.go index 4d903e3..be8eb91 100644 --- a/internal/service/notify.go +++ b/internal/service/notify.go @@ -10,6 +10,7 @@ import ( "io" http2 "net/http" "voucher/internal/biz" + "voucher/internal/biz/bo" "voucher/internal/pkg/helper" ) @@ -85,3 +86,48 @@ func (srv *NotifyService) Notify(ctx http.Context) error { return ctx.JSON(http2.StatusOK, nil) } + +// NotifyMock 模拟微信回调通知,用于测试 +func (srv *NotifyService) NotifyMock(ctx http.Context) error { + + bodyBytes, err := io.ReadAll(ctx.Request().Body) + if err != nil { + log.Errorf("微信回调通知,读取响应体失败: %v", err) + return fmt.Errorf("读取响应体失败: %w", err) + } + + if len(bodyBytes) == 0 { + log.Errorf("微信回调通知,响应体不能为空") + return ctx.JSON(http2.StatusOK, map[string]string{ + "code": "FAIL", + "message": "微信回调通知,响应体不能为空", + }) + } + + var bizData *bo.WechatVoucherNotifyBo + err = json.Unmarshal(bodyBytes, &bizData) + if err != nil { + log.Errorf("微信回调通知,解析响应体失败: %v", err) + return ctx.JSON(http2.StatusOK, map[string]string{ + "code": "FAIL", + "message": "微信回调通知,解析响应体失败", + }) + } + + ip := helper.GetClientIP(ctx) + + if err = srv.VoucherBiz.WechatNotifyConsumer(ctx, ip, bizData); err != nil { + log.Errorf("微信回调通知,consumer处理失败:%s\nbody:%s\n解析数据:%+v", err.Error(), string(bodyBytes), bizData) + + if errors.Is(err, gorm.ErrRecordNotFound) { + return ctx.JSON(http2.StatusOK, nil) + } + + return ctx.JSON(http2.StatusOK, map[string]string{ + "code": "FAIL", + "message": err.Error(), + }) + } + + return ctx.JSON(http2.StatusOK, nil) +}