支持微信支付宝独立支付渠道
This commit is contained in:
@@ -50,9 +50,7 @@ func (r *Repository) ExportBackup(ctx context.Context, actorID uint64, meta Audi
|
||||
return backup, nil
|
||||
}
|
||||
|
||||
// ImportBackup 导入支付配置备份,按 provider + merchant_id 更新或新增。
|
||||
|
||||
// ImportBackup 导入支付配置备份,按 provider + merchant_id 更新或新增。
|
||||
// ImportBackup 导入支付配置备份,按 provider + merchant_id + pay_way 更新或新增。
|
||||
func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, actorID uint64, meta AuditMeta) (*ImportBackupResult, error) {
|
||||
if backup.Type != "payment_config_backup" || backup.Version <= 0 {
|
||||
return nil, ErrInvalidBackup
|
||||
@@ -61,21 +59,22 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
return nil, ErrEmptyBackup
|
||||
}
|
||||
|
||||
activeKey := backupActiveKey(backup.Configs)
|
||||
activeKeys := backupActiveKeys(backup.Configs)
|
||||
result := &ImportBackupResult{Total: len(backup.Configs)}
|
||||
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if activeKey != "" {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID); err != nil {
|
||||
for payWay := range activeKeys {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, cfg := range backup.Configs {
|
||||
req := importCreateRequest(cfg, activeKey)
|
||||
req := importCreateRequest(cfg, activeKeys)
|
||||
if err := r.validateCreateRequest(req); err != nil {
|
||||
return err
|
||||
}
|
||||
payWay := firstNonEmpty(req.PayWay, "ZFBZF")
|
||||
|
||||
encryptedSignKey, err := r.encryptor.Encrypt(req.SignKey)
|
||||
if err != nil {
|
||||
@@ -93,7 +92,7 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
"notify_key": encryptedNotifyKey,
|
||||
"notify_url": req.NotifyURL,
|
||||
"jump_url": req.JumpURL,
|
||||
"pay_way": firstNonEmpty(req.PayWay, "ZFBZF"),
|
||||
"pay_way": payWay,
|
||||
"jspay_flag": firstNonEmpty(req.JSPayFlag, "2"),
|
||||
"sign_type": firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
"extra_config": model.JSONMap(req.ExtraConfig),
|
||||
@@ -106,7 +105,7 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
|
||||
var existing model.PaymentMerchantConfig
|
||||
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("provider = ? AND merchant_id = ?", req.Provider, req.MerchantID).
|
||||
Where("provider = ? AND merchant_id = ? AND pay_way = ?", req.Provider, req.MerchantID, payWay).
|
||||
First(&existing).Error
|
||||
if err == nil {
|
||||
if err := tx.Model(&existing).Updates(updates).Error; err != nil {
|
||||
@@ -129,7 +128,7 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: firstNonEmpty(req.PayWay, "ZFBZF"),
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: model.JSONMap(req.ExtraConfig),
|
||||
@@ -158,23 +157,30 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FindActiveByProvider 查询提供商的所有激活配置
|
||||
|
||||
func backupActiveKey(configs []ConfigDTO) string {
|
||||
// backupActiveKeys 提取备份中每个支付方式的启用配置键。
|
||||
func backupActiveKeys(configs []ConfigDTO) map[string]string {
|
||||
keys := make(map[string]string)
|
||||
for _, cfg := range configs {
|
||||
payWay := firstNonEmpty(cfg.PayWay, "ZFBZF")
|
||||
if _, exists := keys[payWay]; exists {
|
||||
continue
|
||||
}
|
||||
if cfg.Status == "active" || cfg.IsDefault {
|
||||
return paymentConfigImportKey(cfg.Provider, cfg.MerchantID)
|
||||
keys[payWay] = paymentConfigImportKey(cfg.Provider, cfg.MerchantID, payWay)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return keys
|
||||
}
|
||||
|
||||
func importCreateRequest(cfg ConfigDTO, activeKey string) CreateRequest {
|
||||
// importCreateRequest 将备份 DTO 转为创建请求,并保留每个支付方式唯一启用配置。
|
||||
func importCreateRequest(cfg ConfigDTO, activeKeys map[string]string) CreateRequest {
|
||||
status := cfg.Status
|
||||
if status == "" {
|
||||
status = "disabled"
|
||||
}
|
||||
isActive := activeKey != "" && paymentConfigImportKey(cfg.Provider, cfg.MerchantID) == activeKey
|
||||
payWay := firstNonEmpty(cfg.PayWay, "ZFBZF")
|
||||
activeKey := activeKeys[payWay]
|
||||
isActive := activeKey != "" && paymentConfigImportKey(cfg.Provider, cfg.MerchantID, payWay) == activeKey
|
||||
if isActive {
|
||||
status = "active"
|
||||
} else if status == "active" {
|
||||
@@ -190,7 +196,7 @@ func importCreateRequest(cfg ConfigDTO, activeKey string) CreateRequest {
|
||||
NotifyKey: cfg.NotifyKey,
|
||||
NotifyURL: cfg.NotifyURL,
|
||||
JumpURL: cfg.JumpURL,
|
||||
PayWay: cfg.PayWay,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: cfg.JSPayFlag,
|
||||
SignType: cfg.SignType,
|
||||
ExtraConfig: cfg.ExtraConfig,
|
||||
@@ -201,6 +207,7 @@ func importCreateRequest(cfg ConfigDTO, activeKey string) CreateRequest {
|
||||
}
|
||||
}
|
||||
|
||||
func paymentConfigImportKey(provider string, merchantID string) string {
|
||||
return provider + ":" + merchantID
|
||||
// paymentConfigImportKey 生成导入去重键,同商户可按支付方式拆分为多条配置。
|
||||
func paymentConfigImportKey(provider string, merchantID string, payWay string) string {
|
||||
return provider + ":" + merchantID + ":" + payWay
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ var (
|
||||
ErrConfigNotFound = errors.New("payment config not found")
|
||||
ErrDuplicateDefault = errors.New("duplicate default config for provider")
|
||||
ErrInvalidProvider = errors.New("invalid payment provider")
|
||||
ErrInvalidPayWay = errors.New("invalid pay_way")
|
||||
ErrInvalidStatus = errors.New("invalid status")
|
||||
ErrInvalidBackup = errors.New("invalid payment config backup")
|
||||
ErrEmptyBackup = errors.New("payment config backup is empty")
|
||||
@@ -95,6 +96,7 @@ type UpdateRequest struct {
|
||||
// ListQuery 列表查询参数
|
||||
type ListQuery struct {
|
||||
Provider string `form:"provider"`
|
||||
PayWay string `form:"pay_way"`
|
||||
Status string `form:"status"`
|
||||
Environment string `form:"environment"`
|
||||
Page int `form:"page"`
|
||||
|
||||
@@ -24,6 +24,7 @@ func NewHandler(service *Service) *Handler {
|
||||
// @Summary 获取支付配置列表
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param provider query string false "支付服务商"
|
||||
// @Param pay_way query string false "支付方式"
|
||||
// @Param status query string false "状态"
|
||||
// @Param environment query string false "环境"
|
||||
// @Param page query int false "页码"
|
||||
@@ -141,7 +142,7 @@ func (h *Handler) ImportBackup(c *gin.Context) {
|
||||
if err == ErrInvalidBackup || err == ErrEmptyBackup || err == ErrInvalidProvider ||
|
||||
err == ErrNameRequired || err == ErrMerchantIDRequired || err == ErrGatewayURLRequired ||
|
||||
err == ErrSignKeyRequired || err == ErrNotifyKeyRequired || err == ErrNotifyURLRequired ||
|
||||
err == ErrInvalidSignType || err == ErrInvalidStatus || err == ErrAppIDRequired ||
|
||||
err == ErrInvalidSignType || err == ErrInvalidStatus || err == ErrInvalidPayWay || err == ErrAppIDRequired ||
|
||||
err == ErrSerialNoRequired || err == ErrTermNoRequired {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
@@ -180,7 +181,7 @@ func (h *Handler) Create(c *gin.Context) {
|
||||
config, err := h.service.Create(c.Request.Context(), req, adminID, auditMeta(c))
|
||||
if err == ErrNameRequired || err == ErrMerchantIDRequired || err == ErrGatewayURLRequired ||
|
||||
err == ErrSignKeyRequired || err == ErrNotifyKeyRequired || err == ErrInvalidProvider ||
|
||||
err == ErrNotifyURLRequired || err == ErrInvalidSignType || err == ErrAppIDRequired ||
|
||||
err == ErrNotifyURLRequired || err == ErrInvalidSignType || err == ErrInvalidPayWay || err == ErrAppIDRequired ||
|
||||
err == ErrInvalidStatus || err == ErrSerialNoRequired || err == ErrTermNoRequired {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
@@ -224,7 +225,7 @@ func (h *Handler) Update(c *gin.Context) {
|
||||
response.NotFound(c, "配置不存在")
|
||||
return
|
||||
}
|
||||
if err == ErrInvalidSignType || err == ErrInvalidStatus || err == ErrNotifyURLRequired {
|
||||
if err == ErrInvalidSignType || err == ErrInvalidStatus || err == ErrInvalidPayWay || err == ErrNotifyURLRequired {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -37,9 +37,10 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
if environment == "" {
|
||||
environment = "production"
|
||||
}
|
||||
payWay := firstNonEmpty(req.PayWay, "ZFBZF")
|
||||
isDefault := req.IsDefault
|
||||
if status == "active" {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
return err
|
||||
}
|
||||
isDefault = true
|
||||
@@ -56,7 +57,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: firstNonEmpty(req.PayWay, "ZFBZF"),
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: req.ExtraConfig,
|
||||
@@ -97,6 +98,9 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
if req.SignType != nil && *req.SignType != "" && !isValidSignType(*req.SignType) {
|
||||
return nil, ErrInvalidSignType
|
||||
}
|
||||
if req.PayWay != nil && !isValidPayWay(*req.PayWay) {
|
||||
return nil, ErrInvalidPayWay
|
||||
}
|
||||
if req.Status != nil && !isValidStatus(*req.Status) {
|
||||
return nil, ErrInvalidStatus
|
||||
}
|
||||
@@ -145,7 +149,7 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
updates["jump_url"] = *req.JumpURL
|
||||
}
|
||||
if req.PayWay != nil {
|
||||
updates["pay_way"] = *req.PayWay
|
||||
updates["pay_way"] = firstNonEmpty(*req.PayWay, "ZFBZF")
|
||||
}
|
||||
if req.JSPayFlag != nil {
|
||||
updates["jspay_flag"] = *req.JSPayFlag
|
||||
@@ -174,8 +178,15 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
if req.Status != nil {
|
||||
finalStatus = *req.Status
|
||||
}
|
||||
finalPayWay := item.PayWay
|
||||
if req.PayWay != nil && *req.PayWay != "" {
|
||||
finalPayWay = *req.PayWay
|
||||
}
|
||||
if finalPayWay == "" {
|
||||
finalPayWay = "ZFBZF"
|
||||
}
|
||||
if finalStatus == "active" {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID, finalPayWay); err != nil {
|
||||
return err
|
||||
}
|
||||
updates["status"] = "active"
|
||||
@@ -225,8 +236,10 @@ func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta
|
||||
}
|
||||
|
||||
var usedCount int64
|
||||
if err := tx.Model(&model.PaymentOrder{}).
|
||||
Where("provider = ? AND merchant_id = ?", item.Provider, item.MerchantID).
|
||||
usedDB := tx.Model(&model.PaymentOrder{}).
|
||||
Where("payment_config_id = ?", item.ID).
|
||||
Or("(payment_config_id = ? OR payment_config_id IS NULL) AND provider = ? AND merchant_id = ? AND pay_way = ?", 0, item.Provider, item.MerchantID, item.PayWay)
|
||||
if err := usedDB.
|
||||
Count(&usedCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -246,12 +259,10 @@ func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta
|
||||
})
|
||||
}
|
||||
|
||||
// IncrementUsage 增加使用统计
|
||||
|
||||
// deactivateOtherConfigs 保证全局同一时间只有一个启用配置。
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64) error {
|
||||
// deactivateOtherConfigs 保证同一个支付方式同一时间只有一个启用配置。
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64, payWay string) error {
|
||||
db := tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("status = ? OR is_default = ?", "active", true)
|
||||
Where("pay_way = ? AND (status = ? OR is_default = ?)", payWay, "active", true)
|
||||
if activeID > 0 {
|
||||
db = db.Where("id != ?", activeID)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
if query.Provider != "" {
|
||||
db = db.Where("provider = ?", query.Provider)
|
||||
}
|
||||
if query.PayWay != "" {
|
||||
db = db.Where("pay_way = ?", query.PayWay)
|
||||
}
|
||||
if query.Status != "" {
|
||||
db = db.Where("status = ?", query.Status)
|
||||
}
|
||||
@@ -46,7 +49,7 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
if err := db.Order("is_default DESC, id DESC").Offset(offset).Limit(pageSize).Find(&items).Error; err != nil {
|
||||
if err := db.Order("pay_way ASC, is_default DESC, id DESC").Offset(offset).Limit(pageSize).Find(&items).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -62,8 +65,6 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
return dtos, total, nil
|
||||
}
|
||||
|
||||
// FindByID 根据 ID 查询配置
|
||||
|
||||
// FindByID 根据 ID 查询配置
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
@@ -89,9 +90,23 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindDefault 查询默认配置
|
||||
// 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 errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
dto, err := r.toDTO(item, includeSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindDefault 查询默认配置
|
||||
// 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 {
|
||||
@@ -103,12 +118,13 @@ func (r *Repository) FindDefault(ctx context.Context, provider string) (*model.P
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// FindDefaultAny 查询任意服务商的默认启用配置。
|
||||
|
||||
// FindDefaultAny 查询任意服务商的默认启用配置。
|
||||
func (r *Repository) FindDefaultAny(ctx context.Context, includeSecret bool) (*ConfigDTO, error) {
|
||||
// FindDefaultByPayWay 查询指定支付方式的默认启用配置。
|
||||
func (r *Repository) FindDefaultByPayWay(ctx context.Context, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("status = ?", "active").Order("is_default DESC, id DESC").First(&item).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("pay_way = ? AND status = ?", payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNoActiveConfigFound
|
||||
}
|
||||
@@ -121,8 +137,6 @@ func (r *Repository) FindDefaultAny(ctx context.Context, includeSecret bool) (*C
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindDefaultByProvider 查询指定服务商的默认启用配置。
|
||||
|
||||
// FindDefaultByProvider 查询指定服务商的默认启用配置。
|
||||
func (r *Repository) FindDefaultByProvider(ctx context.Context, provider string, includeSecret bool) (*ConfigDTO, error) {
|
||||
item, err := r.FindDefault(ctx, provider)
|
||||
@@ -136,9 +150,44 @@ func (r *Repository) FindDefaultByProvider(ctx context.Context, provider string,
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindByProviderMerchant 根据服务商和商户号查配置,用于历史支付单继续使用原商户密钥。
|
||||
// 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).
|
||||
Where("provider = ? AND pay_way = ? AND status = ?", provider, payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNoActiveConfigFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
dto, err := r.toDTO(item, includeSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindByProviderMerchant 根据服务商和商户号查配置,用于历史支付单继续使用原商户密钥。
|
||||
// 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).
|
||||
Order("status = 'active' DESC, is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
dto, err := r.toDTO(item, includeSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// 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).
|
||||
@@ -156,8 +205,6 @@ func (r *Repository) FindByProviderMerchant(ctx context.Context, provider string
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// ExportBackup 导出所有支付配置备份,包含解密后的密钥。
|
||||
|
||||
// FindActiveByProvider 查询提供商的所有激活配置
|
||||
func (r *Repository) FindActiveByProvider(ctx context.Context, provider string) ([]model.PaymentMerchantConfig, error) {
|
||||
var items []model.PaymentMerchantConfig
|
||||
|
||||
@@ -2,15 +2,17 @@ package paymentconfig
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestImportCreateRequestKeepsOnlyOneActiveConfig(t *testing.T) {
|
||||
func TestImportCreateRequestKeepsOneActiveConfigPerPayWay(t *testing.T) {
|
||||
configs := []ConfigDTO{
|
||||
{Provider: "lakala", MerchantID: "M1", Status: "active", IsDefault: true},
|
||||
{Provider: "leshua", MerchantID: "M2", Status: "active", IsDefault: false},
|
||||
{Provider: "lakala", MerchantID: "M1", PayWay: "ZFBZF", Status: "active", IsDefault: true},
|
||||
{Provider: "leshua", MerchantID: "M2", PayWay: "ZFBZF", Status: "active", IsDefault: false},
|
||||
{Provider: "leshua", MerchantID: "M3", PayWay: "WXZF", Status: "active", IsDefault: true},
|
||||
}
|
||||
activeKey := backupActiveKey(configs)
|
||||
activeKeys := backupActiveKeys(configs)
|
||||
|
||||
first := importCreateRequest(configs[0], activeKey)
|
||||
second := importCreateRequest(configs[1], activeKey)
|
||||
first := importCreateRequest(configs[0], activeKeys)
|
||||
second := importCreateRequest(configs[1], activeKeys)
|
||||
third := importCreateRequest(configs[2], activeKeys)
|
||||
|
||||
if first.Status != "active" || !first.IsDefault {
|
||||
t.Fatalf("first config status/default = %s/%v, want active/true", first.Status, first.IsDefault)
|
||||
@@ -18,6 +20,9 @@ func TestImportCreateRequestKeepsOnlyOneActiveConfig(t *testing.T) {
|
||||
if second.Status != "disabled" || second.IsDefault {
|
||||
t.Fatalf("second config status/default = %s/%v, want disabled/false", second.Status, second.IsDefault)
|
||||
}
|
||||
if third.Status != "active" || !third.IsDefault {
|
||||
t.Fatalf("third config status/default = %s/%v, want active/true", third.Status, third.IsDefault)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportCreateRequestPreservesTestingConfig(t *testing.T) {
|
||||
@@ -27,7 +32,7 @@ func TestImportCreateRequestPreservesTestingConfig(t *testing.T) {
|
||||
Status: "testing",
|
||||
}
|
||||
|
||||
req := importCreateRequest(cfg, "")
|
||||
req := importCreateRequest(cfg, nil)
|
||||
|
||||
if req.Status != "testing" || req.IsDefault {
|
||||
t.Fatalf("status/default = %s/%v, want testing/false", req.Status, req.IsDefault)
|
||||
|
||||
@@ -64,7 +64,7 @@ func (s *Service) Delete(ctx context.Context, id uint64, actorID uint64, meta Au
|
||||
return s.repo.Delete(ctx, id, actorID, meta)
|
||||
}
|
||||
|
||||
// GetDefaultConfig 获取默认配置(用于支付模块调用)
|
||||
// GetDefaultConfig 获取指定服务商的默认配置,保留给管理工具或兼容调用使用。
|
||||
func (s *Service) GetDefaultConfig(ctx context.Context, provider string) (*ConfigDTO, error) {
|
||||
config, err := s.repo.FindDefault(ctx, provider)
|
||||
if err != nil {
|
||||
@@ -76,3 +76,8 @@ func (s *Service) GetDefaultConfig(ctx context.Context, provider string) (*Confi
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// GetDefaultConfigByPayWay 获取指定支付方式的默认配置,供新支付链路按微信/支付宝选渠道。
|
||||
func (s *Service) GetDefaultConfigByPayWay(ctx context.Context, payWay string) (*ConfigDTO, error) {
|
||||
return s.repo.FindDefaultByPayWay(ctx, payWay, true)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ func (r *Repository) validateCreateRequest(req CreateRequest) error {
|
||||
if req.SignType != "" && !isValidSignType(req.SignType) {
|
||||
return ErrInvalidSignType
|
||||
}
|
||||
if req.PayWay != "" && !isValidPayWay(req.PayWay) {
|
||||
return ErrInvalidPayWay
|
||||
}
|
||||
if req.Status != "" && !isValidStatus(req.Status) {
|
||||
return ErrInvalidStatus
|
||||
}
|
||||
@@ -71,6 +74,11 @@ func isValidSignType(value string) bool {
|
||||
return value == "MD5" || value == "SHA256withRSA"
|
||||
}
|
||||
|
||||
// isValidPayWay 校验支付方式是否属于系统支持的微信或支付宝编码。
|
||||
func isValidPayWay(value string) bool {
|
||||
return value == "ZFBZF" || value == "WXZF"
|
||||
}
|
||||
|
||||
func isValidStatus(value string) bool {
|
||||
return value == "active" || value == "disabled" || value == "testing"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user