支持后台支付配置管理

This commit is contained in:
yml
2026-06-06 17:49:22 +08:00
parent 13543b3dcb
commit 4d40883da6
29 changed files with 3173 additions and 198 deletions
-32
View File
@@ -16,7 +16,6 @@ type Config struct {
Storage StorageConfig
SMS SMSConfig
Realname RealnameConfig
Payment PaymentConfig
Log LogConfig
}
@@ -42,23 +41,6 @@ type RealnameConfig struct {
CloudMarketAppCode string
}
type PaymentConfig struct {
Provider string
Leshua LeshuaPaymentConfig
}
type LeshuaPaymentConfig struct {
GatewayURL string
MerchantID string
SignKey string
NotifyKey string
NotifyURL string
JumpURL string
PayWay string
JSPayFlag string
SignType string
}
type LogConfig struct {
Level string
Dir string
@@ -94,20 +76,6 @@ func Load() Config {
CloudMarketURL: getEnv("REALNAME_CLOUDMARKET_URL", "https://sinocheck2.market.alicloudapi.com/fortest/ttttt"),
CloudMarketAppCode: getEnv("REALNAME_CLOUDMARKET_APPCODE", ""),
},
Payment: PaymentConfig{
Provider: getEnv("PAYMENT_PROVIDER", "mock"),
Leshua: LeshuaPaymentConfig{
GatewayURL: getEnv("LESHUA_GATEWAY_URL", "https://t-paygate.lepass.cn/cgi-bin/lepos_pay_gateway.cgi"),
MerchantID: getEnv("LESHUA_MERCHANT_ID", ""),
SignKey: getEnv("LESHUA_SIGN_KEY", ""),
NotifyKey: getEnv("LESHUA_NOTIFY_KEY", ""),
NotifyURL: getEnv("LESHUA_NOTIFY_URL", ""),
JumpURL: getEnv("LESHUA_JUMP_URL", ""),
PayWay: getEnv("LESHUA_PAY_WAY", "ZFBZF"),
JSPayFlag: getEnv("LESHUA_JSPAY_FLAG", "2"),
SignType: getEnv("LESHUA_SIGN_TYPE", "MD5"),
},
},
Log: LogConfig{
Level: getEnv("LOG_LEVEL", "info"),
Dir: getEnv("LOG_DIR", "logs"),
@@ -16,8 +16,6 @@ import (
"sort"
"strings"
"time"
"hfb_sys/backend/internal/config"
)
var (
@@ -26,10 +24,22 @@ var (
)
type Client struct {
cfg config.LeshuaPaymentConfig
cfg Config
httpClient *http.Client
}
type Config struct {
GatewayURL string
MerchantID string
SignKey string
NotifyKey string
NotifyURL string
JumpURL string
PayWay string
JSPayFlag string
SignType string
}
type CreatePaymentRequest struct {
ThirdOrderID string
AmountCent int64
@@ -131,7 +141,7 @@ type VerifyNotifyResult struct {
ParamKeys []string
}
func NewClient(cfg config.LeshuaPaymentConfig) *Client {
func NewClient(cfg Config) *Client {
return &Client{
cfg: cfg,
httpClient: &http.Client{
@@ -234,11 +244,11 @@ func (c *Client) CreateRefund(ctx context.Context, req CreateRefundRequest) (*Cr
return nil, nil, err
}
params := map[string]string{
"service": "unified_refund",
"merchant_id": c.cfg.MerchantID,
"service": "unified_refund",
"merchant_id": c.cfg.MerchantID,
"merchant_refund_id": req.MerchantRefundID,
"refund_amount": fmt.Sprintf("%d", req.RefundAmountCent),
"nonce_str": Nonce(32),
"refund_amount": fmt.Sprintf("%d", req.RefundAmountCent),
"nonce_str": Nonce(32),
}
if req.LeshuaOrderID != "" {
params["leshua_order_id"] = req.LeshuaOrderID
@@ -3,8 +3,6 @@ package leshua
import (
"strings"
"testing"
"hfb_sys/backend/internal/config"
)
func TestSignUsesASCIISortedNonEmptyParams(t *testing.T) {
@@ -32,7 +30,7 @@ func TestSignUsesASCIISortedNonEmptyParams(t *testing.T) {
}
func TestVerifyNotifyIncludesEmptyAndExcludesErrorCode(t *testing.T) {
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
client := NewClient(Config{NotifyKey: "notify-secret"})
params := map[string]string{
"merchant_id": "1234567890",
"third_order_id": "NO1",
@@ -59,7 +57,7 @@ func TestVerifyNotifyIncludesEmptyAndExcludesErrorCode(t *testing.T) {
}
func TestVerifyNotifyDoesNotFallBackToSignKey(t *testing.T) {
client := NewClient(config.LeshuaPaymentConfig{
client := NewClient(Config{
NotifyKey: "wrong-notify-secret",
SignKey: "sign-secret",
})
@@ -85,7 +83,7 @@ func TestVerifyNotifyDoesNotFallBackToSignKey(t *testing.T) {
}
func TestVerifyNotifyUsesDocumentedNotifySignature(t *testing.T) {
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
client := NewClient(Config{NotifyKey: "notify-secret"})
params := map[string]string{
"merchant_id": "1234567890",
"third_order_id": "NO1",
@@ -109,7 +107,7 @@ func TestVerifyNotifyUsesDocumentedNotifySignature(t *testing.T) {
}
func TestVerifyNotifyKeepsEmptyXMLFieldsInSignature(t *testing.T) {
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
client := NewClient(Config{NotifyKey: "notify-secret"})
params, err := ParsePayload([]byte(`<leshua>
<amount>100</amount>
<goods_tag></goods_tag>
@@ -190,7 +188,7 @@ func TestSignRefundUsesSameAlgorithm(t *testing.T) {
}
func TestVerifyRefundNotify(t *testing.T) {
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
client := NewClient(Config{NotifyKey: "notify-secret"})
params := map[string]string{
"merchant_id": "1234567890",
"third_order_id": "NO1",
@@ -0,0 +1,103 @@
package model
import (
"database/sql/driver"
"encoding/json"
"time"
)
// PaymentMerchantConfig 支付商户配置
type PaymentMerchantConfig struct {
ID uint64 `gorm:"primaryKey" json:"id"`
Name string `gorm:"size:128;not null" json:"name"`
Provider string `gorm:"size:32;not null;index:idx_payment_merchant_configs_provider" json:"provider"`
MerchantID string `gorm:"size:128;not null;index:idx_payment_merchant_configs_merchant_id" json:"merchant_id"`
GatewayURL string `gorm:"size:512;not null;default:''" json:"gateway_url"`
SignKey string `gorm:"size:512;not null;default:''" json:"sign_key"` // 加密存储
NotifyKey string `gorm:"size:512;not null;default:''" json:"notify_key"` // 加密存储
NotifyURL string `gorm:"size:512;not null;default:''" json:"notify_url"`
JumpURL string `gorm:"size:512;not null;default:''" json:"jump_url"`
PayWay string `gorm:"size:32;not null;default:'ZFBZF'" json:"pay_way"`
JSPayFlag string `gorm:"column:jspay_flag;size:8;not null;default:'2'" json:"jspay_flag"`
SignType string `gorm:"size:16;not null;default:'MD5'" json:"sign_type"`
ExtraConfig JSONMap `gorm:"type:json" json:"extra_config"`
IsDefault bool `gorm:"not null;default:0;index:idx_payment_merchant_configs_default" json:"is_default"`
Status string `gorm:"size:32;not null;default:'active';index:idx_payment_merchant_configs_status" json:"status"`
Environment string `gorm:"size:16;not null;default:'production'" json:"environment"`
BusinessTags JSONArray `gorm:"type:json" json:"business_tags"`
// 统计信息
TotalTransactions int64 `gorm:"not null;default:0" json:"total_transactions"`
TotalAmountCent int64 `gorm:"not null;default:0" json:"total_amount_cent"`
LastUsedAt *time.Time `json:"last_used_at"`
// 审计信息
CreatedBy *uint64 `json:"created_by"`
UpdatedBy *uint64 `json:"updated_by"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (PaymentMerchantConfig) TableName() string {
return "payment_merchant_configs"
}
// JSONMap 用于 extra_config 字段
type JSONMap map[string]any
func (j JSONMap) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
return json.Marshal(j)
}
func (j *JSONMap) Scan(value any) error {
if value == nil {
*j = nil
return nil
}
bytes, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(bytes, j)
}
// JSONArray 用于 business_tags 字段
type JSONArray []string
func (j JSONArray) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
return json.Marshal(j)
}
func (j *JSONArray) Scan(value any) error {
if value == nil {
*j = nil
return nil
}
bytes, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(bytes, j)
}
// PaymentConfigUsageLog 支付配置使用日志
type PaymentConfigUsageLog struct {
ID uint64 `gorm:"primaryKey" json:"id"`
ConfigID uint64 `gorm:"not null;index:idx_payment_config_usage_logs_config" json:"config_id"`
PaymentOrderID uint64 `gorm:"not null;index:idx_payment_config_usage_logs_payment" json:"payment_order_id"`
Provider string `gorm:"size:32;not null" json:"provider"`
MerchantID string `gorm:"size:128;not null" json:"merchant_id"`
AmountCent int64 `gorm:"not null" json:"amount_cent"`
BizType string `gorm:"size:32;not null" json:"biz_type"`
CreatedAt time.Time `json:"created_at"`
}
func (PaymentConfigUsageLog) TableName() string {
return "payment_config_usage_logs"
}
@@ -196,6 +196,14 @@ func (r *Repository) loadRolesAndPerms(dto *AdminDTO) {
dto.Roles = roles
// 加载权限
for _, role := range roles {
if role.Code == "super_admin" {
dto.Permissions = []string{"*"}
cachePermissions(r, dto.ID, dto.Permissions)
return
}
}
var permCodes []string
r.db.Table("permissions").
Select("DISTINCT permissions.code").
@@ -206,12 +214,17 @@ func (r *Repository) loadRolesAndPerms(dto *AdminDTO) {
dto.Permissions = permCodes
// 缓存权限到 Redis
if r.redis != nil && len(permCodes) > 0 {
ctx := context.Background()
key := fmt.Sprintf("admin:perms:%d", dto.ID)
raw, _ := json.Marshal(permCodes)
r.redis.Set(ctx, key, string(raw), 2*time.Hour)
cachePermissions(r, dto.ID, permCodes)
}
func cachePermissions(r *Repository, adminID uint64, permCodes []string) {
if r.redis == nil || len(permCodes) == 0 {
return
}
ctx := context.Background()
key := fmt.Sprintf("admin:perms:%d", adminID)
raw, _ := json.Marshal(permCodes)
r.redis.Set(ctx, key, string(raw), 2*time.Hour)
}
func captchaKey(id string) string {
@@ -25,7 +25,11 @@ func (r *Repository) List() ([]RoleDTO, error) {
result := make([]RoleDTO, 0, len(roles))
for _, role := range roles {
var count int64
r.db.Model(&model.RolePermission{}).Where("role_id = ?", role.ID).Count(&count)
if role.Code == "super_admin" {
r.db.Model(&model.Permission{}).Count(&count)
} else {
r.db.Model(&model.RolePermission{}).Where("role_id = ?", role.ID).Count(&count)
}
result = append(result, RoleDTO{
ID: role.ID,
Code: role.Code,
@@ -44,7 +48,13 @@ func (r *Repository) FindByID(id uint64) (*RoleDTO, error) {
if err := r.db.First(&role, id).Error; err != nil {
return nil, err
}
perms, err := r.getRolePermissions(id)
var perms []PermissionDTO
var err error
if role.Code == "super_admin" {
perms, err = r.ListPermissions()
} else {
perms, err = r.getRolePermissions(id)
}
if err != nil {
return nil, err
}
@@ -111,6 +121,13 @@ func (r *Repository) AssignPermissions(roleID uint64, permIDs []uint64) error {
return err
}
return r.db.Transaction(func(tx *gorm.DB) error {
if role.Code == "super_admin" {
var allPermIDs []uint64
if err := tx.Model(&model.Permission{}).Pluck("id", &allPermIDs).Error; err != nil {
return err
}
permIDs = allPermIDs
}
if err := tx.Where("role_id = ?", roleID).Delete(&model.RolePermission{}).Error; err != nil {
return err
}
+250 -104
View File
@@ -10,10 +10,10 @@ import (
"math"
"time"
"hfb_sys/backend/internal/config"
"hfb_sys/backend/internal/integrations/payment/leshua"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/order"
"hfb_sys/backend/internal/modules/paymentconfig"
"hfb_sys/backend/internal/modules/wallet"
"gorm.io/datatypes"
@@ -23,12 +23,16 @@ import (
type Repository struct {
db *gorm.DB
cfg config.PaymentConfig
configRepo *paymentconfig.Repository
orderRepo *order.Repository
walletRepo *wallet.Repository
leshua *leshua.Client
provider string
isMockMode bool
}
type runtimePaymentConfig struct {
ID uint64
Provider string
MerchantID string
Leshua leshua.Config
}
const (
@@ -47,32 +51,104 @@ var refundBizTypes = []string{
"rent_refund",
}
func NewRepository(db *gorm.DB, cfg config.PaymentConfig, orderRepo *order.Repository, walletRepo *wallet.Repository) *Repository {
provider := cfg.Provider
if provider == "" {
provider = "mock"
}
func NewRepository(db *gorm.DB, configRepo *paymentconfig.Repository, orderRepo *order.Repository, walletRepo *wallet.Repository) *Repository {
return &Repository{
db: db,
cfg: cfg,
configRepo: configRepo,
orderRepo: orderRepo,
walletRepo: walletRepo,
leshua: leshua.NewClient(cfg.Leshua),
provider: provider,
isMockMode: provider != "leshua",
}
}
func (c runtimePaymentConfig) isMockMode() bool {
return c.Provider != "leshua"
}
func (c runtimePaymentConfig) client() *leshua.Client {
return leshua.NewClient(c.Leshua)
}
func (r *Repository) defaultRuntimeConfig() (*runtimePaymentConfig, error) {
if r.configRepo == nil {
return nil, ErrPaymentUnavailable
}
dto, err := r.configRepo.FindDefaultAny(true)
if err != nil {
return nil, err
}
return runtimeConfigFromDTO(dto), nil
}
func (r *Repository) runtimeConfigForPayment(payment *model.PaymentOrder) (*runtimePaymentConfig, error) {
provider := firstNonEmpty(payment.Provider, "mock")
merchantID := payment.MerchantID
if r.configRepo != nil && merchantID != "" {
dto, err := r.configRepo.FindByProviderMerchant(provider, merchantID, true)
if err == nil {
return runtimeConfigFromDTO(dto), nil
}
if err != paymentconfig.ErrConfigNotFound {
return nil, err
}
}
if provider != "leshua" {
return &runtimePaymentConfig{
Provider: provider,
MerchantID: merchantID,
}, nil
}
if r.configRepo == nil {
return nil, ErrPaymentUnavailable
}
dto, err := r.configRepo.FindDefaultByProvider(provider, true)
if err != nil {
return nil, err
}
return runtimeConfigFromDTO(dto), nil
}
func runtimeConfigFromDTO(dto *paymentconfig.ConfigDTO) *runtimePaymentConfig {
provider := firstNonEmpty(dto.Provider, "mock")
payWay := firstNonEmpty(dto.PayWay, "ZFBZF")
jsPayFlag := firstNonEmpty(dto.JSPayFlag, "2")
signType := firstNonEmpty(dto.SignType, "MD5")
return &runtimePaymentConfig{
ID: dto.ID,
Provider: provider,
MerchantID: dto.MerchantID,
Leshua: leshua.Config{
GatewayURL: dto.GatewayURL,
MerchantID: dto.MerchantID,
SignKey: dto.SignKey,
NotifyKey: dto.NotifyKey,
NotifyURL: dto.NotifyURL,
JumpURL: dto.JumpURL,
PayWay: payWay,
JSPayFlag: jsPayFlag,
SignType: signType,
},
}
}
func (r *Repository) Start(userID uint64, orderID uint64, req StartPaymentRequest, clientIP string) (*PaymentDTO, error) {
payment, orderRow, err := r.preparePayment(userID, orderID, req)
defaultConfig, err := r.defaultRuntimeConfig()
if err != nil {
return nil, ErrPaymentUnavailable
}
payment, orderRow, err := r.preparePayment(userID, orderID, req, *defaultConfig)
if err != nil {
return nil, err
}
runtimeConfig, err := r.runtimeConfigForPayment(payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
if payment.Status == "paid" {
r.recordConfigUsage(runtimeConfig, payment)
dto := toDTO(*payment)
return &dto, nil
}
if r.isMockMode {
if runtimeConfig.isMockMode() {
if err := r.confirmPaid(payment, "2", time.Now(), map[string]string{
"mock": "true",
"third_order_id": payment.ThirdOrderID,
@@ -85,21 +161,24 @@ func (r *Repository) Start(userID uint64, orderID uint64, req StartPaymentReques
if err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, latest)
dto := toDTO(*latest)
return &dto, nil
}
if payment.Status == "paying" && (payment.TDCode != "" || payment.JSPayURL != "" || payment.JSPayInfo != "") {
r.recordConfigUsage(runtimeConfig, payment)
dto := toDTO(*payment)
return &dto, nil
}
resp, rawReq, err := r.leshua.CreatePayment(context.Background(), leshua.CreatePaymentRequest{
client := runtimeConfig.client()
resp, rawReq, err := client.CreatePayment(context.Background(), leshua.CreatePaymentRequest{
ThirdOrderID: payment.ThirdOrderID,
AmountCent: payment.AmountCent,
PayWay: payment.PayWay,
JSPayFlag: payment.JSPayFlag,
NotifyURL: r.cfg.Leshua.NotifyURL,
JumpURL: r.cfg.Leshua.JumpURL,
NotifyURL: runtimeConfig.Leshua.NotifyURL,
JumpURL: runtimeConfig.Leshua.JumpURL,
ClientIP: clientIP,
Body: "租号订单 " + orderRow.OrderNo,
Attach: orderRow.OrderNo,
@@ -128,6 +207,7 @@ func (r *Repository) Start(userID uint64, orderID uint64, req StartPaymentReques
if err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, latest)
dto := toDTO(*latest)
return &dto, nil
}
@@ -137,11 +217,15 @@ func (r *Repository) StartWalletRecharge(userID uint64, req WalletRechargePaymen
if userID == 0 || req.Amount < MinWalletRechargeAmount || amountCent <= 0 {
return nil, ErrPaymentCannotStart
}
payment, err := r.createWalletRechargePayment(userID, amountCent, req)
runtimeConfig, err := r.defaultRuntimeConfig()
if err != nil {
return nil, ErrPaymentUnavailable
}
payment, err := r.createWalletRechargePayment(userID, amountCent, req, *runtimeConfig)
if err != nil {
return nil, err
}
if r.isMockMode {
if runtimeConfig.isMockMode() {
if err := r.confirmPaid(payment, "2", time.Now(), map[string]string{
"mock": "true",
"third_order_id": payment.ThirdOrderID,
@@ -154,16 +238,18 @@ func (r *Repository) StartWalletRecharge(userID uint64, req WalletRechargePaymen
if err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, latest)
dto := toDTO(*latest)
return &dto, nil
}
resp, rawReq, err := r.leshua.CreatePayment(context.Background(), leshua.CreatePaymentRequest{
client := runtimeConfig.client()
resp, rawReq, err := client.CreatePayment(context.Background(), leshua.CreatePaymentRequest{
ThirdOrderID: payment.ThirdOrderID,
AmountCent: payment.AmountCent,
PayWay: payment.PayWay,
JSPayFlag: payment.JSPayFlag,
NotifyURL: r.cfg.Leshua.NotifyURL,
JumpURL: r.cfg.Leshua.JumpURL,
NotifyURL: runtimeConfig.Leshua.NotifyURL,
JumpURL: runtimeConfig.Leshua.JumpURL,
ClientIP: clientIP,
Body: "钱包充值 " + payment.PaymentNo,
Attach: payment.PaymentNo,
@@ -192,6 +278,7 @@ func (r *Repository) StartWalletRecharge(userID uint64, req WalletRechargePaymen
if err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, latest)
dto := toDTO(*latest)
return &dto, nil
}
@@ -204,11 +291,15 @@ func (r *Repository) QueryWalletRecharge(userID uint64, paymentID uint64) (*Paym
}
return nil, err
}
if payment.Status == "paid" || r.isMockMode {
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
if payment.Status == "paid" || runtimeConfig.isMockMode() {
dto := toDTO(payment)
return &dto, nil
}
resp, err := r.leshua.QueryPayment(context.Background(), payment.ThirdOrderID, payment.ProviderOrderID)
resp, err := runtimeConfig.client().QueryPayment(context.Background(), payment.ThirdOrderID, payment.ProviderOrderID)
if err != nil {
return nil, err
}
@@ -231,11 +322,15 @@ func (r *Repository) Query(userID uint64, orderID uint64) (*PaymentDTO, error) {
}
return nil, err
}
if payment.Status == "paid" || r.isMockMode {
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
if payment.Status == "paid" || runtimeConfig.isMockMode() {
dto := toDTO(payment)
return &dto, nil
}
resp, err := r.leshua.QueryPayment(context.Background(), payment.ThirdOrderID, payment.ProviderOrderID)
resp, err := runtimeConfig.client().QueryPayment(context.Background(), payment.ThirdOrderID, payment.ProviderOrderID)
if err != nil {
return nil, err
}
@@ -251,48 +346,32 @@ func (r *Repository) Query(userID uint64, orderID uint64) (*PaymentDTO, error) {
}
func (r *Repository) HandleLeshuaNotify(params map[string]string, rawPayload string, contentType string) (*NotifyResult, error) {
var verify leshua.VerifyNotifyResult
if !r.isMockMode {
verify = r.leshua.VerifyNotifyDetail(params)
if !verify.OK {
log.Printf(
"[payment] leshua notify verify failed third_order_id=%s got=%s expected=%s keys=%v base_string=%s",
params["third_order_id"],
verify.Got,
verify.Expected["notify_key"],
verify.ParamKeys,
verify.BaseString["notify_key"],
)
if err := r.recordNotifyDiagnostic(params, rawPayload, contentType, verify, "verify_failed"); err != nil {
log.Printf("[payment] leshua notify diagnostic save failed third_order_id=%s err=%v", params["third_order_id"], err)
}
return nil, ErrPaymentVerifyFailed
}
log.Printf("[payment] leshua notify verified third_order_id=%s matched_key=%s", params["third_order_id"], verify.MatchedKey)
}
// 退款通知会携带 merchant_refund_id 或 leshua_refund_id。
if params["merchant_refund_id"] != "" || params["leshua_refund_id"] != "" {
return r.HandleRefundNotify(params, rawPayload, contentType)
}
thirdOrderID := params["third_order_id"]
if thirdOrderID == "" {
return nil, ErrPaymentNotFound
}
var payment model.PaymentOrder
if err := r.db.Where("third_order_id = ?", thirdOrderID).First(&payment).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, ErrPaymentNotFound
}
payment, err := r.findPaymentForNotify(params)
if err != nil {
return nil, err
}
runtimeConfig, err := r.runtimeConfigForPayment(payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
verify, err := r.verifyNotify(payment, runtimeConfig, params, rawPayload, contentType)
if err != nil {
return nil, err
}
if amount := parseCent(params["amount"]); amount > 0 && amount != payment.AmountCent {
if err := r.recordNotifyDiagnostic(params, rawPayload, contentType, verify, "amount_mismatch"); err != nil {
if err := r.recordNotifyDiagnostic(payment.ID, params, rawPayload, contentType, verify, "amount_mismatch"); err != nil {
log.Printf("[payment] leshua notify diagnostic save failed third_order_id=%s err=%v", params["third_order_id"], err)
}
return nil, ErrPaymentVerifyFailed
}
raw := withNotifyDiagnostic(params, rawPayload, contentType, verify, "verified")
if err := r.applyChannelStatus(&payment, params["status"], params["pay_time"], raw, channelSourceNotify); err != nil {
if err := r.applyChannelStatus(payment, params["status"], params["pay_time"], raw, channelSourceNotify); err != nil {
return nil, err
}
return &NotifyResult{OK: true, Message: "000000"}, nil
@@ -307,9 +386,13 @@ func (r *Repository) StartRefund(orderID uint64, refundAmountCent int64, bizType
}
return nil, err
}
runtimeConfig, err := r.runtimeConfigForPayment(&originalPayment)
if err != nil {
return nil, ErrPaymentUnavailable
}
var existingRefund model.PaymentOrder
err := r.db.Where("order_id = ? AND biz_type = ? AND status NOT IN ('failed')", orderID, bizType).Order("id DESC").First(&existingRefund).Error
err = r.db.Where("order_id = ? AND biz_type = ? AND status NOT IN ('failed')", orderID, bizType).Order("id DESC").First(&existingRefund).Error
if err == nil {
dto := toRefundDTO(existingRefund)
return &dto, nil
@@ -329,8 +412,8 @@ func (r *Repository) StartRefund(orderID uint64, refundAmountCent int64, bizType
OrderID: orderID,
OrderNo: originalPayment.OrderNo,
UserID: originalPayment.UserID,
Provider: r.provider,
MerchantID: r.cfg.Leshua.MerchantID,
Provider: runtimeConfig.Provider,
MerchantID: runtimeConfig.MerchantID,
ThirdOrderID: merchantRefundID,
ProviderOrderID: "",
PayWay: originalPayment.PayWay,
@@ -340,7 +423,7 @@ func (r *Repository) StartRefund(orderID uint64, refundAmountCent int64, bizType
Status: "refunding",
}
if r.isMockMode {
if runtimeConfig.isMockMode() {
refundOrder.ProviderOrderID = "MOCKREF" + merchantRefundID
refundOrder.Status = "refunded"
now := time.Now()
@@ -351,6 +434,7 @@ func (r *Repository) StartRefund(orderID uint64, refundAmountCent int64, bizType
if err := r.db.Create(&refundOrder).Error; err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, &refundOrder)
if err := r.updateOrderRefundStatus(orderID, refundAmountCent); err != nil {
log.Printf("[payment] mock update order refund status failed order_id=%d err=%v", orderID, err)
}
@@ -361,16 +445,17 @@ func (r *Repository) StartRefund(orderID uint64, refundAmountCent int64, bizType
if err := r.db.Create(&refundOrder).Error; err != nil {
return nil, err
}
r.recordConfigUsage(runtimeConfig, &refundOrder)
if err := r.markOrderRefunding(orderID, refundAmountCent); err != nil {
log.Printf("[payment] mark order refunding failed order_id=%d err=%v", orderID, err)
}
resp, rawReq, err := r.leshua.CreateRefund(context.Background(), leshua.CreateRefundRequest{
resp, rawReq, err := runtimeConfig.client().CreateRefund(context.Background(), leshua.CreateRefundRequest{
ThirdOrderID: originalPayment.ThirdOrderID,
LeshuaOrderID: originalPayment.ProviderOrderID,
MerchantRefundID: merchantRefundID,
RefundAmountCent: refundAmountCent,
NotifyURL: r.cfg.Leshua.NotifyURL,
NotifyURL: runtimeConfig.Leshua.NotifyURL,
Attach: originalPayment.OrderNo,
})
if err != nil {
@@ -425,11 +510,15 @@ func (r *Repository) QueryRefundStatus(orderID uint64) (*RefundDTO, error) {
}
return nil, err
}
if payment.Status == "refunded" || payment.Status == "failed" || r.isMockMode {
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
if payment.Status == "refunded" || payment.Status == "failed" || runtimeConfig.isMockMode() {
dto := toRefundDTO(payment)
return &dto, nil
}
resp, err := r.leshua.QueryRefund(context.Background(), leshua.QueryRefundRequest{
resp, err := runtimeConfig.client().QueryRefund(context.Background(), leshua.QueryRefundRequest{
ThirdOrderID: payment.ThirdOrderID,
MerchantRefundID: payment.ThirdOrderID,
LeshuaRefundID: payment.ProviderOrderID,
@@ -465,25 +554,19 @@ func (r *Repository) QueryRefundStatus(orderID uint64) (*RefundDTO, error) {
// HandleRefundNotify 处理乐刷退款通知。
func (r *Repository) HandleRefundNotify(params map[string]string, rawPayload string, contentType string) (*NotifyResult, error) {
var verify leshua.VerifyNotifyResult
if !r.isMockMode {
verify = r.leshua.VerifyNotifyDetail(params)
if !verify.OK {
log.Printf("[payment] refund notify verify failed merchant_refund_id=%s", params["merchant_refund_id"])
return nil, ErrPaymentVerifyFailed
}
}
merchantRefundID := params["merchant_refund_id"]
if merchantRefundID == "" {
return nil, ErrPaymentNotFound
}
var payment model.PaymentOrder
if err := r.db.Where("third_order_id = ?", merchantRefundID).First(&payment).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, ErrPaymentNotFound
}
payment, err := r.findRefundPaymentForNotify(params)
if err != nil {
return nil, err
}
runtimeConfig, err := r.runtimeConfigForPayment(payment)
if err != nil {
return nil, ErrPaymentUnavailable
}
verify, err := r.verifyNotify(payment, runtimeConfig, params, rawPayload, contentType)
if err != nil {
return nil, err
}
raw := withNotifyDiagnostic(params, rawPayload, contentType, verify, "verified")
status := params["status"]
switch status {
@@ -570,7 +653,7 @@ func toRefundDTO(payment model.PaymentOrder) RefundDTO {
}
}
func (r *Repository) preparePayment(userID uint64, orderID uint64, req StartPaymentRequest) (*model.PaymentOrder, *model.RentalOrder, error) {
func (r *Repository) preparePayment(userID uint64, orderID uint64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (*model.PaymentOrder, *model.RentalOrder, error) {
var paymentID uint64
var orderRow model.RentalOrder
err := r.db.Transaction(func(tx *gorm.DB) error {
@@ -593,12 +676,12 @@ func (r *Repository) preparePayment(userID uint64, orderID uint64, req StartPaym
Order("id DESC").
First(&existing).Error
if err == nil {
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, r.cfg.Leshua.PayWay, "ZFBZF")
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, r.cfg.Leshua.JSPayFlag, "2")
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, runtimeConfig.Leshua.PayWay, "ZFBZF")
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, runtimeConfig.Leshua.JSPayFlag, "2")
existing.AmountCent = amountCent
existing.Provider = r.provider
existing.MerchantID = r.cfg.Leshua.MerchantID
if r.isMockMode && existing.ProviderOrderID == "" {
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
if existing.Provider != "leshua" && existing.ProviderOrderID == "" {
existing.ProviderOrderID = "MOCK" + existing.ThirdOrderID
}
if err := tx.Save(&existing).Error; err != nil {
@@ -620,17 +703,17 @@ func (r *Repository) preparePayment(userID uint64, orderID uint64, req StartPaym
OrderID: row.ID,
OrderNo: row.OrderNo,
UserID: row.RenterID,
Provider: r.provider,
MerchantID: r.cfg.Leshua.MerchantID,
Provider: runtimeConfig.Provider,
MerchantID: runtimeConfig.MerchantID,
ThirdOrderID: row.OrderNo,
ProviderOrderID: "",
PayWay: firstNonEmpty(req.PayWay, r.cfg.Leshua.PayWay, "ZFBZF"),
JSPayFlag: firstNonEmpty(req.JSPayFlag, r.cfg.Leshua.JSPayFlag, "2"),
PayWay: firstNonEmpty(req.PayWay, runtimeConfig.Leshua.PayWay, "ZFBZF"),
JSPayFlag: firstNonEmpty(req.JSPayFlag, runtimeConfig.Leshua.JSPayFlag, "2"),
AmountCent: amountCent,
BizType: "order_pay",
Status: "created",
}
if r.isMockMode {
if runtimeConfig.isMockMode() {
payment.ProviderOrderID = "MOCK" + row.OrderNo
payment.TDCode = "mock://leshua/pay/" + row.OrderNo
}
@@ -651,7 +734,7 @@ func (r *Repository) preparePayment(userID uint64, orderID uint64, req StartPaym
return payment, &orderRow, nil
}
func (r *Repository) createWalletRechargePayment(userID uint64, amountCent int64, req WalletRechargePaymentRequest) (*model.PaymentOrder, error) {
func (r *Repository) createWalletRechargePayment(userID uint64, amountCent int64, req WalletRechargePaymentRequest, runtimeConfig runtimePaymentConfig) (*model.PaymentOrder, error) {
paymentNo, err := newPaymentNo()
if err != nil {
return nil, err
@@ -661,17 +744,17 @@ func (r *Repository) createWalletRechargePayment(userID uint64, amountCent int64
OrderID: 0,
OrderNo: paymentNo,
UserID: userID,
Provider: r.provider,
MerchantID: r.cfg.Leshua.MerchantID,
Provider: runtimeConfig.Provider,
MerchantID: runtimeConfig.MerchantID,
ThirdOrderID: paymentNo,
ProviderOrderID: "",
PayWay: firstNonEmpty(req.PayWay, r.cfg.Leshua.PayWay, "ZFBZF"),
JSPayFlag: firstNonEmpty(req.JSPayFlag, r.cfg.Leshua.JSPayFlag, "2"),
PayWay: firstNonEmpty(req.PayWay, runtimeConfig.Leshua.PayWay, "ZFBZF"),
JSPayFlag: firstNonEmpty(req.JSPayFlag, runtimeConfig.Leshua.JSPayFlag, "2"),
AmountCent: amountCent,
BizType: "wallet_recharge",
Status: "created",
}
if r.isMockMode {
if runtimeConfig.isMockMode() {
payment.ProviderOrderID = "MOCK" + paymentNo
payment.TDCode = "mock://leshua/recharge/" + paymentNo
}
@@ -758,6 +841,70 @@ func (r *Repository) findPaymentByID(paymentID uint64) (*model.PaymentOrder, err
return &payment, nil
}
func (r *Repository) findPaymentForNotify(params map[string]string) (*model.PaymentOrder, error) {
thirdOrderID := params["third_order_id"]
if thirdOrderID == "" {
return nil, ErrPaymentNotFound
}
var payment model.PaymentOrder
if err := r.db.Where("third_order_id = ?", thirdOrderID).First(&payment).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, ErrPaymentNotFound
}
return nil, err
}
return &payment, nil
}
func (r *Repository) findRefundPaymentForNotify(params map[string]string) (*model.PaymentOrder, error) {
merchantRefundID := params["merchant_refund_id"]
if merchantRefundID == "" {
return nil, ErrPaymentNotFound
}
var payment model.PaymentOrder
if err := r.db.Where("third_order_id = ?", merchantRefundID).First(&payment).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, ErrPaymentNotFound
}
return nil, err
}
return &payment, nil
}
func (r *Repository) verifyNotify(payment *model.PaymentOrder, runtimeConfig *runtimePaymentConfig, params map[string]string, rawPayload string, contentType string) (leshua.VerifyNotifyResult, error) {
var verify leshua.VerifyNotifyResult
if runtimeConfig.isMockMode() {
return verify, nil
}
verify = runtimeConfig.client().VerifyNotifyDetail(params)
if !verify.OK {
log.Printf(
"[payment] leshua notify verify failed payment_id=%d third_order_id=%s got=%s expected=%s keys=%v base_string=%s",
payment.ID,
params["third_order_id"],
verify.Got,
verify.Expected["notify_key"],
verify.ParamKeys,
verify.BaseString["notify_key"],
)
if err := r.recordNotifyDiagnostic(payment.ID, params, rawPayload, contentType, verify, "verify_failed"); err != nil {
log.Printf("[payment] leshua notify diagnostic save failed payment_id=%d err=%v", payment.ID, err)
}
return verify, ErrPaymentVerifyFailed
}
log.Printf("[payment] leshua notify verified payment_id=%d third_order_id=%s matched_key=%s", payment.ID, params["third_order_id"], verify.MatchedKey)
return verify, nil
}
func (r *Repository) recordConfigUsage(runtimeConfig *runtimePaymentConfig, payment *model.PaymentOrder) {
if r.configRepo == nil || runtimeConfig == nil || payment == nil || runtimeConfig.ID == 0 {
return
}
if err := r.configRepo.RecordUsage(runtimeConfig.ID, payment.ID, runtimeConfig.Provider, runtimeConfig.MerchantID, payment.AmountCent, payment.BizType); err != nil {
log.Printf("[payment] record config usage failed config_id=%d payment_id=%d err=%v", runtimeConfig.ID, payment.ID, err)
}
}
func toDTO(payment model.PaymentOrder) PaymentDTO {
return PaymentDTO{
ID: payment.ID,
@@ -827,14 +974,13 @@ func withRawSource(raw map[string]string, source string) map[string]string {
return out
}
func (r *Repository) recordNotifyDiagnostic(params map[string]string, rawPayload string, contentType string, verify leshua.VerifyNotifyResult, status string) error {
thirdOrderID := params["third_order_id"]
if thirdOrderID == "" {
func (r *Repository) recordNotifyDiagnostic(paymentID uint64, params map[string]string, rawPayload string, contentType string, verify leshua.VerifyNotifyResult, status string) error {
if paymentID == 0 {
return nil
}
raw := withNotifyDiagnostic(params, rawPayload, contentType, verify, status)
return r.db.Model(&model.PaymentOrder{}).
Where("third_order_id = ?", thirdOrderID).
Where("id = ?", paymentID).
Update("raw_response", jsonMap(raw)).Error
}
@@ -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
}
+36 -2
View File
@@ -1,6 +1,7 @@
package router
import (
"os"
"strings"
"hfb_sys/backend/internal/config"
@@ -23,11 +24,12 @@ import (
"hfb_sys/backend/internal/modules/notification"
"hfb_sys/backend/internal/modules/order"
"hfb_sys/backend/internal/modules/payment"
"hfb_sys/backend/internal/modules/paymentaccount"
"hfb_sys/backend/internal/modules/paymentconfig"
"hfb_sys/backend/internal/modules/realname"
"hfb_sys/backend/internal/modules/systemconfig"
"hfb_sys/backend/internal/modules/user"
"hfb_sys/backend/internal/modules/wallet"
"hfb_sys/backend/internal/modules/paymentaccount"
"hfb_sys/backend/internal/modules/withdrawal"
_ "hfb_sys/backend/docs" // Swagger 文档
@@ -123,9 +125,32 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
}
withdrawalService := withdrawal.NewService(withdrawalRepo)
withdrawalHandler := withdrawal.NewHandler(withdrawalService)
// 支付配置管理
var paymentConfigRepo *paymentconfig.Repository
var paymentConfigService *paymentconfig.Service
var paymentConfigHandler *paymentconfig.Handler
if deps.DB != nil {
// 从环境变量获取加密密钥,如果没有则使用 MockEncryptor
encryptionKey := os.Getenv("PAYMENT_CONFIG_ENCRYPTION_KEY")
var encryptor paymentconfig.Encryptor
if encryptionKey != "" {
if aesEncryptor, err := paymentconfig.NewAESEncryptor(encryptionKey); err == nil {
encryptor = aesEncryptor
}
}
if encryptor == nil {
encryptor = &paymentconfig.MockEncryptor{}
logger.Warn("PAYMENT_CONFIG_ENCRYPTION_KEY not set or invalid, using MockEncryptor")
}
paymentConfigRepo = paymentconfig.NewRepository(deps.DB, encryptor)
paymentConfigService = paymentconfig.NewService(paymentConfigRepo)
paymentConfigHandler = paymentconfig.NewHandler(paymentConfigService)
}
var paymentRepo *payment.Repository
if deps.DB != nil {
paymentRepo = payment.NewRepository(deps.DB, cfg.Payment, orderRepo, walletRepo)
paymentRepo = payment.NewRepository(deps.DB, paymentConfigRepo, orderRepo, walletRepo)
}
paymentService := payment.NewService(paymentRepo)
paymentHandler := payment.NewHandler(paymentService)
@@ -395,6 +420,15 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
adminRoutes.POST("/withdrawals/:id/review", requirePerm("withdrawal:review"), withdrawalHandler.Review)
adminRoutes.POST("/withdrawals/:id/confirm-payment", requirePerm("withdrawal:pay"), withdrawalHandler.ConfirmPayment)
// 支付配置管理
if paymentConfigHandler != nil {
adminRoutes.GET("/payment-configs", requirePerm("payment_config:list"), paymentConfigHandler.List)
adminRoutes.GET("/payment-configs/:id", requirePerm("payment_config:list"), paymentConfigHandler.Get)
adminRoutes.POST("/payment-configs", requirePerm("payment_config:create"), paymentConfigHandler.Create)
adminRoutes.PUT("/payment-configs/:id", requirePerm("payment_config:update"), paymentConfigHandler.Update)
adminRoutes.DELETE("/payment-configs/:id", requirePerm("payment_config:delete"), paymentConfigHandler.Delete)
}
adminRoutes.GET("/system-configs", requirePerm("system_config:view"), systemConfigHandler.List)
adminRoutes.PUT("/system-configs/:key", requirePerm("system_config:update"), systemConfigHandler.Update)
adminRoutes.GET("/audit-logs", requirePerm("audit_log:view"), adminAuditHandler.List)