fix: tighten legacy payment provider resolution

This commit is contained in:
IanShaw027
2026-04-20 21:46:24 +08:00
parent 422f60a145
commit b309822199
2 changed files with 87 additions and 15 deletions

View File

@@ -21,7 +21,7 @@ import (
// getOrderProviderInstance looks up the provider instance that processed this order.
// For legacy orders without provider_instance_id, it resolves only when the
// enabled instance is uniquely identifiable from the stored order fields.
// historical instance is uniquely identifiable from the stored order fields.
func (s *PaymentService) getOrderProviderInstance(ctx context.Context, o *dbent.PaymentOrder) (*dbent.PaymentProviderInstance, error) {
if s == nil || s.entClient == nil || o == nil {
return nil, nil
@@ -40,47 +40,55 @@ func (s *PaymentService) getOrderProviderInstance(ctx context.Context, o *dbent.
}
func (s *PaymentService) resolveUniqueLegacyOrderProviderInstance(ctx context.Context, o *dbent.PaymentOrder) (*dbent.PaymentProviderInstance, error) {
paymentType := payment.GetBasePaymentType(strings.TrimSpace(o.PaymentType))
providerKey := strings.TrimSpace(psStringValue(o.ProviderKey))
if providerKey != "" {
instances, err := s.entClient.PaymentProviderInstance.Query().
Where(
paymentproviderinstance.EnabledEQ(true),
paymentproviderinstance.ProviderKeyEQ(providerKey),
).
Where(paymentproviderinstance.ProviderKeyEQ(providerKey)).
All(ctx)
if err != nil {
return nil, err
}
if len(instances) == 1 {
return instances[0], nil
matched := psFilterLegacyOrderProviderInstances(paymentType, instances)
if len(matched) == 1 {
return matched[0], nil
}
return nil, nil
}
paymentType := payment.GetBasePaymentType(strings.TrimSpace(o.PaymentType))
if paymentType == "" {
return nil, nil
}
instances, err := s.entClient.PaymentProviderInstance.Query().
Where(paymentproviderinstance.EnabledEQ(true)).
All(ctx)
if err != nil {
return nil, err
}
var matched []*dbent.PaymentProviderInstance
for _, inst := range instances {
if psLegacyOrderMatchesInstance(paymentType, inst) {
matched = append(matched, inst)
}
}
matched := psFilterLegacyOrderProviderInstances(paymentType, instances)
if len(matched) == 1 {
return matched[0], nil
}
return nil, nil
}
func psFilterLegacyOrderProviderInstances(orderPaymentType string, instances []*dbent.PaymentProviderInstance) []*dbent.PaymentProviderInstance {
if len(instances) == 0 {
return nil
}
if strings.TrimSpace(orderPaymentType) == "" {
return instances
}
var matched []*dbent.PaymentProviderInstance
for _, inst := range instances {
if psLegacyOrderMatchesInstance(orderPaymentType, inst) {
matched = append(matched, inst)
}
}
return matched
}
func psLegacyOrderMatchesInstance(orderPaymentType string, inst *dbent.PaymentProviderInstance) bool {
if inst == nil {
return false