优化结构
This commit is contained in:
@@ -120,7 +120,7 @@ func (uc *IntegrationConfigUsecase) Save(ctx context.Context, config *Integratio
|
||||
return err
|
||||
}
|
||||
if definition, ok := IntegrationDefinition(config.Kind, config.Provider); ok {
|
||||
values = mergeIntegrationDefaults(definition.Defaults, values)
|
||||
values = MergeIntegrationDefaults(definition.Defaults, values)
|
||||
if config.Enabled {
|
||||
if err := ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
|
||||
return err
|
||||
@@ -153,7 +153,7 @@ func (uc *IntegrationConfigUsecase) Test(ctx context.Context, config *Integratio
|
||||
return err
|
||||
}
|
||||
if definition, ok := IntegrationDefinition(config.Kind, config.Provider); ok {
|
||||
values = mergeIntegrationDefaults(definition.Defaults, values)
|
||||
values = MergeIntegrationDefaults(definition.Defaults, values)
|
||||
}
|
||||
if err := ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
|
||||
return err
|
||||
@@ -209,11 +209,7 @@ func DefaultIntegrationConfig(kind, provider string) map[string]any {
|
||||
if !ok {
|
||||
return map[string]any{}
|
||||
}
|
||||
return mergeIntegrationDefaults(definition.Defaults, nil)
|
||||
}
|
||||
|
||||
func mergeIntegrationDefaults(defaults, values map[string]any) map[string]any {
|
||||
return MergeIntegrationDefaults(defaults, values)
|
||||
return MergeIntegrationDefaults(definition.Defaults, nil)
|
||||
}
|
||||
|
||||
// MergeIntegrationDefaults returns a new map with stored values overriding defaults.
|
||||
|
||||
@@ -89,7 +89,7 @@ var genericPaymentDefaults = map[string]any{
|
||||
}
|
||||
|
||||
func genericPaymentDefinition(provider, name, description string) IntegrationConfigDefinition {
|
||||
defaults := mergeIntegrationDefaults(genericPaymentDefaults, nil)
|
||||
defaults := MergeIntegrationDefaults(genericPaymentDefaults, nil)
|
||||
fields := append([]IntegrationConfigField(nil), genericPaymentFields...)
|
||||
return paymentDefinition(provider, name, description, defaults, fields...)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -96,7 +96,7 @@ func (uc *MediaUsecase) InitUpload(ctx context.Context, userID uint, name, hash
|
||||
completed, err := uc.FindCompletedSession(ctx, userID, strings.ToLower(hash))
|
||||
if err == nil && completed.MediaID != 0 {
|
||||
if media, findErr := uc.FindMedia(ctx, completed.MediaID); findErr == nil {
|
||||
copy := &MediaFile{Name: name, URL: media.URL, Tag: media.Tag, Key: media.Key}
|
||||
copy := &MediaFile{Name: name, URL: media.URL, Tag: media.Tag, Key: media.Key, Size: media.Size, MD5: media.MD5, Mime: media.Mime, UserID: userID}
|
||||
if createErr := uc.CreateMedia(ctx, copy); createErr == nil {
|
||||
return nil, copy, nil, nil
|
||||
}
|
||||
@@ -249,7 +249,10 @@ func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uin
|
||||
if err = uc.CreateMedia(ctx, media); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
_ = uc.CompleteUploadSession(ctx, uploadID, key, media.ID)
|
||||
if err = uc.CompleteUploadSession(ctx, uploadID, key, media.ID); err != nil {
|
||||
_ = uc.FailUploadSession(ctx, uploadID)
|
||||
return nil, fmt.Errorf("完成上传会话失败: %w", err)
|
||||
}
|
||||
_ = uc.DeleteChunks(ctx, uploadID)
|
||||
_ = uc.files.DeletePrefix(ctx, uc.chunkPrefix(uploadID))
|
||||
return media, nil
|
||||
|
||||
Reference in New Issue
Block a user