切换主体
This commit is contained in:
parent
f1e813584f
commit
7e8ef4cd2b
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
// ---脚本--
|
||||
// 订单通知重试 -- 不健全
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue