修复支付取消与成功统计
This commit is contained in:
@@ -2,7 +2,9 @@ package paymentconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
@@ -61,6 +63,9 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
}
|
||||
dtos = append(dtos, dto)
|
||||
}
|
||||
if err := r.applySuccessfulPaymentStats(ctx, dtos); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return dtos, total, nil
|
||||
}
|
||||
@@ -78,6 +83,11 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
statDTOs := []ConfigDTO{dto}
|
||||
if err := r.applySuccessfulPaymentStats(ctx, statDTOs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto = statDTOs[0]
|
||||
if includeSecret {
|
||||
if err := appendAuditLog(r.db.WithContext(ctx), actorID, "payment_config.view_secret", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
@@ -90,6 +100,103 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
type paymentConfigSuccessStat struct {
|
||||
ConfigID uint64
|
||||
TotalTransactions int64
|
||||
TotalAmountCent int64
|
||||
LastPaidAt nullableTime
|
||||
}
|
||||
|
||||
// applySuccessfulPaymentStats 使用真实成功支付流水覆盖配置统计,避免仅打开二维码也被算作成交。
|
||||
func (r *Repository) applySuccessfulPaymentStats(ctx context.Context, dtos []ConfigDTO) error {
|
||||
if len(dtos) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uint64, 0, len(dtos))
|
||||
indexByID := make(map[uint64]int, len(dtos))
|
||||
for idx, dto := range dtos {
|
||||
ids = append(ids, dto.ID)
|
||||
indexByID[dto.ID] = idx
|
||||
dtos[idx].TotalTransactions = 0
|
||||
dtos[idx].TotalAmountCent = 0
|
||||
dtos[idx].LastUsedAt = nil
|
||||
}
|
||||
|
||||
var rows []paymentConfigSuccessStat
|
||||
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).
|
||||
Select("payment_config_id AS config_id, COUNT(*) AS total_transactions, COALESCE(SUM(amount_cent), 0) AS total_amount_cent, MAX(paid_at) AS last_paid_at").
|
||||
Where("payment_config_id IN ? AND biz_type = ? AND status = ?", ids, "order_pay", "paid").
|
||||
Group("payment_config_id").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range rows {
|
||||
idx, ok := indexByID[row.ConfigID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
dtos[idx].TotalTransactions = row.TotalTransactions
|
||||
dtos[idx].TotalAmountCent = row.TotalAmountCent
|
||||
if row.LastPaidAt.Time != nil {
|
||||
value := row.LastPaidAt.Time.Format(time.RFC3339)
|
||||
dtos[idx].LastUsedAt = &value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type nullableTime struct {
|
||||
Time *time.Time
|
||||
}
|
||||
|
||||
// Value 实现 driver.Valuer,让 GORM 能把 nullableTime 当作普通扫描字段处理。
|
||||
func (nt nullableTime) Value() (driver.Value, error) {
|
||||
if nt.Time == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return *nt.Time, nil
|
||||
}
|
||||
|
||||
// Scan 兼容 MySQL 的 time.Time 和 SQLite 聚合函数返回的字符串时间。
|
||||
func (nt *nullableTime) Scan(value any) error {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
nt.Time = nil
|
||||
case time.Time:
|
||||
nt.Time = &v
|
||||
case []byte:
|
||||
return nt.scanString(string(v))
|
||||
case string:
|
||||
return nt.scanString(v)
|
||||
default:
|
||||
nt.Time = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nt *nullableTime) scanString(value string) error {
|
||||
if value == "" {
|
||||
nt.Time = nil
|
||||
return nil
|
||||
}
|
||||
for _, layout := range []string{
|
||||
time.RFC3339Nano,
|
||||
time.RFC3339,
|
||||
"2006-01-02 15:04:05.999999999-07:00",
|
||||
"2006-01-02 15:04:05-07:00",
|
||||
"2006-01-02 15:04:05.999999999",
|
||||
"2006-01-02 15:04:05",
|
||||
} {
|
||||
parsed, err := time.Parse(layout, value)
|
||||
if err == nil {
|
||||
nt.Time = &parsed
|
||||
return nil
|
||||
}
|
||||
}
|
||||
nt.Time = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindRuntimeByID 根据配置 ID 查询运行时配置,不写审计日志,供支付单回溯原配置使用。
|
||||
func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
package paymentconfig
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func TestImportCreateRequestKeepsOneActiveConfigPerPayWay(t *testing.T) {
|
||||
configs := []ConfigDTO{
|
||||
@@ -38,3 +47,75 @@ func TestImportCreateRequestPreservesTestingConfig(t *testing.T) {
|
||||
t.Fatalf("status/default = %s/%v, want testing/false", req.Status, req.IsDefault)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListUsesSuccessfulPaymentStats 验证配置页只统计成功支付,不把打开二维码或取消订单算作成交。
|
||||
func TestListUsesSuccessfulPaymentStats(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
if err != nil {
|
||||
t.Fatalf("open db failed: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.PaymentMerchantConfig{}, &model.PaymentOrder{}); err != nil {
|
||||
t.Fatalf("migrate failed: %v", err)
|
||||
}
|
||||
config := model.PaymentMerchantConfig{
|
||||
Name: "拉卡拉微信",
|
||||
Provider: "lakala",
|
||||
MerchantID: "M1",
|
||||
PayWay: "WXZF",
|
||||
Status: "active",
|
||||
Environment: "production",
|
||||
TotalTransactions: 99,
|
||||
TotalAmountCent: 999999,
|
||||
}
|
||||
if err := db.Create(&config).Error; err != nil {
|
||||
t.Fatalf("create config failed: %v", err)
|
||||
}
|
||||
paidAt := time.Date(2026, 6, 14, 23, 36, 7, 0, time.UTC)
|
||||
payments := []model.PaymentOrder{
|
||||
{
|
||||
PaymentNo: "PAY-SUCCESS",
|
||||
OrderID: 1,
|
||||
OrderNo: "ORD-SUCCESS",
|
||||
UserID: 2,
|
||||
PaymentConfigID: config.ID,
|
||||
Provider: "lakala",
|
||||
MerchantID: "M1",
|
||||
ThirdOrderID: "PAY-SUCCESS",
|
||||
AmountCent: 10130,
|
||||
BizType: "order_pay",
|
||||
Status: "paid",
|
||||
PaidAt: &paidAt,
|
||||
},
|
||||
{
|
||||
PaymentNo: "PAY-CANCELLED",
|
||||
OrderID: 2,
|
||||
OrderNo: "ORD-CANCELLED",
|
||||
UserID: 2,
|
||||
PaymentConfigID: config.ID,
|
||||
Provider: "lakala",
|
||||
MerchantID: "M1",
|
||||
ThirdOrderID: "PAY-CANCELLED",
|
||||
AmountCent: 10130,
|
||||
BizType: "order_pay",
|
||||
Status: "closed",
|
||||
},
|
||||
}
|
||||
if err := db.Create(&payments).Error; err != nil {
|
||||
t.Fatalf("create payments failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db, nil)
|
||||
|
||||
items, total, err := repo.List(t.Context(), ListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("List() error = %v", err)
|
||||
}
|
||||
if total != 1 || len(items) != 1 {
|
||||
t.Fatalf("items/total = %d/%d, want 1/1", len(items), total)
|
||||
}
|
||||
if items[0].TotalTransactions != 1 || items[0].TotalAmountCent != 10130 {
|
||||
t.Fatalf("success stats = %d/%d, want 1/10130", items[0].TotalTransactions, items[0].TotalAmountCent)
|
||||
}
|
||||
if items[0].LastUsedAt == nil {
|
||||
t.Fatal("last success pay time should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user