feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -13,10 +14,11 @@ import (
|
||||
|
||||
// ExportBackup 导出所有支付配置备份,包含解密后的密钥。
|
||||
func (r *Repository) ExportBackup(ctx context.Context, actorID uint64, meta AuditMeta) (*ExportBackup, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var backup *ExportBackup
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var items []model.PaymentMerchantConfig
|
||||
if err := tx.Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx, ctx).Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -42,6 +44,7 @@ func (r *Repository) ExportBackup(ctx context.Context, actorID uint64, meta Audi
|
||||
return appendAuditLog(tx, actorID, "payment_config.export", 0, meta, map[string]any{
|
||||
"total": len(configs),
|
||||
"exported_at": exportedAt,
|
||||
"tenant_id": tenantID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
@@ -61,10 +64,11 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
|
||||
activeKeys := backupActiveKeys(backup.Configs)
|
||||
result := &ImportBackupResult{Total: len(backup.Configs)}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for payWay := range activeKeys {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -104,7 +108,7 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
}
|
||||
|
||||
var existing model.PaymentMerchantConfig
|
||||
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
err = applyOwnerTenantScope(tx.Clauses(clause.Locking{Strength: "UPDATE"}), ctx).
|
||||
Where("provider = ? AND merchant_id = ? AND pay_way = ?", req.Provider, req.MerchantID, payWay).
|
||||
First(&existing).Error
|
||||
if err == nil {
|
||||
@@ -120,24 +124,25 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
|
||||
createdBy := actorID
|
||||
item := model.PaymentMerchantConfig{
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: model.JSONMap(req.ExtraConfig),
|
||||
IsDefault: req.IsDefault,
|
||||
Status: firstNonEmpty(req.Status, "active"),
|
||||
Environment: firstNonEmpty(req.Environment, "production"),
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &createdBy,
|
||||
UpdatedBy: &actorID,
|
||||
OwnerTenantID: tenantID,
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: model.JSONMap(req.ExtraConfig),
|
||||
IsDefault: req.IsDefault,
|
||||
Status: firstNonEmpty(req.Status, "active"),
|
||||
Environment: firstNonEmpty(req.Environment, "production"),
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &createdBy,
|
||||
UpdatedBy: &actorID,
|
||||
}
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
return err
|
||||
@@ -146,9 +151,10 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
}
|
||||
|
||||
return appendAuditLog(tx, actorID, "payment_config.import", 0, meta, map[string]any{
|
||||
"total": result.Total,
|
||||
"created": result.Created,
|
||||
"updated": result.Updated,
|
||||
"total": result.Total,
|
||||
"created": result.Created,
|
||||
"updated": result.Updated,
|
||||
"tenant_id": tenantID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -30,6 +30,7 @@ var (
|
||||
// DTO 数据传输对象
|
||||
type ConfigDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
OwnerTenantID uint64 `json:"owner_tenant_id"`
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider"`
|
||||
MerchantID string `json:"merchant_id"`
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
// Create 创建配置
|
||||
func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
req = normalizeCreateRequest(req)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
// 验证必填字段
|
||||
if err := r.validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
@@ -42,7 +44,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
payWay := firstNonEmpty(req.PayWay, "ZFBZF")
|
||||
isDefault := req.IsDefault
|
||||
if status == "active" {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
isDefault = true
|
||||
@@ -51,24 +53,25 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
}
|
||||
|
||||
item := model.PaymentMerchantConfig{
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: req.ExtraConfig,
|
||||
IsDefault: isDefault,
|
||||
Status: status,
|
||||
Environment: environment,
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &actorID,
|
||||
UpdatedBy: &actorID,
|
||||
OwnerTenantID: tenantID,
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: req.ExtraConfig,
|
||||
IsDefault: isDefault,
|
||||
Status: status,
|
||||
Environment: environment,
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &actorID,
|
||||
UpdatedBy: &actorID,
|
||||
}
|
||||
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
@@ -77,8 +80,9 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
|
||||
// 记录审计日志
|
||||
if err := appendAuditLog(tx, actorID, "payment_config.create", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
"owner_tenant_id": item.OwnerTenantID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,9 +114,10 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
}
|
||||
|
||||
var dto ConfigDTO
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
@@ -187,7 +192,7 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
finalPayWay = "ZFBZF"
|
||||
}
|
||||
if finalStatus == "active" {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID, finalPayWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID, finalPayWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
updates["status"] = "active"
|
||||
@@ -288,7 +293,7 @@ func trimExtraConfig(config map[string]any) map[string]any {
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta AuditMeta) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := tx.Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx.Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
@@ -320,8 +325,9 @@ func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta
|
||||
}
|
||||
|
||||
// deactivateOtherConfigs 保证同一个支付方式同一时间只有一个启用配置。
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64, payWay string) error {
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64, payWay string, tenantID uint64) error {
|
||||
db := tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("owner_tenant_id = ?", tenantID).
|
||||
Where("pay_way = ? AND (status = ? OR is_default = ?)", payWay, "active", true)
|
||||
if activeID > 0 {
|
||||
db = db.Where("id != ?", activeID)
|
||||
|
||||
@@ -9,25 +9,26 @@ import (
|
||||
// toDTO 转换为 DTO
|
||||
func (r *Repository) toDTO(item model.PaymentMerchantConfig, includeSecret bool) (ConfigDTO, error) {
|
||||
dto := ConfigDTO{
|
||||
ID: item.ID,
|
||||
Name: item.Name,
|
||||
Provider: item.Provider,
|
||||
MerchantID: item.MerchantID,
|
||||
GatewayURL: item.GatewayURL,
|
||||
NotifyURL: item.NotifyURL,
|
||||
JumpURL: item.JumpURL,
|
||||
PayWay: item.PayWay,
|
||||
JSPayFlag: item.JSPayFlag,
|
||||
SignType: item.SignType,
|
||||
ExtraConfig: item.ExtraConfig,
|
||||
IsDefault: item.IsDefault,
|
||||
Status: item.Status,
|
||||
Environment: item.Environment,
|
||||
BusinessTags: item.BusinessTags,
|
||||
CreatedBy: item.CreatedBy,
|
||||
UpdatedBy: item.UpdatedBy,
|
||||
CreatedAt: item.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: item.UpdatedAt.Format(time.RFC3339),
|
||||
ID: item.ID,
|
||||
OwnerTenantID: item.OwnerTenantID,
|
||||
Name: item.Name,
|
||||
Provider: item.Provider,
|
||||
MerchantID: item.MerchantID,
|
||||
GatewayURL: item.GatewayURL,
|
||||
NotifyURL: item.NotifyURL,
|
||||
JumpURL: item.JumpURL,
|
||||
PayWay: item.PayWay,
|
||||
JSPayFlag: item.JSPayFlag,
|
||||
SignType: item.SignType,
|
||||
ExtraConfig: item.ExtraConfig,
|
||||
IsDefault: item.IsDefault,
|
||||
Status: item.Status,
|
||||
Environment: item.Environment,
|
||||
BusinessTags: item.BusinessTags,
|
||||
CreatedBy: item.CreatedBy,
|
||||
UpdatedBy: item.UpdatedBy,
|
||||
CreatedAt: item.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: item.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
// 只有明确要求时才解密并返回密钥
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -14,7 +15,7 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
var items []model.PaymentMerchantConfig
|
||||
var total int64
|
||||
|
||||
db := r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{})
|
||||
db := applyOwnerTenantScope(r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{}), ctx)
|
||||
|
||||
// 过滤条件
|
||||
if query.Provider != "" {
|
||||
@@ -72,7 +73,7 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
// FindByID 根据 ID 查询配置
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
@@ -134,7 +135,7 @@ func (r *Repository) applyPaymentReferenceFlags(ctx context.Context, dtos []Conf
|
||||
// FindRuntimeByID 根据配置 ID 查询运行时配置,不写审计日志,供支付单回溯原配置使用。
|
||||
func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
@@ -150,7 +151,8 @@ func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecr
|
||||
// FindDefault 查询指定服务商的默认启用配置,兼容旧调用路径。
|
||||
func (r *Repository) FindDefault(ctx context.Context, provider string) (*model.PaymentMerchantConfig, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND is_default = ? AND status = ?", provider, true, "active").First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND is_default = ? AND status = ?", provider, true, "active").First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNoActiveConfigFound
|
||||
}
|
||||
@@ -162,7 +164,7 @@ func (r *Repository) FindDefault(ctx context.Context, provider string) (*model.P
|
||||
// FindDefaultByPayWay 查询指定支付方式的默认启用配置。
|
||||
func (r *Repository) FindDefaultByPayWay(ctx context.Context, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("pay_way = ? AND status = ?", payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
@@ -194,7 +196,7 @@ func (r *Repository) FindDefaultByProvider(ctx context.Context, provider string,
|
||||
// FindDefaultByProviderPayWay 查询指定服务商和支付方式的默认启用配置。
|
||||
func (r *Repository) FindDefaultByProviderPayWay(ctx context.Context, provider string, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND pay_way = ? AND status = ?", provider, payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
@@ -213,7 +215,8 @@ func (r *Repository) FindDefaultByProviderPayWay(ctx context.Context, provider s
|
||||
// FindByProviderMerchantPayWay 根据服务商、商户号和支付方式查配置,用于旧支付单回溯原商户密钥。
|
||||
func (r *Repository) FindByProviderMerchantPayWay(ctx context.Context, provider string, merchantID string, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND merchant_id = ? AND pay_way = ?", provider, merchantID, payWay).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND merchant_id = ? AND pay_way = ?", provider, merchantID, payWay).
|
||||
Order("status = 'active' DESC, is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -231,7 +234,8 @@ func (r *Repository) FindByProviderMerchantPayWay(ctx context.Context, provider
|
||||
// FindByProviderMerchant 根据服务商和商户号查配置,保留给历史数据没有支付方式时兜底使用。
|
||||
func (r *Repository) FindByProviderMerchant(ctx context.Context, provider string, merchantID string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND merchant_id = ?", provider, merchantID).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND merchant_id = ?", provider, merchantID).
|
||||
Order("status = 'active' DESC, is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -249,10 +253,18 @@ func (r *Repository) FindByProviderMerchant(ctx context.Context, provider string
|
||||
// FindActiveByProvider 查询提供商的所有激活配置
|
||||
func (r *Repository) FindActiveByProvider(ctx context.Context, provider string) ([]model.PaymentMerchantConfig, error) {
|
||||
var items []model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND status = ?", provider, "active").Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND status = ?", provider, "active").Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func applyOwnerTenantScope(db *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
return db.Where("owner_tenant_id = ?", tenantID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// Create 创建配置
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package paymentconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func paymentConfigTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestPaymentConfigsAndDefaultsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
repo := NewRepository(db, &MockEncryptor{})
|
||||
dachuiCtx := paymentConfigTenantContext(101)
|
||||
jieyouCtx := paymentConfigTenantContext(202)
|
||||
|
||||
dachuiConfig, err := repo.Create(dachuiCtx, CreateRequest{
|
||||
Name: "大锤支付宝",
|
||||
Provider: "mock",
|
||||
MerchantID: "DACHUI-MOCK",
|
||||
PayWay: "ZFBZF",
|
||||
Status: "active",
|
||||
}, 1, AuditMeta{})
|
||||
if err != nil {
|
||||
t.Fatalf("create dachui config: %v", err)
|
||||
}
|
||||
jieyouConfig, err := repo.Create(jieyouCtx, CreateRequest{
|
||||
Name: "解忧支付宝",
|
||||
Provider: "mock",
|
||||
MerchantID: "JIEYOU-MOCK",
|
||||
PayWay: "ZFBZF",
|
||||
Status: "active",
|
||||
}, 2, AuditMeta{})
|
||||
if err != nil {
|
||||
t.Fatalf("create jieyou config: %v", err)
|
||||
}
|
||||
if dachuiConfig.OwnerTenantID != 101 || jieyouConfig.OwnerTenantID != 202 {
|
||||
t.Fatalf("config tenant ids = %d/%d", dachuiConfig.OwnerTenantID, jieyouConfig.OwnerTenantID)
|
||||
}
|
||||
|
||||
dachuiDefault, err := repo.FindDefaultByPayWay(dachuiCtx, "ZFBZF", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jieyouDefault, err := repo.FindDefaultByPayWay(jieyouCtx, "ZFBZF", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dachuiDefault.ID != dachuiConfig.ID || jieyouDefault.ID != jieyouConfig.ID {
|
||||
t.Fatalf("tenant defaults = %d/%d", dachuiDefault.ID, jieyouDefault.ID)
|
||||
}
|
||||
if dachuiDefault.MerchantID != "DACHUI-MOCK" || jieyouDefault.MerchantID != "JIEYOU-MOCK" {
|
||||
t.Fatalf("tenant merchants = %s/%s", dachuiDefault.MerchantID, jieyouDefault.MerchantID)
|
||||
}
|
||||
|
||||
if _, err := repo.FindByID(jieyouCtx, dachuiConfig.ID, false, 0, AuditMeta{}); !errors.Is(err, ErrConfigNotFound) {
|
||||
t.Fatalf("cross-tenant config lookup error = %v", err)
|
||||
}
|
||||
items, total, err := repo.List(dachuiCtx, ListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if total != 1 || len(items) != 1 || items[0].ID != dachuiConfig.ID {
|
||||
t.Fatalf("dachui config list = %+v, total=%d", items, total)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// IncrementUsage 增加使用统计
|
||||
func (r *Repository) IncrementUsage(ctx context.Context, id uint64, amountCent int64) error {
|
||||
now := time.Now()
|
||||
return r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{}).Where("id = ?", id).Updates(map[string]any{
|
||||
return applyOwnerTenantScope(r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{}), ctx).Where("id = ?", id).Updates(map[string]any{
|
||||
"total_transactions": gorm.Expr("total_transactions + ?", 1),
|
||||
"total_amount_cent": gorm.Expr("total_amount_cent + ?", amountCent),
|
||||
"last_used_at": now,
|
||||
@@ -23,7 +23,7 @@ func (r *Repository) IncrementUsage(ctx context.Context, id uint64, amountCent i
|
||||
// RecordUsage 记录支付配置命中情况,同一个支付单只记录一次。
|
||||
|
||||
// RecordUsage 记录支付配置命中情况,同一个支付单只记录一次。
|
||||
func (r *Repository) RecordUsage(ctx context.Context, configID uint64, paymentOrderID uint64, provider string, merchantID string, amountCent int64, bizType string) error {
|
||||
func (r *Repository) RecordUsage(ctx context.Context, tenantID uint64, configID uint64, paymentOrderID uint64, provider string, merchantID string, amountCent int64, bizType string) error {
|
||||
if configID == 0 || paymentOrderID == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -37,6 +37,7 @@ func (r *Repository) RecordUsage(ctx context.Context, configID uint64, paymentOr
|
||||
return err
|
||||
}
|
||||
log := model.PaymentConfigUsageLog{
|
||||
TenantID: tenantID,
|
||||
ConfigID: configID,
|
||||
PaymentOrderID: paymentOrderID,
|
||||
Provider: provider,
|
||||
@@ -48,11 +49,13 @@ func (r *Repository) RecordUsage(ctx context.Context, configID uint64, paymentOr
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
return tx.Model(&model.PaymentMerchantConfig{}).Where("id = ?", configID).Updates(map[string]any{
|
||||
"total_transactions": gorm.Expr("total_transactions + ?", 1),
|
||||
"total_amount_cent": gorm.Expr("total_amount_cent + ?", amountCent),
|
||||
"last_used_at": now,
|
||||
}).Error
|
||||
return tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("id = ? AND owner_tenant_id = ?", configID, tenantID).
|
||||
Updates(map[string]any{
|
||||
"total_transactions": gorm.Expr("total_transactions + ?", 1),
|
||||
"total_amount_cent": gorm.Expr("total_amount_cent + ?", amountCent),
|
||||
"last_used_at": now,
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user