btcpayserver/BTCPayServer/Payments/Bitcoin/BitcoinPaymentMethodBitpayAPIExtension.cs
Nicolas Dorier b4946f4db1
Fix divisibility in invoice details of lightning amounts (#6202)
* Fix divisibility in invoice details of lightning amounts

This PR will show 11 decimal in the invoice details for BTC amount
of lightning payment methods.

It also hacks around the fact that some
lightning clients don't create the requested amount of sats, which
resulted in over or under payments. (Blink not supporting msats, and
strike)

Now, In that case, a payment method fee (which can be negative) called tweak fee
will be added to the prompt.

We are also hiding this tweak fee from the user in the checkout page in
order to not disturb the UI with inconsequential fee of 0.000000001 sats.

* Only show 8 digits in checkout, even if amount is 11 digits
2024-09-12 12:43:08 +09:00

70 lines
2.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Models;
using BTCPayServer.Payments.Lightning;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Mvc;
using NBitpayClient;
namespace BTCPayServer.Payments.Bitcoin
{
public class BitcoinPaymentMethodBitpayAPIExtension : IPaymentMethodBitpayAPIExtension
{
public BitcoinPaymentMethodBitpayAPIExtension(
PaymentMethodId paymentMethodId,
IEnumerable<IPaymentLinkExtension> paymentLinkExtensions,
CurrencyNameTable currencyNameTable,
PaymentMethodHandlerDictionary handlers)
{
PaymentMethodId = paymentMethodId;
_currencyNameTable = currencyNameTable;
paymentLinkExtension = paymentLinkExtensions.Single(p => p.PaymentMethodId == PaymentMethodId);
handler = (BitcoinLikePaymentHandler)handlers[paymentMethodId];
}
public PaymentMethodId PaymentMethodId { get; }
private IPaymentLinkExtension paymentLinkExtension;
private BitcoinLikePaymentHandler handler;
private readonly CurrencyNameTable _currencyNameTable;
internal static decimal ToSmallestUnit(int divisibility, decimal v)
{
for (int i = 0; i < divisibility; i++)
{
v *= 10.0m;
}
return v;
}
public void PopulateCryptoInfo(Services.Invoices.InvoiceCryptoInfo cryptoInfo, InvoiceResponse dto, PaymentPrompt prompt, IUrlHelper urlHelper)
{
var accounting = prompt.Calculate();
cryptoInfo.PaymentUrls = new Services.Invoices.InvoiceCryptoInfo.InvoicePaymentUrls()
{
BIP21 = paymentLinkExtension.GetPaymentLink(prompt, urlHelper),
};
var minerInfo = new MinerFeeInfo();
if (_currencyNameTable.GetCurrencyData(prompt.Currency, false)?.Divisibility is int divisibility)
{
minerInfo.TotalFee = ToSmallestUnit(divisibility, accounting.PaymentMethodFee);
}
minerInfo.SatoshiPerBytes = handler.ParsePaymentPromptDetails(prompt.Details).RecommendedFeeRate.SatoshiPerByte;
dto.MinerFees.TryAdd(cryptoInfo.CryptoCode, minerInfo);
#pragma warning disable 618
if (prompt.Currency == "BTC")
{
dto.BTCPrice = cryptoInfo.Price;
dto.Rate = cryptoInfo.Rate;
dto.ExRates = cryptoInfo.ExRates;
dto.BitcoinAddress = cryptoInfo.Address;
dto.BTCPaid = cryptoInfo.Paid;
dto.BTCDue = cryptoInfo.Due;
dto.PaymentUrls = cryptoInfo.PaymentUrls;
}
#pragma warning restore 618
}
}
}