Skip to content

Commit

Permalink
refactor(order): 调整payment支付接口API,修复订单模块受影响的代码和测试 (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
longyue0521 authored Apr 26, 2024
1 parent 965bf87 commit be8680f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
12 changes: 6 additions & 6 deletions internal/order/internal/integration/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ func (s *OrderModuleTestSuite) getPaymentMockService() *paymentmocks.MockService
return r, nil
}).AnyTimes()

paymentSvc.EXPECT().PayByOrderID(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, oid int64) (payment.Payment, error) {
if oid == 11212 {
paymentSvc.EXPECT().PayByID(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, pmtID int64) (payment.Payment, error) {
if pmtID == 212 {
return payment.Payment{
ID: 212,
SN: "paymentSN-repay-212",
OrderID: oid,
OrderID: 11212,
OrderSN: fmt.Sprintf("orderSN-repay-11%d", 212),
TotalAmount: 9990,
Records: []payment.Record{
Expand All @@ -212,11 +212,11 @@ func (s *OrderModuleTestSuite) getPaymentMockService() *paymentmocks.MockService
},
},
}, nil
} else if oid == 11213 {
} else if pmtID == 213 {
return payment.Payment{
ID: 213,
SN: "paymentSN-repay-213",
OrderID: oid,
OrderID: 11213,
OrderSN: fmt.Sprintf("orderSN-repay-11%d", 213),
TotalAmount: 9990,
Records: []payment.Record{
Expand All @@ -229,7 +229,7 @@ func (s *OrderModuleTestSuite) getPaymentMockService() *paymentmocks.MockService
},
}, nil
}
r, ok := tables[oid]
r, ok := tables[pmtID]
if !ok {
return payment.Payment{}, fmt.Errorf(fmt.Sprintf("未配置的支付id=%d", id))
}
Expand Down
10 changes: 5 additions & 5 deletions internal/order/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (h *Handler) CreateOrder(ctx *ginx.Context, req CreateOrderReq, sess sessio
return systemErrorResult, err
}

wechatCodeURL, err := h.processPaymentForOrder(ctx.Request.Context(), order.ID)
wechatCodeURL, err := h.processPaymentForOrder(ctx.Request.Context(), p.ID)
if err != nil {
return systemErrorResult, err
}
Expand Down Expand Up @@ -253,10 +253,10 @@ func (h *Handler) createPayment(ctx context.Context, order domain.Order, payment
})
}

func (h *Handler) processPaymentForOrder(ctx context.Context, oid int64) (string, error) {
p, err := h.paymentSvc.PayByOrderID(ctx, oid)
func (h *Handler) processPaymentForOrder(ctx context.Context, pmtID int64) (string, error) {
p, err := h.paymentSvc.PayByID(ctx, pmtID)
if err != nil {
return "", fmt.Errorf("执行支付失败: %w, oid: %d", err, oid)
return "", fmt.Errorf("执行支付失败: %w, pmtID: %d", err, pmtID)
}
// 微信支付需要返回二维码URL
var wechatCodeURL string
Expand All @@ -281,7 +281,7 @@ func (h *Handler) RepayOrder(ctx *ginx.Context, req OrderSNReq, sess session.Ses
return systemErrorResult, fmt.Errorf("订单状态非法: %w, uid: %d, sn: %s", err, uid, req.SN)
}

wechatCodeURL, err := h.processPaymentForOrder(ctx.Request.Context(), order.ID)
wechatCodeURL, err := h.processPaymentForOrder(ctx.Request.Context(), order.Payment.ID)
if err != nil {
return systemErrorResult, fmt.Errorf("执行支付失败: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/payment/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Service interface {
CreatePayment(ctx context.Context, payment domain.Payment) (domain.Payment, error)
GetPaymentChannels(ctx context.Context) []domain.PaymentChannel
FindPaymentByID(ctx context.Context, pmtID int64) (domain.Payment, error)
PayByOrderID(ctx context.Context, oid int64) (domain.Payment, error)
PayByID(ctx context.Context, pmtID int64) (domain.Payment, error)
}

func NewService(wechatSvc *wechat.NativePaymentService,
Expand Down Expand Up @@ -130,8 +130,8 @@ func (s *service) FindPaymentByID(ctx context.Context, pmtID int64) (domain.Paym
return s.repo.FindPaymentByID(ctx, pmtID)
}

// PayByOrderID 通过订单序ID支付,查找并执行支付计划
func (s *service) PayByOrderID(ctx context.Context, oid int64) (domain.Payment, error) {
// PayByID 通过支付主记录ID支付,查找并执行支付计划
func (s *service) PayByID(ctx context.Context, pmtID int64) (domain.Payment, error) {
// 幂等
return domain.Payment{}, nil
}
32 changes: 16 additions & 16 deletions internal/payment/mocks/payment.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be8680f

Please sign in to comment.