From f51c470d6663a9ab34497c75d047692bcafcad90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Thu, 6 Mar 2025 14:46:05 +0800 Subject: [PATCH] cmb --- api/v1/cmb_cpn.proto | 4 +-- internal/biz/cmb.go | 44 ++++++++++++++++++++++++-------- internal/biz/cmb/consume.go | 19 +++++++++++--- internal/data/mixrepoimpl/cmb.go | 2 ++ internal/data/wechat_test.go | 44 ++++++++++++++++++++++++++++++++ internal/service/cmb.go | 2 ++ 6 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 internal/data/wechat_test.go diff --git a/api/v1/cmb_cpn.proto b/api/v1/cmb_cpn.proto index 8f4a8bf..55ba048 100644 --- a/api/v1/cmb_cpn.proto +++ b/api/v1/cmb_cpn.proto @@ -74,13 +74,13 @@ message CmbQueryProductReply { string activityName = 9 [json_name = "activityName"]; // 外部合作方权益批次号 string activityId = 10 [json_name = "activityId"]; - // 批次额度 单位为分 + // 面额,单位为分 string amount = 11 [json_name = "amount"]; // 门槛,单位为分 string minAmount = 12 [json_name = "minAmount"]; // 有效期形式,0:固定有效期,1:动态有效期 string availableType = 13 [json_name = "availableType"]; - // 动态有效期天数-非必填 格式yyyy-mm-dd hh:mm:ss.sss + // 动态有效期天数-非必填 string availableDays = 14 [json_name = "availableDays"]; // 有效期开始时间-非必填 string startTime = 15 [json_name = "startTime"]; diff --git a/internal/biz/cmb.go b/internal/biz/cmb.go index 7901194..17b2cfe 100644 --- a/internal/biz/cmb.go +++ b/internal/biz/cmb.go @@ -4,10 +4,12 @@ import ( "context" "errors" "fmt" + "github.com/wechatpay-apiv3/wechatpay-go/services/merchantexclusivecoupon" "gorm.io/gorm" "time" v1 "voucher/api/v1" "voucher/internal/biz/bo" + "voucher/internal/biz/vo" "voucher/internal/pkg/lock" ) @@ -63,18 +65,38 @@ func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (rep return err } - // todo reps = &v1.CmbQueryProductReply{ - ActivityName: product.Name, - ActivityId: product.ProductNo, - Amount: *wechatResp.GoodsName, - MinAmount: fmt.Sprintf("%d", *wechatResp.StockSendRule.MaxAmount), - AvailableType: *wechatResp.GoodsName, - AvailableDays: *wechatResp.GoodsName, - StartTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableBeginTime.Format(time.DateTime), - EndTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableEndTime.Format(time.DateTime), - //AvailableStock: *wechatResp.SendCountInformation.TotalSendNum, - Detail: *wechatResp.GoodsName, + ActivityName: product.Name, + ActivityId: product.ProductNo, + + StartTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableBeginTime.Format(time.DateTime), + EndTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableEndTime.Format(time.DateTime), + Detail: *wechatResp.DisplayPatternInfo.Description, + + Amount: "", + MinAmount: "", + + AvailableType: "", + AvailableDays: "", + AvailableStock: "", + } + + availableStock := *wechatResp.StockSendRule.MaxCoupons - *wechatResp.SendCountInformation.TotalSendNum + reps.AvailableStock = fmt.Sprintf("%d", availableStock) + + if wechatResp.CouponUseRule.CouponAvailableTime.AvailableDayAfterReceive != nil { + reps.AvailableType = vo.CmbAvailableTypeDynamic.GetValue() + reps.AvailableDays = fmt.Sprintf("%d", *wechatResp.CouponUseRule.CouponAvailableTime.AvailableDayAfterReceive) + } else { + reps.AvailableType = vo.CmbAvailableTypeFixed.GetValue() + } + + if *wechatResp.StockType == merchantexclusivecoupon.BUSIFAVORSTOCKTYPE_NORMAL { + reps.Amount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.DiscountAmount) + reps.MinAmount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.TransactionMinimum) + } else if *wechatResp.StockType == merchantexclusivecoupon.BUSIFAVORSTOCKTYPE_DISCOUNT { + reps.Amount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.DiscountCoupon.DiscountPercent) + reps.MinAmount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.TransactionMinimum) } return nil diff --git a/internal/biz/cmb/consume.go b/internal/biz/cmb/consume.go index b5c732b..17b8bac 100644 --- a/internal/biz/cmb/consume.go +++ b/internal/biz/cmb/consume.go @@ -13,7 +13,11 @@ import ( func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo string, err error) { if order.Status.IsWait() { - return "", fmt.Errorf("订单状态错误,%s", order.Status.GetText()) + return outRequestNo, fmt.Errorf("订单状态错误,%s", order.Status.GetText()) + } + + if !order.Channel.IsWeChat() { + return outRequestNo, fmt.Errorf("订单渠道错误,%s", order.Channel.GetText()) } if err = v.ing(ctx, order.ID); err != nil { @@ -25,13 +29,15 @@ func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo return } + // todo + // 注册“刚哥那边”回调中心tag,一个批次只能注册一个tag,微信立减金消费时根据不同的tag消费(区分测试/正式注册tag + couponId, err := v.WechatCpnRepo.Order(ctx, orderWechat) if err != nil { - return "", v.fail(ctx, order, orderWechat, err.Error()) + return outRequestNo, v.fail(ctx, order, orderWechat, err.Error()) } - err = v.success(ctx, order, orderWechat, couponId) - if err != nil { + if err = v.success(ctx, order, orderWechat, couponId); err != nil { return } @@ -63,20 +69,25 @@ func (v *Cmb) create(ctx context.Context, order *bo.OrderBo) (*bo.OrderWechatBo, } func (v *Cmb) ing(ctx context.Context, id uint64) error { + return v.OrderRepo.Ing(ctx, id) } func (v *Cmb) success(ctx context.Context, order *bo.OrderBo, orderWechat *bo.OrderWechatBo, couponId string) error { + if err := v.OrderWechatRepo.Success(ctx, orderWechat.ID, couponId); err != nil { return err } + return v.OrderRepo.Success(ctx, order.ID) } func (v *Cmb) fail(ctx context.Context, order *bo.OrderBo, orderWechat *bo.OrderWechatBo, remarks string) error { + if err := v.OrderWechatRepo.Fail(ctx, orderWechat.ID, remarks); err != nil { return err } + return v.OrderRepo.Fail(ctx, order.ID) } diff --git a/internal/data/mixrepoimpl/cmb.go b/internal/data/mixrepoimpl/cmb.go index fb3522c..1e25a31 100644 --- a/internal/data/mixrepoimpl/cmb.go +++ b/internal/data/mixrepoimpl/cmb.go @@ -95,6 +95,7 @@ func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) ( if err != nil { return nil, err } + req.Sign = sing return req, nil @@ -128,6 +129,7 @@ func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo) if err != nil { return nil, err } + reply.Sign = sign return reply, nil diff --git a/internal/data/wechat_test.go b/internal/data/wechat_test.go new file mode 100644 index 0000000..1549a08 --- /dev/null +++ b/internal/data/wechat_test.go @@ -0,0 +1,44 @@ +package data + +import ( + "context" + "fmt" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/services/merchantexclusivecoupon" + "testing" +) + +func TestGetClient(t *testing.T) { + server := &Server{ + MchID: "", + MchCertificateSerialNumber: "", + } + stockId := "" + + ctx := context.Background() + + client, err := GetClient(ctx, server) + if err != nil { + t.Error(err) + return + } + + svc := merchantexclusivecoupon.BusiFavorApiService{Client: client} + + response, result, err := svc.QueryStock(ctx, merchantexclusivecoupon.QueryStockRequest{ + StockId: core.String(stockId), + }) + + if err != nil { + t.Error(err) + return + } + + if result.Response.StatusCode != 200 { + err = fmt.Errorf("查询活动微信返回错误StatusCode[%d]Status[%s]", result.Response.StatusCode, result.Response.Status) + t.Error(err) + return + } + + t.Log(response) +} diff --git a/internal/service/cmb.go b/internal/service/cmb.go index d0fa1a5..77263f8 100644 --- a/internal/service/cmb.go +++ b/internal/service/cmb.go @@ -70,6 +70,7 @@ func (s *VoucherService) cmbOrder(ctx http.Context) (*v1.CmbOrderReply, error) { } func (s *VoucherService) CmbProductQuery(ctx http.Context) error { + var ( reply *v1.CmbReply err error @@ -97,6 +98,7 @@ func (s *VoucherService) CmbProductQuery(ctx http.Context) error { } func (s *VoucherService) cmbProductQuery(ctx http.Context) (*v1.CmbQueryProductReply, error) { + var req *v1.CmbRequest if err := ctx.BindForm(&req); err != nil { return nil, err