Cleanup AvailableCrypto from the model

This commit is contained in:
nicolas.dorier 2024-10-07 19:15:40 +09:00
parent 34b2cca492
commit c35e7406cd
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
8 changed files with 23 additions and 37 deletions

View File

@ -726,7 +726,7 @@ namespace BTCPayServer.Controllers
bool isDefaultPaymentId = false;
var storeBlob = store.GetStoreBlob();
var displayedPaymentMethods = invoice.GetPaymentPrompts().Select(p => p.PaymentMethodId).ToList();
var displayedPaymentMethods = invoice.GetPaymentPrompts().Select(p => p.PaymentMethodId).ToHashSet();
var btcId = PaymentTypes.CHAIN.GetPaymentMethodId("BTC");
@ -916,33 +916,21 @@ namespace BTCPayServer.Controllers
var handler = _handlers[kv.PaymentMethodId];
return new PaymentModel.AvailableCrypto
{
Handler = handler,
Displayed = displayedPaymentMethods.Contains(kv.PaymentMethodId),
PaymentMethodId = kv.PaymentMethodId,
CryptoCode = kv.Currency,
PaymentMethodName = _prettyName.PrettyName(kv.PaymentMethodId),
IsLightning = handler is ILightningPaymentHandler,
CryptoImage = Request.GetRelativePathOrAbsolute(GetPaymentMethodImage(kv.PaymentMethodId)),
Link = Url.Action(nameof(Checkout),
new
Order = kv.PaymentMethodId switch
{
invoiceId,
paymentMethodId = kv.PaymentMethodId.ToString()
})
_ when PaymentTypes.CHAIN.GetPaymentMethodId(_NetworkProvider.DefaultNetwork.CryptoCode) == kv.PaymentMethodId => 0,
_ when PaymentTypes.LN.GetPaymentMethodId(_NetworkProvider.DefaultNetwork.CryptoCode) == kv.PaymentMethodId => 1,
_ when handler is ILightningPaymentHandler => 2,
_ => 3
}
};
}).Where(c => c.CryptoImage != "/")
.OrderByDescending(a => a.CryptoCode == _NetworkProvider.DefaultNetwork.CryptoCode).ThenBy(a => a.PaymentMethodName).ThenBy(a => a.IsLightning ? 1 : 0)
})
.OrderBy(a => a.Order)
.ToList()
};
foreach (var kv in invoice.GetPaymentPrompts())
{
if (_paymentModelExtensions.TryGetValue(kv.PaymentMethodId, out var extension) &&
_handlers.TryGetValue(kv.PaymentMethodId, out var h))
{
extension.ModifyPaymentModel(new PaymentModelContext(model, store, storeBlob, invoice, Url, kv, h, paymentMethodId == kv.PaymentMethodId));
}
}
model.PaymentMethodId = paymentMethodId.ToString();
model.OrderAmountFiat = OrderAmountFromInvoice(model.CryptoCode, invoice, DisplayFormatter.CurrencyFormat.Symbol);
@ -957,6 +945,12 @@ namespace BTCPayServer.Controllers
var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds);
model.TimeLeft = expiration.PrettyPrint();
if (_paymentModelExtensions.TryGetValue(paymentMethodId, out var extension) &&
_handlers.TryGetValue(paymentMethodId, out var h))
{
extension.ModifyPaymentModel(new PaymentModelContext(model, store, storeBlob, invoice, Url, prompt, h));
}
return model;
}

View File

@ -14,15 +14,8 @@ namespace BTCPayServer.Models.InvoicingModels
{
[JsonConverter(typeof(PaymentMethodIdJsonConverter))]
public PaymentMethodId PaymentMethodId { get; set; }
public string CryptoImage { get; set; }
public string Link { get; set; }
public string PaymentMethodName { get; set; }
public bool IsLightning { get; set; }
public string CryptoCode { get; set; }
public bool Displayed { get; set; }
[JsonIgnore]
public IPaymentMethodHandler Handler { get; internal set; }
public string Order { get; set; }
[JsonExtensionData]
public Dictionary<string, JToken> AdditionalData { get; set; } = new();
}

View File

@ -47,7 +47,7 @@ namespace BTCPayServer.Payments.Bitcoin
public PaymentMethodId PaymentMethodId { get; }
public void ModifyPaymentModel(PaymentModelContext context)
{
if (context is not { IsSelected: true, Handler: BitcoinLikePaymentHandler handler})
if (context is not { Handler: BitcoinLikePaymentHandler handler})
return;
var prompt = context.Prompt;
var details = handler.ParsePaymentPromptDetails(prompt.Details);

View File

@ -11,8 +11,7 @@ namespace BTCPayServer.Payments
InvoiceEntity InvoiceEntity,
IUrlHelper UrlHelper,
PaymentPrompt Prompt,
IPaymentMethodHandler Handler,
bool IsSelected);
IPaymentMethodHandler Handler);
public interface IPaymentModelExtension
{
public PaymentMethodId PaymentMethodId { get; }

View File

@ -39,7 +39,7 @@ namespace BTCPayServer.Payments.LNURLPay
private const string UriScheme = "lightning:";
public void ModifyPaymentModel(PaymentModelContext context)
{
if (context is not { IsSelected: true, Handler: LNURLPayPaymentHandler handler })
if (context is not { Handler: LNURLPayPaymentHandler handler })
return;
var lnurl = paymentLinkExtension.GetPaymentLink(context.Prompt, context.UrlHelper);
if (lnurl is not null)

View File

@ -40,7 +40,7 @@ namespace BTCPayServer.Payments.Lightning
public string Badge => "⚡";
public void ModifyPaymentModel(PaymentModelContext context)
{
if (context is not { IsSelected: true, Handler: LightningLikePaymentHandler handler })
if (context is not { Handler: LightningLikePaymentHandler handler })
return;
var paymentPrompt = context.InvoiceEntity.GetPaymentPrompt(PaymentMethodId);
if (paymentPrompt is null)

View File

@ -34,7 +34,7 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments
public void ModifyPaymentModel(PaymentModelContext context)
{
if (context is not { IsSelected: true, Handler: MoneroLikePaymentMethodHandler handler })
if (context is not { Handler: MoneroLikePaymentMethodHandler handler })
return;
context.Model.CheckoutBodyComponentName = BitcoinPaymentModelExtension.CheckoutBodyComponentName;
if (context.Model.Activated)

View File

@ -34,7 +34,7 @@ namespace BTCPayServer.Services.Altcoins.Zcash.Payments
public void ModifyPaymentModel(PaymentModelContext context)
{
if (context is not { IsSelected: true, Handler: ZcashLikePaymentMethodHandler handler })
if (context is not { Handler: ZcashLikePaymentMethodHandler handler })
return;
context.Model.CheckoutBodyComponentName = BitcoinPaymentModelExtension.CheckoutBodyComponentName;
if (context.Model.Activated)