钱包充值为开发态测试功能(生产环境本就禁用),且支付回调存在重复入账风险:入账与标记 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>
65 lines
2.4 KiB
Go
65 lines
2.4 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"hfb_sys/backend/internal/model"
|
|
"time"
|
|
)
|
|
|
|
func (r *Repository) applyChannelStatus(ctx context.Context, payment *model.PaymentOrder, status string, payTime string, raw map[string]string, source string) error {
|
|
switch status {
|
|
case "paid":
|
|
paidAt := parseChannelTime(payTime)
|
|
if paidAt == nil {
|
|
now := time.Now()
|
|
paidAt = &now
|
|
}
|
|
return r.confirmPaid(ctx, payment, status, *paidAt, raw, source)
|
|
case "closed":
|
|
return r.updateChannelStatus(ctx, payment.ID, "closed", raw, source)
|
|
case "failed":
|
|
return r.updateChannelStatus(ctx, payment.ID, "failed", raw, source)
|
|
default:
|
|
return r.updateChannelStatus(ctx, payment.ID, "paying", raw, source)
|
|
}
|
|
}
|
|
func (r *Repository) updateChannelStatus(ctx context.Context, paymentID uint64, status string, raw map[string]string, source string) error {
|
|
updates := map[string]any{
|
|
"status": status,
|
|
"raw_response": jsonMap(withRawSource(raw, source)),
|
|
}
|
|
if source == channelSourceNotify {
|
|
updates["notified_at"] = time.Now()
|
|
}
|
|
return r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", paymentID).Updates(updates).Error
|
|
}
|
|
func (r *Repository) confirmPaid(ctx context.Context, payment *model.PaymentOrder, status string, paidAt time.Time, raw map[string]string, source string) error {
|
|
if payment.Status != "paid" && payment.OrderID != 0 {
|
|
if r.orderRepo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if err := r.orderRepo.ConfirmPaidFromChannel(ctx, payment.OrderID, firstNonEmpty(payment.ProviderOrderID, payment.PaymentNo)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
updates := map[string]any{
|
|
"status": "paid",
|
|
"provider_order_id": firstNonEmpty(raw["provider_order_id"], raw["leshua_order_id"], raw["pay_order_no"], raw["trade_no"], payment.ProviderOrderID),
|
|
"raw_response": jsonMap(withRawSource(raw, source)),
|
|
"paid_at": paidAt,
|
|
}
|
|
if source == channelSourceNotify {
|
|
updates["notified_at"] = time.Now()
|
|
}
|
|
return r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(updates).Error
|
|
}
|
|
func (r *Repository) markPaymentFailed(ctx context.Context, paymentID uint64, raw map[string]string, message string) error {
|
|
if raw == nil {
|
|
raw = map[string]string{"error": message}
|
|
}
|
|
return r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", paymentID).Updates(map[string]any{
|
|
"status": "failed",
|
|
"raw_response": jsonMap(raw),
|
|
}).Error
|
|
}
|