fix: resolve unique legacy payment providers

This commit is contained in:
IanShaw027
2026-04-20 21:09:38 +08:00
parent 32059ae9d5
commit bdcd3d87e5
4 changed files with 220 additions and 36 deletions

View File

@@ -4,14 +4,12 @@ import (
"context"
"fmt"
"log/slog"
"strconv"
"strings"
dbent "github.com/Wei-Shaw/sub2api/ent"
"github.com/Wei-Shaw/sub2api/ent/paymentorder"
"github.com/Wei-Shaw/sub2api/ent/paymentproviderinstance"
"github.com/Wei-Shaw/sub2api/internal/payment"
"github.com/Wei-Shaw/sub2api/internal/payment/provider"
)
// GetWebhookProvider returns the provider instance that should verify a webhook.
@@ -24,6 +22,13 @@ func (s *PaymentService) GetWebhookProvider(ctx context.Context, providerKey, ou
if psHasPinnedProviderInstance(order) {
return s.getPinnedOrderProvider(ctx, order)
}
inst, err := s.getOrderProviderInstance(ctx, order)
if err != nil {
return nil, fmt.Errorf("load order provider instance: %w", err)
}
if inst != nil {
return s.createProviderFromInstance(ctx, inst)
}
if !s.webhookRegistryFallbackAllowed(ctx, providerKey) {
return nil, fmt.Errorf("webhook provider fallback is ambiguous for %s", providerKey)
}
@@ -48,18 +53,7 @@ func (s *PaymentService) getPinnedOrderProvider(ctx context.Context, o *dbent.Pa
if inst == nil {
return nil, fmt.Errorf("order %d provider instance is missing", o.ID)
}
instID := strconv.FormatInt(int64(inst.ID), 10)
cfg, err := s.loadBalancer.GetInstanceConfig(ctx, int64(inst.ID))
if err != nil {
return nil, fmt.Errorf("load provider instance config: %w", err)
}
prov, err := provider.CreateProvider(inst.ProviderKey, instID, cfg)
if err != nil {
return nil, fmt.Errorf("create pinned provider: %w", err)
}
return prov, nil
return s.createProviderFromInstance(ctx, inst)
}
func (s *PaymentService) webhookRegistryFallbackAllowed(ctx context.Context, providerKey string) bool {