[Greenfield] Select default payoutMethodId if none are selected in the Refund route (#6315)

This commit is contained in:
Nicolas Dorier 2024-10-17 22:54:59 +09:00 committed by GitHub
parent 77fba4aee3
commit 7b6a115adc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 28 deletions

View File

@ -51,6 +51,7 @@ namespace BTCPayServer.Controllers.Greenfield
private readonly Dictionary<PaymentMethodId, IPaymentLinkExtension> _paymentLinkExtensions;
private readonly PayoutMethodHandlerDictionary _payoutHandlers;
private readonly PaymentMethodHandlerDictionary _handlers;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly DefaultRulesCollection _defaultRules;
public LanguageService LanguageService { get; }
@ -65,6 +66,7 @@ namespace BTCPayServer.Controllers.Greenfield
Dictionary<PaymentMethodId, IPaymentLinkExtension> paymentLinkExtensions,
PayoutMethodHandlerDictionary payoutHandlers,
PaymentMethodHandlerDictionary handlers,
BTCPayNetworkProvider networkProvider,
DefaultRulesCollection defaultRules)
{
_invoiceController = invoiceController;
@ -79,6 +81,7 @@ namespace BTCPayServer.Controllers.Greenfield
_paymentLinkExtensions = paymentLinkExtensions;
_payoutHandlers = payoutHandlers;
_handlers = handlers;
_networkProvider = networkProvider;
_defaultRules = defaultRules;
LanguageService = languageService;
}
@ -338,6 +341,9 @@ namespace BTCPayServer.Controllers.Greenfield
}
PaymentPrompt? paymentPrompt = null;
PayoutMethodId? payoutMethodId = null;
if (request.PayoutMethodId is null)
request.PayoutMethodId = invoice.GetDefaultPaymentMethodId(store, _networkProvider)?.ToString();
if (request.PayoutMethodId is not null && PayoutMethodId.TryParse(request.PayoutMethodId, out payoutMethodId))
{
var supported = _payoutHandlers.GetSupportedPayoutMethods(store);

View File

@ -764,34 +764,7 @@ namespace BTCPayServer.Controllers
paymentMethodId = null;
if (paymentMethodId is null)
{
PaymentMethodId? invoicePaymentId = invoice.DefaultPaymentMethod;
PaymentMethodId? storePaymentId = store.GetDefaultPaymentId();
if (invoicePaymentId is not null)
{
if (displayedPaymentMethods.Contains(invoicePaymentId))
paymentMethodId = invoicePaymentId;
}
if (paymentMethodId is null && storePaymentId is not null)
{
if (displayedPaymentMethods.Contains(storePaymentId))
paymentMethodId = storePaymentId;
}
if (paymentMethodId is null && invoicePaymentId is not null)
{
paymentMethodId = invoicePaymentId.FindNearest(displayedPaymentMethods);
}
if (paymentMethodId is null && storePaymentId is not null)
{
paymentMethodId = storePaymentId.FindNearest(displayedPaymentMethods);
}
if (paymentMethodId is null)
{
var defaultBTC = PaymentTypes.CHAIN.GetPaymentMethodId(_NetworkProvider.DefaultNetwork.CryptoCode);
var defaultLNURLPay = PaymentTypes.LNURL.GetPaymentMethodId(_NetworkProvider.DefaultNetwork.CryptoCode);
paymentMethodId = displayedPaymentMethods.FirstOrDefault(e => e == defaultBTC) ??
displayedPaymentMethods.FirstOrDefault(e => e == defaultLNURLPay) ??
displayedPaymentMethods.FirstOrDefault();
}
paymentMethodId = invoice.GetDefaultPaymentMethodId(store, _NetworkProvider, displayedPaymentMethods);
isDefaultPaymentId = true;
}
if (paymentMethodId is null)

View File

@ -371,6 +371,45 @@ namespace BTCPayServer.Services.Invoices
get;
set;
} = new Dictionary<string, decimal>();
#nullable enable
public PaymentMethodId? GetDefaultPaymentMethodId(Data.StoreData store, BTCPayNetworkProvider networkProvider, HashSet<PaymentMethodId>? authorized = null)
{
PaymentMethodId? paymentMethodId = null;
PaymentMethodId? invoicePaymentId = DefaultPaymentMethod;
PaymentMethodId? storePaymentId = store.GetDefaultPaymentId();
authorized ??= GetPaymentPrompts().Select(p => p.PaymentMethodId).ToHashSet();
if (invoicePaymentId is not null)
{
if (authorized.Contains(invoicePaymentId))
paymentMethodId = invoicePaymentId;
}
if (paymentMethodId is null && storePaymentId is not null)
{
if (authorized.Contains(storePaymentId))
paymentMethodId = storePaymentId;
}
if (paymentMethodId is null && invoicePaymentId is not null)
{
paymentMethodId = invoicePaymentId.FindNearest(authorized);
}
if (paymentMethodId is null && storePaymentId is not null)
{
paymentMethodId = storePaymentId.FindNearest(authorized);
}
if (paymentMethodId is null)
{
var defaultBTC = PaymentTypes.CHAIN.GetPaymentMethodId(networkProvider.DefaultNetwork.CryptoCode);
var defaultLNURLPay = PaymentTypes.LNURL.GetPaymentMethodId(networkProvider.DefaultNetwork.CryptoCode);
paymentMethodId = authorized.FirstOrDefault(e => e == defaultBTC) ??
authorized.FirstOrDefault(e => e == defaultLNURLPay) ??
authorized.FirstOrDefault();
}
return paymentMethodId;
}
#nullable restore
public void UpdateTotals()
{
PaidAmount = new Amounts()