refactor(payment): 支付插件 - 宿主仅通过 RPC 包加载并保护订单版本
This commit is contained in:
1 parent
d6bbdbe27b
commit
38e715ea3f
45 files changed
+1584
-203
No files matched your search
@@ -154,6 +154,25 @@ func (r *Repository) PaymentOrderCountForProvider(providerID string) (int64, err
|
||||
return count, err
|
||||
}
|
||||
|
||||
// ActivePaymentOrderCountForPlugin reports orders that still need the plugin
|
||||
// runtime for create retries, callbacks, queries or close operations.
|
||||
func (r *Repository) ActivePaymentOrderCountForPlugin(pluginID, pluginVersion string) (int64, error) {
|
||||
query := r.db.Model(&model.PaymentOrder{}).Where("plugin_id = ? AND status IN ?", strings.TrimSpace(pluginID), []model.PaymentOrderStatus{
|
||||
model.PaymentOrderCreated, model.PaymentOrderPending, model.PaymentOrderClosing, model.PaymentOrderCreateFailed,
|
||||
})
|
||||
if strings.TrimSpace(pluginVersion) != "" {
|
||||
// Rows created before plugin version pinning have an empty version. Keep
|
||||
// them protected during upgrades because they still depend on this
|
||||
// plugin's historical runtime.
|
||||
query = query.Where("(plugin_version = ? OR plugin_version = '' OR plugin_version IS NULL)", strings.TrimSpace(pluginVersion))
|
||||
}
|
||||
var count int64
|
||||
if err := query.Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SaveVerifiedPaymentNotification(notification *model.PaymentNotification) (bool, error) {
|
||||
created := r.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "provider_id"}, {Name: "provider_event_id"}}, DoNothing: true,
|
||||
|
||||
@@ -59,6 +59,29 @@ func TestCompletePaymentOrderGrantsCreditsExactlyOnce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivePaymentOrderCountForPluginScopesVersionAndState(t *testing.T) {
|
||||
db := openPaymentTestDB(t)
|
||||
repo := New(db)
|
||||
orders := []model.PaymentOrder{
|
||||
{ID: "plugin-v1-pending", UserID: "u1", IdempotencyKey: "i1", MerchantOrderNo: "m1", ProviderID: "p1", PluginID: "plugin", PluginVersion: "1.0.0", Status: model.PaymentOrderPending},
|
||||
{ID: "plugin-legacy-pending", UserID: "u5", IdempotencyKey: "i5", MerchantOrderNo: "m5", ProviderID: "p1", PluginID: "plugin", Status: model.PaymentOrderPending},
|
||||
{ID: "plugin-v1-credited", UserID: "u2", IdempotencyKey: "i2", MerchantOrderNo: "m2", ProviderID: "p1", PluginID: "plugin", PluginVersion: "1.0.0", Status: model.PaymentOrderCredited},
|
||||
{ID: "plugin-v2-pending", UserID: "u3", IdempotencyKey: "i3", MerchantOrderNo: "m3", ProviderID: "p1", PluginID: "plugin", PluginVersion: "2.0.0", Status: model.PaymentOrderPending},
|
||||
{ID: "other-pending", UserID: "u4", IdempotencyKey: "i4", MerchantOrderNo: "m4", ProviderID: "p2", PluginID: "other", PluginVersion: "1.0.0", Status: model.PaymentOrderPending},
|
||||
}
|
||||
if err := db.Create(&orders).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count, err := repo.ActivePaymentOrderCountForPlugin("plugin", "1.0.0")
|
||||
if err != nil || count != 2 {
|
||||
t.Fatalf("version-scoped active count = %d, error = %v", count, err)
|
||||
}
|
||||
count, err = repo.ActivePaymentOrderCountForPlugin("plugin", "")
|
||||
if err != nil || count != 3 {
|
||||
t.Fatalf("all-version active count = %d, error = %v", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletePaymentOrderRejectsAmountMismatchWithoutGrant(t *testing.T) {
|
||||
db := openPaymentTestDB(t)
|
||||
repo := New(db)
|
||||
|
||||
Reference in new issue
Block a user