删除钱包充值功能并消除重复入账风险
钱包充值为开发态测试功能(生产环境本就禁用),且支付回调存在重复入账风险:入账与标记 paid 两步非原子、wallet_ledger 去重无唯一索引、幂等键使用了可变的 ProviderOrderID。直接删除该功能从根本上消除风险。 后端: - payment 移除 StartWalletRecharge/QueryWalletRecharge 及相关 handler/DTO/常量;confirmPaid 增加 OrderID 守卫;解除对 wallet 仓库的依赖 - wallet 移除 Recharge/ConfirmRechargeFromChannel 及相关定义 - 移除三条充值路由;adminfinance 财务统计口径只统计 order_pay - 清理充值相关测试用例 前端: - 移除充值 API、WalletView 充值面板/弹窗、admin 充值标签与筛选 - 保留钱包余额、流水、提现等核心能力 go build/vet 与 vue-tsc typecheck 均通过。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3ef7705776
commit
e8621728fd
@@ -9,10 +9,6 @@ type AccountDTO struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type RechargeRequest struct {
|
||||
AmountCent int64 `json:"amount_cent" binding:"required"`
|
||||
}
|
||||
|
||||
type WithdrawRequest struct {
|
||||
AmountCent int64 `json:"amount_cent" binding:"required"`
|
||||
}
|
||||
|
||||
@@ -62,25 +62,6 @@ func (h *Handler) Ledger(c *gin.Context) {
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
func (h *Handler) Recharge(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
var req RechargeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "充值金额不正确")
|
||||
return
|
||||
}
|
||||
account, err := h.service.Recharge(c.Request.Context(), userID, req)
|
||||
if err != nil {
|
||||
writeWalletError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, account)
|
||||
}
|
||||
|
||||
func (h *Handler) Withdraw(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
@@ -155,8 +136,6 @@ func writeWalletError(c *gin.Context, err error) {
|
||||
response.BadRequest(c, "充值金额不正确")
|
||||
case errors.Is(err, ErrInsufficientBalance):
|
||||
response.Error(c, 409, "insufficient_balance", "钱包余额不足")
|
||||
case errors.Is(err, ErrRechargeDisabled):
|
||||
response.Error(c, 410, "wallet_recharge_disabled", "钱包充值已关闭")
|
||||
case errors.Is(err, ErrFeaturePending):
|
||||
response.Error(c, 501, "feature_pending", "提现功能待开发")
|
||||
default:
|
||||
|
||||
@@ -64,59 +64,6 @@ func (r *Repository) Ledger(ctx context.Context, userID uint64, page, pageSize i
|
||||
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Recharge(ctx context.Context, userID uint64, amountCent int64) (*AccountDTO, error) {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
return AppendEntries(tx, Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: amountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "dev_recharge",
|
||||
BizNo: "DEV",
|
||||
Remark: "开发环境充值",
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Account(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) ConfirmRechargeFromChannel(ctx context.Context, userID uint64, bizNo string, amountCent int64) error {
|
||||
if userID == 0 || amountCent <= 0 || bizNo == "" {
|
||||
return ErrInvalidAmount
|
||||
}
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := ensureAccount(tx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
var account model.WalletAccount
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("user_id = ?", userID).
|
||||
First(&account).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var existing int64
|
||||
if err := tx.Model(&model.WalletLedger{}).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_no = ?", userID, "channel_recharge", bizNo).
|
||||
Count(&existing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if existing > 0 {
|
||||
return nil
|
||||
}
|
||||
return AppendEntries(tx, Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: amountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "channel_recharge",
|
||||
BizNo: bizNo,
|
||||
Remark: "渠道充值入账",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Withdraw 保留仓库能力;当前公开提现入口在 service 层标记为待开发,不会调用到这里。
|
||||
func (r *Repository) Withdraw(ctx context.Context, userID uint64, amountCent int64) (*AccountDTO, error) {
|
||||
if userID == 0 || amountCent <= 0 {
|
||||
|
||||
@@ -64,113 +64,6 @@ func TestRepositoryAccountCreatesAccountIfNotExists(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRepositoryRechargeIncreasesAvailableBalance 测试充值增加可用余额
|
||||
func TestRepositoryRechargeIncreasesAvailableBalance(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer cleanupTestDB(t, db)
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctx := context.Background()
|
||||
userID := uint64(1002)
|
||||
|
||||
// 第一次充值
|
||||
account, err := repo.Recharge(ctx, userID, 10000)
|
||||
if err != nil {
|
||||
t.Fatalf("Recharge() error = %v", err)
|
||||
}
|
||||
if account.AvailableBalanceCent != 10000 {
|
||||
t.Fatalf("第一次充值后余额 = %d, want 10000", account.AvailableBalanceCent)
|
||||
}
|
||||
|
||||
// 第二次充值
|
||||
account, err = repo.Recharge(ctx, userID, 5000)
|
||||
if err != nil {
|
||||
t.Fatalf("Recharge() error = %v", err)
|
||||
}
|
||||
if account.AvailableBalanceCent != 15000 {
|
||||
t.Fatalf("第二次充值后余额 = %d, want 15000", account.AvailableBalanceCent)
|
||||
}
|
||||
|
||||
// 验证账本记录
|
||||
ledger, err := repo.Ledger(ctx, userID, 1, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Ledger() error = %v", err)
|
||||
}
|
||||
if ledger.Total != 2 {
|
||||
t.Fatalf("账本记录数 = %d, want 2", ledger.Total)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRepositoryConfirmRechargeFromChannelIsIdempotent 测试渠道充值幂等性
|
||||
func TestRepositoryConfirmRechargeFromChannelIsIdempotent(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer cleanupTestDB(t, db)
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctx := context.Background()
|
||||
userID := uint64(1003)
|
||||
bizNo := "PAY123456"
|
||||
amount := int64(10000)
|
||||
|
||||
// 第一次确认充值
|
||||
err := repo.ConfirmRechargeFromChannel(ctx, userID, bizNo, amount)
|
||||
if err != nil {
|
||||
t.Fatalf("第一次 ConfirmRechargeFromChannel() error = %v", err)
|
||||
}
|
||||
|
||||
account, _ := repo.Account(ctx, userID)
|
||||
if account.AvailableBalanceCent != amount {
|
||||
t.Fatalf("第一次充值后余额 = %d, want %d", account.AvailableBalanceCent, amount)
|
||||
}
|
||||
|
||||
// 第二次确认充值(相同 bizNo)应该幂等,不重复入账
|
||||
err = repo.ConfirmRechargeFromChannel(ctx, userID, bizNo, amount)
|
||||
if err != nil {
|
||||
t.Fatalf("第二次 ConfirmRechargeFromChannel() error = %v", err)
|
||||
}
|
||||
|
||||
account, _ = repo.Account(ctx, userID)
|
||||
if account.AvailableBalanceCent != amount {
|
||||
t.Fatalf("第二次充值后余额 = %d, want %d(应保持不变)", account.AvailableBalanceCent, amount)
|
||||
}
|
||||
|
||||
// 验证只有一条账本记录
|
||||
ledger, _ := repo.Ledger(ctx, userID, 1, 10)
|
||||
if ledger.Total != 1 {
|
||||
t.Fatalf("账本记录数 = %d, want 1(幂等)", ledger.Total)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRepositoryConfirmRechargeFromChannelRejectsInvalidParams 测试参数验证
|
||||
func TestRepositoryConfirmRechargeFromChannelRejectsInvalidParams(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer cleanupTestDB(t, db)
|
||||
|
||||
repo := NewRepository(db)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
userID uint64
|
||||
bizNo string
|
||||
amount int64
|
||||
wantError error
|
||||
}{
|
||||
{"userID 为 0", 0, "BIZ123", 1000, ErrInvalidAmount},
|
||||
{"bizNo 为空", 1004, "", 1000, ErrInvalidAmount},
|
||||
{"amount 为 0", 1004, "BIZ123", 0, ErrInvalidAmount},
|
||||
{"amount 为负数", 1004, "BIZ123", -1000, ErrInvalidAmount},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := repo.ConfirmRechargeFromChannel(context.Background(), tc.userID, tc.bizNo, tc.amount)
|
||||
if err != tc.wantError {
|
||||
t.Fatalf("error = %v, want %v", err, tc.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAppendEntriesUpdatesBalanceCorrectly 测试 AppendEntries 余额计算
|
||||
func TestAppendEntriesUpdatesBalanceCorrectly(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
|
||||
@@ -10,12 +10,8 @@ var (
|
||||
ErrInvalidAmount = errors.New("invalid amount")
|
||||
ErrInsufficientBalance = errors.New("insufficient balance")
|
||||
ErrFeaturePending = errors.New("feature pending")
|
||||
ErrRechargeDisabled = errors.New("wallet recharge disabled")
|
||||
)
|
||||
|
||||
// MinRechargeAmountCent 最小充值金额:1分
|
||||
const MinRechargeAmountCent = 1
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
@@ -38,13 +34,6 @@ func (s *Service) Ledger(ctx context.Context, userID uint64, page, pageSize int)
|
||||
return s.repo.Ledger(ctx, userID, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) Recharge(ctx context.Context, userID uint64, req RechargeRequest) (*AccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return nil, ErrRechargeDisabled
|
||||
}
|
||||
|
||||
func (s *Service) Withdraw(ctx context.Context, userID uint64, req WithdrawRequest) (*AccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
|
||||
@@ -25,15 +25,6 @@ func TestServiceLedgerWithNilRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRechargeIsDisabled(t *testing.T) {
|
||||
svc := &Service{repo: &Repository{}}
|
||||
ctx := context.Background()
|
||||
_, err := svc.Recharge(ctx, 1, RechargeRequest{AmountCent: 100})
|
||||
if !errors.Is(err, ErrRechargeDisabled) {
|
||||
t.Fatalf("Recharge() error = %v, want ErrRechargeDisabled", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceWithdrawIsPending(t *testing.T) {
|
||||
svc := &Service{repo: &Repository{}}
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user