支持微信支付宝独立支付渠道
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user