From 8d294ef29777f2f3bc38986f69f6b8935ed01096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Thu, 13 Mar 2025 15:20:00 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=9F=A5=E8=AF=A2=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/cmb_cpn.proto | 8 +++++++ internal/biz/wechatrepo/cpn.go | 1 + internal/data/wechatrepoimpl/cpn.go | 36 ++++++++++++++++++++++++++++- internal/server/http.go | 1 + internal/service/cmb_mock.go | 22 ++++++++++++++++++ internal/service/voucher.go | 16 ++++++++----- 6 files changed, 77 insertions(+), 7 deletions(-) diff --git a/api/v1/cmb_cpn.proto b/api/v1/cmb_cpn.proto index fd8a20b..d03e5de 100644 --- a/api/v1/cmb_cpn.proto +++ b/api/v1/cmb_cpn.proto @@ -154,4 +154,12 @@ message EncryptBody { } message DecryptBody { string decryptBody = 1 [json_name = "decryptBody"]; +} + + +message QueryWechatVoucherNotifyUrlRequest { + string mch_id = 1 [json_name = "mch_id"]; +} +message QueryWechatVoucherNotifyUrlReply { + string url = 1 [json_name = "url"]; } \ No newline at end of file diff --git a/internal/biz/wechatrepo/cpn.go b/internal/biz/wechatrepo/cpn.go index bf13c38..8f2dee9 100644 --- a/internal/biz/wechatrepo/cpn.go +++ b/internal/biz/wechatrepo/cpn.go @@ -11,5 +11,6 @@ type WechatCpnRepo interface { Order(ctx context.Context, order *bo.OrderBo) (couponId string, err error) Query(ctx context.Context, order *bo.OrderBo) (vo.OrderStatus, error) QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error) + QueryCallback(ctx context.Context, mchId string) (*cashcoupons.Callback, error) RegisterNotifyTag(ctx context.Context, stockID string) error } diff --git a/internal/data/wechatrepoimpl/cpn.go b/internal/data/wechatrepoimpl/cpn.go index bd412a5..183ca6c 100644 --- a/internal/data/wechatrepoimpl/cpn.go +++ b/internal/data/wechatrepoimpl/cpn.go @@ -147,13 +147,47 @@ func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stock } if result.Response.StatusCode != CodeSuccess { - err = fmt.Errorf("查询活动微信返回错误StatusCode[%d]Status[%s]", result.Response.StatusCode, result.Response.Status) + err = fmt.Errorf("查询活动微信返回错误tatus[%s]", result.Response.Status) return nil, err } return response, nil } +func (c *CpnRepoImpl) QueryCallback(ctx context.Context, mchId string) (*cashcoupons.Callback, error) { + + client, err := data.GetClient(ctx, c.Server) + if err != nil { + return nil, err + } + + svc := cashcoupons.CallBackUrlApiService{Client: client} + + response, result, err := svc.QueryCallback(ctx, cashcoupons.QueryCallbackRequest{ + Mchid: core.String(mchId), + }) + + if err != nil { + + bodyBytes, err := io.ReadAll(result.Response.Body) + if err != nil { + return nil, err + } + + if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil { + return nil, err + } + + return nil, fmt.Errorf("微信返回错误:%s", ErrBody.Message) + } + + if result.Response.StatusCode != CodeSuccess { + return nil, fmt.Errorf("微信返回错误tatus[%s]", result.Response.Status) + } + + return response, nil +} + func (c *CpnRepoImpl) Notify(ctx context.Context) error { return nil } diff --git a/internal/server/http.go b/internal/server/http.go index 145488c..6e6073e 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -48,6 +48,7 @@ func NewHTTPServer( v1.POST("/product/query", voucherService.CmbProductQuery) v1.POST("/decryptBody", voucherService.DecryptBody) + v1.POST("/queryWechatVoucherNotifyUrl", voucherService.QueryWechatVoucherNotifyUrl) return srv } diff --git a/internal/service/cmb_mock.go b/internal/service/cmb_mock.go index 1bac5bd..83e333e 100644 --- a/internal/service/cmb_mock.go +++ b/internal/service/cmb_mock.go @@ -104,3 +104,25 @@ func (s *VoucherService) DecryptBody(ctx http.Context) error { DecryptBody: decryptBody, }) } + +func (s *VoucherService) QueryWechatVoucherNotifyUrl(ctx http.Context) error { + + bodyBytes, err := io.ReadAll(ctx.Request().Body) + if err != nil { + return err + } + + var req *v1.QueryWechatVoucherNotifyUrlRequest + if err = json.Unmarshal(bodyBytes, &req); err != nil { + return err + } + + rep, err := s.VoucherBiz.WechatCpnRepo.QueryCallback(ctx, req.MchId) + if err != nil { + return err + } + + return ctx.JSON(200, &v1.QueryWechatVoucherNotifyUrlReply{ + Url: *rep.NotifyUrl, + }) +} diff --git a/internal/service/voucher.go b/internal/service/voucher.go index cb763cb..4b87215 100644 --- a/internal/service/voucher.go +++ b/internal/service/voucher.go @@ -3,23 +3,27 @@ package service import ( "voucher/internal/biz" "voucher/internal/biz/mixrepos" + "voucher/internal/biz/wechatrepo" "voucher/internal/conf" ) type VoucherService struct { - bc *conf.Bootstrap - VoucherBiz *biz.VoucherBiz - CmbMixRepo mixrepos.CmbMixRepo + bc *conf.Bootstrap + VoucherBiz *biz.VoucherBiz + CmbMixRepo mixrepos.CmbMixRepo + WechatCpnRepo wechatrepo.WechatCpnRepo } func NewVoucherService( bc *conf.Bootstrap, VoucherBiz *biz.VoucherBiz, CmbMixRepo mixrepos.CmbMixRepo, + WechatCpnRepo wechatrepo.WechatCpnRepo, ) *VoucherService { return &VoucherService{ - bc: bc, - VoucherBiz: VoucherBiz, - CmbMixRepo: CmbMixRepo, + bc: bc, + VoucherBiz: VoucherBiz, + CmbMixRepo: CmbMixRepo, + WechatCpnRepo: WechatCpnRepo, } }