修复支付取消与成功统计
This commit is contained in:
@@ -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