btcpayserver/BTCPayServer/Payments/Lightning/LNCheckoutModelExtension.cs

65 lines
2.9 KiB
C#
Raw Normal View History

#nullable enable
using BTCPayServer.Models.InvoicingModels;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services;
using Org.BouncyCastle.Crypto.Modes.Gcm;
using System.Collections.Generic;
using System.Linq;
2024-10-14 14:11:00 +09:00
using Microsoft.Extensions.Localization;
namespace BTCPayServer.Payments.Lightning
{
2024-10-07 19:58:08 +09:00
public class LNCheckoutModelExtension : ICheckoutModelExtension
{
public const string CheckoutBodyComponentName = "LightningCheckoutBody";
private readonly DisplayFormatter _displayFormatter;
IPaymentLinkExtension _PaymentLinkExtension;
2024-10-14 14:11:00 +09:00
private readonly bool isBTC;
2024-10-07 19:58:08 +09:00
public LNCheckoutModelExtension(
PaymentMethodId paymentMethodId,
BTCPayNetwork network,
DisplayFormatter displayFormatter,
IEnumerable<IPaymentLinkExtension> paymentLinkExtensions,
2024-10-14 14:11:00 +09:00
IStringLocalizer stringLocalizer,
PaymentMethodHandlerDictionary handlers)
{
Network = network;
_displayFormatter = displayFormatter;
2024-10-14 14:11:00 +09:00
StringLocalizer = stringLocalizer;
Handlers = handlers;
PaymentMethodId = paymentMethodId;
_PaymentLinkExtension = paymentLinkExtensions.Single(p => p.PaymentMethodId == PaymentMethodId);
2024-10-14 14:11:00 +09:00
isBTC = PaymentTypes.LN.GetPaymentMethodId("BTC") == paymentMethodId;
}
public BTCPayNetwork Network { get; }
2024-10-14 14:11:00 +09:00
public IStringLocalizer StringLocalizer { get; }
public PaymentMethodHandlerDictionary Handlers { get; }
public PaymentMethodId PaymentMethodId { get; }
2024-10-14 14:11:00 +09:00
public string DisplayName => isBTC ? StringLocalizer["Lightning"] : StringLocalizer["Lightning ({0})", Network.DisplayName];
public string Image => Network.LightningImagePath;
public string Badge => "⚡";
2024-10-07 19:58:08 +09:00
public void ModifyCheckoutModel(CheckoutModelContext context)
{
2024-10-07 19:15:40 +09:00
if (context is not { Handler: LightningLikePaymentHandler handler })
return;
var paymentPrompt = context.InvoiceEntity.GetPaymentPrompt(PaymentMethodId);
if (paymentPrompt is null)
return;
context.Model.CheckoutBodyComponentName = CheckoutBodyComponentName;
context.Model.InvoiceBitcoinUrl = _PaymentLinkExtension.GetPaymentLink(context.Prompt, context.UrlHelper);
if (context.Model.InvoiceBitcoinUrl is not null)
context.Model.InvoiceBitcoinUrlQR = $"lightning:{context.Model.InvoiceBitcoinUrl.ToUpperInvariant()?.Substring("LIGHTNING:".Length)}";
context.Model.PeerInfo = handler.ParsePaymentPromptDetails(paymentPrompt.Details).NodeInfo;
2024-10-14 14:11:00 +09:00
if (context.StoreBlob.LightningAmountInSatoshi && isBTC)
{
2024-10-07 19:58:08 +09:00
BitcoinCheckoutModelExtension.PreparePaymentModelForAmountInSats(context.Model, paymentPrompt.Rate, _displayFormatter);
}
}
}
}