fix: resolve unique legacy payment providers
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dbent "github.com/Wei-Shaw/sub2api/ent"
|
||||
@@ -237,29 +236,38 @@ func (s *PaymentService) ExpireTimedOutOrders(ctx context.Context) (int, error)
|
||||
// getOrderProvider creates a provider using the order's original instance config.
|
||||
// Falls back to registry lookup if instance ID is missing (legacy orders).
|
||||
func (s *PaymentService) getOrderProvider(ctx context.Context, o *dbent.PaymentOrder) (payment.Provider, error) {
|
||||
if o.ProviderInstanceID != nil && *o.ProviderInstanceID != "" {
|
||||
instID, err := strconv.ParseInt(*o.ProviderInstanceID, 10, 64)
|
||||
if err == nil {
|
||||
cfg, err := s.loadBalancer.GetInstanceConfig(ctx, instID)
|
||||
if err == nil {
|
||||
providerKey := strings.TrimSpace(psStringValue(o.ProviderKey))
|
||||
if providerKey == "" {
|
||||
providerKey = s.registry.GetProviderKey(o.PaymentType)
|
||||
}
|
||||
if providerKey == "" {
|
||||
providerKey = o.PaymentType
|
||||
}
|
||||
p, err := provider.CreateProvider(providerKey, *o.ProviderInstanceID, cfg)
|
||||
if err == nil {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
inst, err := s.getOrderProviderInstance(ctx, o)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load order provider instance: %w", err)
|
||||
}
|
||||
if inst != nil {
|
||||
return s.createProviderFromInstance(ctx, inst)
|
||||
}
|
||||
s.EnsureProviders(ctx)
|
||||
return s.registry.GetProvider(o.PaymentType)
|
||||
}
|
||||
|
||||
func (s *PaymentService) createProviderFromInstance(ctx context.Context, inst *dbent.PaymentProviderInstance) (payment.Provider, error) {
|
||||
if inst == nil {
|
||||
return nil, fmt.Errorf("payment provider instance is missing")
|
||||
}
|
||||
|
||||
cfg, err := s.loadBalancer.GetInstanceConfig(ctx, int64(inst.ID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load provider instance config: %w", err)
|
||||
}
|
||||
if inst.PaymentMode != "" {
|
||||
cfg["paymentMode"] = inst.PaymentMode
|
||||
}
|
||||
|
||||
instID := strconv.FormatInt(int64(inst.ID), 10)
|
||||
prov, err := provider.CreateProvider(inst.ProviderKey, instID, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create provider from instance: %w", err)
|
||||
}
|
||||
return prov, nil
|
||||
}
|
||||
|
||||
func psStringValue(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user