支持后台支付配置管理
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package paymentconfig
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConfigNotFound = errors.New("payment config not found")
|
||||
ErrDuplicateDefault = errors.New("duplicate default config for provider")
|
||||
ErrInvalidProvider = errors.New("invalid payment provider")
|
||||
ErrInvalidStatus = errors.New("invalid status")
|
||||
ErrCannotDeleteInUse = errors.New("cannot delete config in use")
|
||||
ErrEncryptionFailed = errors.New("encryption failed")
|
||||
ErrDecryptionFailed = errors.New("decryption failed")
|
||||
ErrNoActiveConfigFound = errors.New("no active config found for provider")
|
||||
ErrMerchantIDRequired = errors.New("merchant_id is required")
|
||||
ErrNameRequired = errors.New("name is required")
|
||||
ErrGatewayURLRequired = errors.New("gateway_url is required for leshua provider")
|
||||
ErrSignKeyRequired = errors.New("sign_key is required for leshua provider")
|
||||
ErrNotifyKeyRequired = errors.New("notify_key is required for leshua provider")
|
||||
ErrNotifyURLRequired = errors.New("notify_url is required for leshua provider")
|
||||
ErrInvalidSignType = errors.New("invalid sign_type")
|
||||
)
|
||||
|
||||
// DTO 数据传输对象
|
||||
type ConfigDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider"`
|
||||
MerchantID string `json:"merchant_id"`
|
||||
GatewayURL string `json:"gateway_url"`
|
||||
SignKey string `json:"sign_key,omitempty"` // 默认不返回
|
||||
NotifyKey string `json:"notify_key,omitempty"` // 默认不返回
|
||||
NotifyURL string `json:"notify_url"`
|
||||
JumpURL string `json:"jump_url"`
|
||||
PayWay string `json:"pay_way"`
|
||||
JSPayFlag string `json:"jspay_flag"`
|
||||
SignType string `json:"sign_type"`
|
||||
ExtraConfig map[string]any `json:"extra_config"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
Status string `json:"status"`
|
||||
Environment string `json:"environment"`
|
||||
BusinessTags []string `json:"business_tags"`
|
||||
TotalTransactions int64 `json:"total_transactions"`
|
||||
TotalAmountCent int64 `json:"total_amount_cent"`
|
||||
LastUsedAt *string `json:"last_used_at"`
|
||||
CreatedBy *uint64 `json:"created_by"`
|
||||
UpdatedBy *uint64 `json:"updated_by"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// CreateRequest 创建支付配置请求
|
||||
type CreateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Provider string `json:"provider" binding:"required,oneof=leshua mock"`
|
||||
MerchantID string `json:"merchant_id" binding:"required"`
|
||||
GatewayURL string `json:"gateway_url"`
|
||||
SignKey string `json:"sign_key"`
|
||||
NotifyKey string `json:"notify_key"`
|
||||
NotifyURL string `json:"notify_url"`
|
||||
JumpURL string `json:"jump_url"`
|
||||
PayWay string `json:"pay_way"`
|
||||
JSPayFlag string `json:"jspay_flag"`
|
||||
SignType string `json:"sign_type"`
|
||||
ExtraConfig map[string]any `json:"extra_config"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
Status string `json:"status" binding:"omitempty,oneof=active disabled testing"`
|
||||
Environment string `json:"environment" binding:"omitempty,oneof=production sandbox"`
|
||||
BusinessTags []string `json:"business_tags"`
|
||||
}
|
||||
|
||||
// UpdateRequest 更新支付配置请求
|
||||
type UpdateRequest struct {
|
||||
Name *string `json:"name"`
|
||||
MerchantID *string `json:"merchant_id"`
|
||||
GatewayURL *string `json:"gateway_url"`
|
||||
SignKey *string `json:"sign_key"`
|
||||
NotifyKey *string `json:"notify_key"`
|
||||
NotifyURL *string `json:"notify_url"`
|
||||
JumpURL *string `json:"jump_url"`
|
||||
PayWay *string `json:"pay_way"`
|
||||
JSPayFlag *string `json:"jspay_flag"`
|
||||
SignType *string `json:"sign_type"`
|
||||
ExtraConfig map[string]any `json:"extra_config"`
|
||||
IsDefault *bool `json:"is_default"`
|
||||
Status *string `json:"status"`
|
||||
Environment *string `json:"environment"`
|
||||
BusinessTags []string `json:"business_tags"`
|
||||
}
|
||||
|
||||
// ListQuery 列表查询参数
|
||||
type ListQuery struct {
|
||||
Provider string `form:"provider"`
|
||||
Status string `form:"status"`
|
||||
Environment string `form:"environment"`
|
||||
Page int `form:"page"`
|
||||
PageSize int `form:"page_size"`
|
||||
}
|
||||
|
||||
// ListResponse 列表响应
|
||||
type ListResponse struct {
|
||||
Items []ConfigDTO `json:"items"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package paymentconfig
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Encryptor 密钥加密器接口
|
||||
type Encryptor interface {
|
||||
Encrypt(plaintext string) (string, error)
|
||||
Decrypt(ciphertext string) (string, error)
|
||||
}
|
||||
|
||||
// AESEncryptor AES 加密器
|
||||
type AESEncryptor struct {
|
||||
key []byte
|
||||
}
|
||||
|
||||
// NewAESEncryptor 创建 AES 加密器
|
||||
// key 必须是 16、24 或 32 字节(对应 AES-128、AES-192、AES-256)
|
||||
func NewAESEncryptor(key string) (*AESEncryptor, error) {
|
||||
keyBytes := []byte(key)
|
||||
if len(keyBytes) != 16 && len(keyBytes) != 24 && len(keyBytes) != 32 {
|
||||
return nil, errors.New("invalid key length: must be 16, 24, or 32 bytes")
|
||||
}
|
||||
return &AESEncryptor{key: keyBytes}, nil
|
||||
}
|
||||
|
||||
// Encrypt 加密明文(使用 AES-GCM)
|
||||
func (e *AESEncryptor) Encrypt(plaintext string) (string, error) {
|
||||
if plaintext == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(e.key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// Decrypt 解密密文(使用 AES-GCM)
|
||||
func (e *AESEncryptor) Decrypt(ciphertext string) (string, error) {
|
||||
if ciphertext == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
ciphertextBytes, err := base64.StdEncoding.DecodeString(ciphertext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(e.key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
nonceSize := gcm.NonceSize()
|
||||
if len(ciphertextBytes) < nonceSize {
|
||||
return "", errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
nonce, ciphertextBytes := ciphertextBytes[:nonceSize], ciphertextBytes[nonceSize:]
|
||||
plaintext, err := gcm.Open(nil, nonce, ciphertextBytes, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
// MockEncryptor 模拟加密器(用于测试)
|
||||
type MockEncryptor struct{}
|
||||
|
||||
func (e *MockEncryptor) Encrypt(plaintext string) (string, error) {
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
func (e *MockEncryptor) Decrypt(ciphertext string) (string, error) {
|
||||
return ciphertext, nil
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package paymentconfig
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"hfb_sys/backend/internal/middleware"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
// List 获取支付配置列表
|
||||
// @Summary 获取支付配置列表
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param provider query string false "支付服务商"
|
||||
// @Param status query string false "状态"
|
||||
// @Param environment query string false "环境"
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "每页数量"
|
||||
// @Success 200 {object} response.Response{data=ListResponse}
|
||||
// @Router /admin/payment-configs [get]
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
var query ListQuery
|
||||
if err := c.ShouldBindQuery(&query); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.service.List(query)
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, resp)
|
||||
}
|
||||
|
||||
// Get 获取单个支付配置
|
||||
// @Summary 获取单个支付配置
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param id path int true "配置ID"
|
||||
// @Param include_secret query bool false "是否包含密钥"
|
||||
// @Success 200 {object} response.Response{data=ConfigDTO}
|
||||
// @Router /admin/payment-configs/{id} [get]
|
||||
func (h *Handler) Get(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "无效的ID")
|
||||
return
|
||||
}
|
||||
|
||||
includeSecret := c.Query("include_secret") == "true"
|
||||
|
||||
config, err := h.service.Get(id, includeSecret)
|
||||
if err == ErrConfigNotFound {
|
||||
response.NotFound(c, "配置不存在")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, config)
|
||||
}
|
||||
|
||||
// Create 创建支付配置
|
||||
// @Summary 创建支付配置
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param body body CreateRequest true "创建请求"
|
||||
// @Success 200 {object} response.Response{data=ConfigDTO}
|
||||
// @Router /admin/payment-configs [post]
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
adminID, ok := currentAdminID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
var req CreateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
config, err := h.service.Create(req, adminID, auditMeta(c))
|
||||
if err == ErrNameRequired || err == ErrMerchantIDRequired || err == ErrGatewayURLRequired ||
|
||||
err == ErrSignKeyRequired || err == ErrNotifyKeyRequired || err == ErrInvalidProvider ||
|
||||
err == ErrNotifyURLRequired || err == ErrInvalidSignType {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "创建失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, config)
|
||||
}
|
||||
|
||||
// Update 更新支付配置
|
||||
// @Summary 更新支付配置
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param id path int true "配置ID"
|
||||
// @Param body body UpdateRequest true "更新请求"
|
||||
// @Success 200 {object} response.Response{data=ConfigDTO}
|
||||
// @Router /admin/payment-configs/{id} [put]
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
adminID, ok := currentAdminID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "无效的ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
config, err := h.service.Update(id, req, adminID, auditMeta(c))
|
||||
if err == ErrConfigNotFound {
|
||||
response.NotFound(c, "配置不存在")
|
||||
return
|
||||
}
|
||||
if err == ErrInvalidSignType || err == ErrNotifyURLRequired {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "更新失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, config)
|
||||
}
|
||||
|
||||
// Delete 删除支付配置
|
||||
// @Summary 删除支付配置
|
||||
// @Tags 管理后台-支付配置
|
||||
// @Param id path int true "配置ID"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /admin/payment-configs/{id} [delete]
|
||||
func (h *Handler) Delete(c *gin.Context) {
|
||||
adminID, ok := currentAdminID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "无效的ID")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.Delete(id, adminID, auditMeta(c))
|
||||
if err == ErrConfigNotFound {
|
||||
response.NotFound(c, "配置不存在")
|
||||
return
|
||||
}
|
||||
if err == ErrCannotDeleteInUse {
|
||||
response.BadRequest(c, "配置正在使用中,无法删除")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "删除失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"message": "删除成功"})
|
||||
}
|
||||
|
||||
func currentAdminID(c *gin.Context) (uint64, bool) {
|
||||
val, exists := c.Get(middleware.ContextAdminID)
|
||||
if !exists {
|
||||
return 0, false
|
||||
}
|
||||
id, ok := val.(uint64)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func auditMeta(c *gin.Context) AuditMeta {
|
||||
return AuditMeta{
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
package paymentconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"hfb_sys/backend/internal/auditlog"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
encryptor Encryptor
|
||||
}
|
||||
|
||||
type AuditMeta = auditlog.Meta
|
||||
|
||||
func NewRepository(db *gorm.DB, encryptor Encryptor) *Repository {
|
||||
return &Repository{
|
||||
db: db,
|
||||
encryptor: encryptor,
|
||||
}
|
||||
}
|
||||
|
||||
// List 获取配置列表
|
||||
func (r *Repository) List(query ListQuery) ([]ConfigDTO, int64, error) {
|
||||
var items []model.PaymentMerchantConfig
|
||||
var total int64
|
||||
|
||||
db := r.db.Model(&model.PaymentMerchantConfig{})
|
||||
|
||||
// 过滤条件
|
||||
if query.Provider != "" {
|
||||
db = db.Where("provider = ?", query.Provider)
|
||||
}
|
||||
if query.Status != "" {
|
||||
db = db.Where("status = ?", query.Status)
|
||||
}
|
||||
if query.Environment != "" {
|
||||
db = db.Where("environment = ?", query.Environment)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页
|
||||
page := query.Page
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
pageSize := query.PageSize
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
if err := db.Order("is_default DESC, id DESC").Offset(offset).Limit(pageSize).Find(&items).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
dtos := make([]ConfigDTO, 0, len(items))
|
||||
for _, item := range items {
|
||||
dto, err := r.toDTO(item, false)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
dtos = append(dtos, dto)
|
||||
}
|
||||
|
||||
return dtos, total, nil
|
||||
}
|
||||
|
||||
// FindByID 根据 ID 查询配置
|
||||
func (r *Repository) FindByID(id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.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 查询默认配置
|
||||
func (r *Repository) FindDefault(provider string) (*model.PaymentMerchantConfig, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.Where("provider = ? AND is_default = ? AND status = ?", provider, true, "active").First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNoActiveConfigFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// FindDefaultAny 查询任意服务商的默认启用配置。
|
||||
func (r *Repository) FindDefaultAny(includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.Where("status = ?", "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
|
||||
}
|
||||
|
||||
// FindDefaultByProvider 查询指定服务商的默认启用配置。
|
||||
func (r *Repository) FindDefaultByProvider(provider string, includeSecret bool) (*ConfigDTO, error) {
|
||||
item, err := r.FindDefault(provider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto, err := r.toDTO(*item, includeSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindByProviderMerchant 根据服务商和商户号查配置,用于历史支付单继续使用原商户密钥。
|
||||
func (r *Repository) FindByProviderMerchant(provider string, merchantID string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.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) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
dto, err := r.toDTO(item, includeSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// FindActiveByProvider 查询提供商的所有激活配置
|
||||
func (r *Repository) FindActiveByProvider(provider string) ([]model.PaymentMerchantConfig, error) {
|
||||
var items []model.PaymentMerchantConfig
|
||||
if err := r.db.Where("provider = ? AND status = ?", provider, "active").Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// Create 创建配置
|
||||
func (r *Repository) Create(req CreateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
// 验证必填字段
|
||||
if err := r.validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var dto ConfigDTO
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 如果设置为默认,先取消同 provider 的其他默认配置
|
||||
if req.IsDefault {
|
||||
if err := tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("provider = ? AND is_default = ?", req.Provider, true).
|
||||
Update("is_default", false).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 加密密钥
|
||||
encryptedSignKey, err := r.encryptor.Encrypt(req.SignKey)
|
||||
if err != nil {
|
||||
return ErrEncryptionFailed
|
||||
}
|
||||
encryptedNotifyKey, err := r.encryptor.Encrypt(req.NotifyKey)
|
||||
if err != nil {
|
||||
return ErrEncryptionFailed
|
||||
}
|
||||
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
environment := req.Environment
|
||||
if environment == "" {
|
||||
environment = "production"
|
||||
}
|
||||
|
||||
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: firstNonEmpty(req.PayWay, "ZFBZF"),
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, "MD5"),
|
||||
ExtraConfig: req.ExtraConfig,
|
||||
IsDefault: req.IsDefault,
|
||||
Status: status,
|
||||
Environment: environment,
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &actorID,
|
||||
UpdatedBy: &actorID,
|
||||
}
|
||||
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if err := appendAuditLog(tx, actorID, "payment_config.create", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dto, err = r.toDTO(item, false)
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// Update 更新配置
|
||||
func (r *Repository) Update(id uint64, req UpdateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
if req.SignType != nil && *req.SignType != "" && *req.SignType != "MD5" {
|
||||
return nil, ErrInvalidSignType
|
||||
}
|
||||
if req.NotifyURL != nil && *req.NotifyURL == "" {
|
||||
return nil, ErrNotifyURLRequired
|
||||
}
|
||||
|
||||
var dto ConfigDTO
|
||||
err := r.db.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 errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 如果设置为默认,先取消同 provider 的其他默认配置
|
||||
if req.IsDefault != nil && *req.IsDefault && !item.IsDefault {
|
||||
if err := tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("provider = ? AND is_default = ? AND id != ?", item.Provider, true, id).
|
||||
Update("is_default", false).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
updates := make(map[string]any)
|
||||
if req.Name != nil {
|
||||
updates["name"] = *req.Name
|
||||
}
|
||||
if req.MerchantID != nil {
|
||||
updates["merchant_id"] = *req.MerchantID
|
||||
}
|
||||
if req.GatewayURL != nil {
|
||||
updates["gateway_url"] = *req.GatewayURL
|
||||
}
|
||||
if req.SignKey != nil {
|
||||
encrypted, err := r.encryptor.Encrypt(*req.SignKey)
|
||||
if err != nil {
|
||||
return ErrEncryptionFailed
|
||||
}
|
||||
updates["sign_key"] = encrypted
|
||||
}
|
||||
if req.NotifyKey != nil {
|
||||
encrypted, err := r.encryptor.Encrypt(*req.NotifyKey)
|
||||
if err != nil {
|
||||
return ErrEncryptionFailed
|
||||
}
|
||||
updates["notify_key"] = encrypted
|
||||
}
|
||||
if req.NotifyURL != nil {
|
||||
updates["notify_url"] = *req.NotifyURL
|
||||
}
|
||||
if req.JumpURL != nil {
|
||||
updates["jump_url"] = *req.JumpURL
|
||||
}
|
||||
if req.PayWay != nil {
|
||||
updates["pay_way"] = *req.PayWay
|
||||
}
|
||||
if req.JSPayFlag != nil {
|
||||
updates["jspay_flag"] = *req.JSPayFlag
|
||||
}
|
||||
if req.SignType != nil {
|
||||
updates["sign_type"] = *req.SignType
|
||||
}
|
||||
if req.ExtraConfig != nil {
|
||||
updates["extra_config"] = req.ExtraConfig
|
||||
}
|
||||
if req.IsDefault != nil {
|
||||
updates["is_default"] = *req.IsDefault
|
||||
}
|
||||
if req.Status != nil {
|
||||
updates["status"] = *req.Status
|
||||
}
|
||||
if req.Environment != nil {
|
||||
updates["environment"] = *req.Environment
|
||||
}
|
||||
if req.BusinessTags != nil {
|
||||
updates["business_tags"] = req.BusinessTags
|
||||
}
|
||||
updates["updated_by"] = actorID
|
||||
|
||||
if err := tx.Model(&item).Updates(updates).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if err := appendAuditLog(tx, actorID, "payment_config.update", item.ID, meta, map[string]any{
|
||||
"updates": updates,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 重新查询
|
||||
if err := tx.Where("id = ?", id).First(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var dtoErr error
|
||||
dto, dtoErr = r.toDTO(item, false)
|
||||
return dtoErr
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// Delete 删除配置
|
||||
func (r *Repository) Delete(id uint64, actorID uint64, meta AuditMeta) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := tx.Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var usedCount int64
|
||||
if err := tx.Model(&model.PaymentOrder{}).
|
||||
Where("provider = ? AND merchant_id = ?", item.Provider, item.MerchantID).
|
||||
Count(&usedCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if usedCount > 0 {
|
||||
return ErrCannotDeleteInUse
|
||||
}
|
||||
|
||||
if err := tx.Delete(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
return appendAuditLog(tx, actorID, "payment_config.delete", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// IncrementUsage 增加使用统计
|
||||
func (r *Repository) IncrementUsage(id uint64, amountCent int64) error {
|
||||
now := time.Now()
|
||||
return r.db.Model(&model.PaymentMerchantConfig{}).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,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// RecordUsage 记录支付配置命中情况,同一个支付单只记录一次。
|
||||
func (r *Repository) RecordUsage(configID uint64, paymentOrderID uint64, provider string, merchantID string, amountCent int64, bizType string) error {
|
||||
if configID == 0 || paymentOrderID == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var existing model.PaymentConfigUsageLog
|
||||
err := tx.Where("payment_order_id = ?", paymentOrderID).First(&existing).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
log := model.PaymentConfigUsageLog{
|
||||
ConfigID: configID,
|
||||
PaymentOrderID: paymentOrderID,
|
||||
Provider: provider,
|
||||
MerchantID: merchantID,
|
||||
AmountCent: amountCent,
|
||||
BizType: bizType,
|
||||
}
|
||||
if err := tx.Create(&log).Error; err != nil {
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
// 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,
|
||||
TotalTransactions: item.TotalTransactions,
|
||||
TotalAmountCent: item.TotalAmountCent,
|
||||
CreatedBy: item.CreatedBy,
|
||||
UpdatedBy: item.UpdatedBy,
|
||||
CreatedAt: item.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: item.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
if item.LastUsedAt != nil {
|
||||
s := item.LastUsedAt.Format(time.RFC3339)
|
||||
dto.LastUsedAt = &s
|
||||
}
|
||||
|
||||
// 只有明确要求时才解密并返回密钥
|
||||
if includeSecret {
|
||||
signKey, err := r.encryptor.Decrypt(item.SignKey)
|
||||
if err != nil {
|
||||
return ConfigDTO{}, ErrDecryptionFailed
|
||||
}
|
||||
notifyKey, err := r.encryptor.Decrypt(item.NotifyKey)
|
||||
if err != nil {
|
||||
return ConfigDTO{}, ErrDecryptionFailed
|
||||
}
|
||||
dto.SignKey = signKey
|
||||
dto.NotifyKey = notifyKey
|
||||
}
|
||||
|
||||
return dto, nil
|
||||
}
|
||||
|
||||
// validateCreateRequest 验证创建请求
|
||||
func (r *Repository) validateCreateRequest(req CreateRequest) error {
|
||||
if req.Name == "" {
|
||||
return ErrNameRequired
|
||||
}
|
||||
if req.Provider == "" {
|
||||
return ErrInvalidProvider
|
||||
}
|
||||
if req.MerchantID == "" {
|
||||
return ErrMerchantIDRequired
|
||||
}
|
||||
if req.SignType != "" && req.SignType != "MD5" {
|
||||
return ErrInvalidSignType
|
||||
}
|
||||
|
||||
// leshua 特定验证
|
||||
if req.Provider == "leshua" {
|
||||
if req.GatewayURL == "" {
|
||||
return ErrGatewayURLRequired
|
||||
}
|
||||
if req.SignKey == "" {
|
||||
return ErrSignKeyRequired
|
||||
}
|
||||
if req.NotifyKey == "" {
|
||||
return ErrNotifyKeyRequired
|
||||
}
|
||||
if req.NotifyURL == "" {
|
||||
return ErrNotifyURLRequired
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func firstNonEmpty(vals ...string) string {
|
||||
for _, v := range vals {
|
||||
if v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func appendAuditLog(tx *gorm.DB, actorID uint64, action string, bizID uint64, meta AuditMeta, detail map[string]any) error {
|
||||
detailJSON, err := json.Marshal(detail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log := model.AuditLog{
|
||||
ActorType: "admin",
|
||||
ActorID: actorID,
|
||||
Action: action,
|
||||
BizType: "payment_config",
|
||||
BizID: &bizID,
|
||||
IP: meta.IP,
|
||||
UserAgent: meta.UserAgent,
|
||||
Detail: detailJSON,
|
||||
}
|
||||
return tx.Create(&log).Error
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package paymentconfig
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
// List 获取配置列表
|
||||
func (s *Service) List(query ListQuery) (*ListResponse, error) {
|
||||
items, total, err := s.repo.List(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
page := query.Page
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
pageSize := query.PageSize
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
return &ListResponse{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Get 获取单个配置
|
||||
func (s *Service) Get(id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
return s.repo.FindByID(id, includeSecret)
|
||||
}
|
||||
|
||||
// Create 创建配置
|
||||
func (s *Service) Create(req CreateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
return s.repo.Create(req, actorID, meta)
|
||||
}
|
||||
|
||||
// Update 更新配置
|
||||
func (s *Service) Update(id uint64, req UpdateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
return s.repo.Update(id, req, actorID, meta)
|
||||
}
|
||||
|
||||
// Delete 删除配置
|
||||
func (s *Service) Delete(id uint64, actorID uint64, meta AuditMeta) error {
|
||||
return s.repo.Delete(id, actorID, meta)
|
||||
}
|
||||
|
||||
// GetDefaultConfig 获取默认配置(用于支付模块调用)
|
||||
func (s *Service) GetDefaultConfig(provider string) (*ConfigDTO, error) {
|
||||
config, err := s.repo.FindDefault(provider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto, err := s.repo.toDTO(*config, true) // 包含密钥
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto, nil
|
||||
}
|
||||
Reference in New Issue
Block a user