优化结构
This commit is contained in:
@@ -349,15 +349,6 @@ func (r *PaymentFulfillmentRegistry) Handler(kind string) PaymentFulfillmentHand
|
||||
return r.handlers[kind]
|
||||
}
|
||||
|
||||
func (r *PaymentFulfillmentRegistry) Len() int {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
return len(r.handlers)
|
||||
}
|
||||
|
||||
type PaymentUsecase struct {
|
||||
repo PaymentRepo
|
||||
orders PaymentOrderRepo
|
||||
@@ -387,12 +378,6 @@ func NewPaymentUsecase(repo PaymentRepo, orders PaymentOrderRepo, hooks PaymentH
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfiguredPaymentUsecase(repo PaymentRepo, orders PaymentOrderRepo, hooks PaymentHooks, sources *PaymentOrderSourceRegistry, fulfillments *PaymentFulfillmentRegistry, appLogger *slog.Logger) (*PaymentUsecase, error) {
|
||||
// Business modules are optional in the template. Missing modules are
|
||||
// reported when the corresponding payment operation is invoked.
|
||||
return NewPaymentUsecase(repo, orders, hooks, sources, fulfillments, appLogger), nil
|
||||
}
|
||||
|
||||
func (uc *PaymentUsecase) RegisterBusinessModule(module PaymentBusinessModule) error {
|
||||
if module == nil {
|
||||
return errors.New("支付业务模块为空")
|
||||
@@ -464,14 +449,11 @@ func (uc *PaymentUsecase) Create(ctx context.Context, req *PaymentRequest) (*Pay
|
||||
if req.OriginalAmount < req.Amount {
|
||||
return nil, errors.New("原始金额不能小于支付订单金额")
|
||||
}
|
||||
canonicalProvider, canonicalTradeNo := req.Provider, req.TradeNo
|
||||
canonicalBusinessType, canonicalBusinessID := req.BusinessType, req.BusinessID
|
||||
canonicalSubject, canonicalCurrency, canonicalAmount := req.Subject, req.Currency, req.Amount
|
||||
canonicalOriginalAmount, canonicalPaymentMode := req.OriginalAmount, req.PaymentMode
|
||||
canonicalExtra, err := json.Marshal(req.Extra)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("支付扩展参数无效: %w", err)
|
||||
}
|
||||
canonicalFingerprint := paymentOrderFingerprint(req, canonicalExtra)
|
||||
if err := uc.hooks.BeforeCreate(ctx, req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -479,7 +461,7 @@ func (uc *PaymentUsecase) Create(ctx context.Context, req *PaymentRequest) (*Pay
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("支付扩展参数无效: %w", err)
|
||||
}
|
||||
if req.Provider != canonicalProvider || req.TradeNo != canonicalTradeNo || req.BusinessType != canonicalBusinessType || req.BusinessID != canonicalBusinessID || req.Subject != canonicalSubject || req.Currency != canonicalCurrency || req.Amount != canonicalAmount || req.OriginalAmount != canonicalOriginalAmount || req.PaymentMode != canonicalPaymentMode || string(currentExtra) != string(canonicalExtra) {
|
||||
if paymentOrderFingerprint(req, currentExtra) != canonicalFingerprint {
|
||||
return nil, ErrPaymentOrderConflict
|
||||
}
|
||||
return uc.createWithOrder(ctx, req)
|
||||
@@ -926,10 +908,7 @@ func (uc *PaymentUsecase) refundWithOrder(ctx context.Context, provider, tradeNo
|
||||
return nil, err
|
||||
}
|
||||
if !accepted {
|
||||
if providerErr != nil {
|
||||
return nil, providerErr
|
||||
}
|
||||
return nil, errors.New(message)
|
||||
return nil, providerErr
|
||||
}
|
||||
result.Status = "refund_pending"
|
||||
return attachOrderResult(result, order), nil
|
||||
|
||||
@@ -182,15 +182,6 @@ func (r *PaymentOrderSourceRegistry) Source(kind string) PaymentOrderSource {
|
||||
return r.sources[kind]
|
||||
}
|
||||
|
||||
func (r *PaymentOrderSourceRegistry) Len() int {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
return len(r.sources)
|
||||
}
|
||||
|
||||
func (r *PaymentOrderSourceRegistry) Unregister(kind string) {
|
||||
if r == nil {
|
||||
return
|
||||
|
||||
@@ -9,5 +9,5 @@ var ProviderSet = wire.NewSet(
|
||||
NewPaymentHooks,
|
||||
NewPaymentOrderSourceRegistry,
|
||||
NewPaymentFulfillmentRegistry,
|
||||
NewConfiguredPaymentUsecase,
|
||||
NewPaymentUsecase,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user