2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2018-02-20 04:45:04 +01:00
|
|
|
using System.Collections.Generic;
|
2023-03-09 21:36:11 +01:00
|
|
|
using System.Globalization;
|
2018-02-20 04:45:04 +01:00
|
|
|
using System.Threading.Tasks;
|
2018-04-07 09:27:46 +02:00
|
|
|
using BTCPayServer.Data;
|
2020-03-29 17:28:22 +02:00
|
|
|
using BTCPayServer.Logging;
|
2019-05-29 16:33:31 +02:00
|
|
|
using BTCPayServer.Models.InvoicingModels;
|
|
|
|
using BTCPayServer.Rating;
|
2023-03-13 02:12:58 +01:00
|
|
|
using BTCPayServer.Services;
|
2018-02-20 04:45:04 +01:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2019-05-29 16:33:31 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
using NBitcoin;
|
|
|
|
using InvoiceResponse = BTCPayServer.Models.InvoiceResponse;
|
2018-02-20 04:45:04 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Payments
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This class customize invoice creation by the creation of payment details for the PaymentMethod during invoice creation
|
|
|
|
/// </summary>
|
|
|
|
public interface IPaymentMethodHandler
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Create needed to track payments of this invoice
|
|
|
|
/// </summary>
|
2022-07-06 15:09:05 +02:00
|
|
|
/// <param name="logs"></param>
|
2018-02-20 04:45:04 +01:00
|
|
|
/// <param name="supportedPaymentMethod"></param>
|
|
|
|
/// <param name="paymentMethod"></param>
|
2018-04-07 09:27:46 +02:00
|
|
|
/// <param name="store"></param>
|
2018-02-20 04:45:04 +01:00
|
|
|
/// <param name="network"></param>
|
2019-05-29 11:43:50 +02:00
|
|
|
/// <param name="preparePaymentObject"></param>
|
2022-07-06 15:09:05 +02:00
|
|
|
/// <param name="invoicePaymentMethods"></param>
|
2018-02-20 04:45:04 +01:00
|
|
|
/// <returns></returns>
|
2022-07-06 15:09:05 +02:00
|
|
|
Task<IPaymentMethodDetails> CreatePaymentMethodDetails(InvoiceLogs logs,
|
|
|
|
ISupportedPaymentMethod supportedPaymentMethod,
|
|
|
|
PaymentMethod paymentMethod, StoreData store, BTCPayNetworkBase network, object preparePaymentObject,
|
|
|
|
IEnumerable<PaymentMethodId> invoicePaymentMethods);
|
2018-08-21 06:54:52 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This method called before the rate have been fetched
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="supportedPaymentMethod"></param>
|
|
|
|
/// <param name="store"></param>
|
|
|
|
/// <param name="network"></param>
|
|
|
|
/// <returns></returns>
|
2019-05-29 11:43:50 +02:00
|
|
|
object PreparePayment(ISupportedPaymentMethod supportedPaymentMethod, StoreData store, BTCPayNetworkBase network);
|
2019-05-29 16:33:31 +02:00
|
|
|
|
2020-11-06 11:09:17 +01:00
|
|
|
void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, StoreBlob storeBlob,
|
|
|
|
IPaymentMethod paymentMethod);
|
2019-05-29 16:33:31 +02:00
|
|
|
string GetCryptoImage(PaymentMethodId paymentMethodId);
|
|
|
|
string GetPaymentMethodName(PaymentMethodId paymentMethodId);
|
|
|
|
|
|
|
|
IEnumerable<PaymentMethodId> GetSupportedPaymentMethods();
|
2019-08-25 15:50:11 +02:00
|
|
|
CheckoutUIPaymentMethodSettings GetCheckoutUISettings();
|
2018-02-20 04:45:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 16:33:31 +02:00
|
|
|
public interface IPaymentMethodHandler<TSupportedPaymentMethod, TBTCPayNetwork> : IPaymentMethodHandler
|
|
|
|
where TSupportedPaymentMethod : ISupportedPaymentMethod
|
|
|
|
where TBTCPayNetwork : BTCPayNetworkBase
|
2018-02-20 04:45:04 +01:00
|
|
|
{
|
2020-03-29 17:28:22 +02:00
|
|
|
Task<IPaymentMethodDetails> CreatePaymentMethodDetails(InvoiceLogs logs, TSupportedPaymentMethod supportedPaymentMethod,
|
2022-07-06 15:09:05 +02:00
|
|
|
PaymentMethod paymentMethod, StoreData store, TBTCPayNetwork network, object preparePaymentObject, IEnumerable<PaymentMethodId> invoicePaymentMethods);
|
2018-02-20 04:45:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-30 09:02:52 +02:00
|
|
|
public abstract class PaymentMethodHandlerBase<TSupportedPaymentMethod, TBTCPayNetwork> : IPaymentMethodHandler<
|
2019-05-29 16:33:31 +02:00
|
|
|
TSupportedPaymentMethod, TBTCPayNetwork>
|
|
|
|
where TSupportedPaymentMethod : ISupportedPaymentMethod
|
|
|
|
where TBTCPayNetwork : BTCPayNetworkBase
|
2018-02-20 04:45:04 +01:00
|
|
|
{
|
2019-06-04 01:59:01 +02:00
|
|
|
public abstract PaymentType PaymentType { get; }
|
2019-05-29 16:33:31 +02:00
|
|
|
|
|
|
|
public abstract Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
2020-03-29 17:28:22 +02:00
|
|
|
InvoiceLogs logs,
|
2019-05-29 16:33:31 +02:00
|
|
|
TSupportedPaymentMethod supportedPaymentMethod,
|
2022-07-06 15:09:05 +02:00
|
|
|
PaymentMethod paymentMethod, StoreData store, TBTCPayNetwork network, object preparePaymentObject, IEnumerable<PaymentMethodId> invoicePaymentMethods);
|
2019-05-29 16:33:31 +02:00
|
|
|
|
2019-09-11 07:49:06 +02:00
|
|
|
public abstract void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse,
|
2020-11-06 11:09:17 +01:00
|
|
|
StoreBlob storeBlob, IPaymentMethod paymentMethod);
|
2019-05-29 16:33:31 +02:00
|
|
|
public abstract string GetCryptoImage(PaymentMethodId paymentMethodId);
|
|
|
|
public abstract string GetPaymentMethodName(PaymentMethodId paymentMethodId);
|
|
|
|
|
|
|
|
public abstract IEnumerable<PaymentMethodId> GetSupportedPaymentMethods();
|
2019-08-27 04:31:15 +02:00
|
|
|
public virtual CheckoutUIPaymentMethodSettings GetCheckoutUISettings()
|
|
|
|
{
|
|
|
|
return new CheckoutUIPaymentMethodSettings()
|
|
|
|
{
|
2020-11-06 08:41:03 +01:00
|
|
|
ExtensionPartial = "Bitcoin/BitcoinLikeMethodCheckout",
|
|
|
|
CheckoutBodyVueComponentName = "BitcoinLikeMethodCheckout",
|
|
|
|
CheckoutHeaderVueComponentName = "BitcoinLikeMethodCheckoutHeader",
|
|
|
|
NoScriptPartialName = "Bitcoin/BitcoinLikeMethodCheckoutNoScript"
|
2019-08-27 04:31:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:33:31 +02:00
|
|
|
public virtual object PreparePayment(TSupportedPaymentMethod supportedPaymentMethod, StoreData store,
|
|
|
|
BTCPayNetworkBase network)
|
2018-08-21 06:54:52 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-04-10 04:07:03 +02:00
|
|
|
|
2023-03-13 02:12:58 +01:00
|
|
|
public virtual void PreparePaymentModelForAmountInSats(PaymentModel model, IPaymentMethod paymentMethod, DisplayFormatter displayFormatter)
|
2023-03-09 21:36:11 +01:00
|
|
|
{
|
|
|
|
var satoshiCulture = new CultureInfo(CultureInfo.InvariantCulture.Name)
|
2023-04-10 04:07:03 +02:00
|
|
|
{
|
2023-03-09 21:36:11 +01:00
|
|
|
NumberFormat = { NumberGroupSeparator = " " }
|
|
|
|
};
|
|
|
|
model.CryptoCode = "sats";
|
|
|
|
model.BtcDue = Money.Parse(model.BtcDue).ToUnit(MoneyUnit.Satoshi).ToString("N0", satoshiCulture);
|
|
|
|
model.BtcPaid = Money.Parse(model.BtcPaid).ToUnit(MoneyUnit.Satoshi).ToString("N0", satoshiCulture);
|
|
|
|
model.OrderAmount = Money.Parse(model.OrderAmount).ToUnit(MoneyUnit.Satoshi).ToString("N0", satoshiCulture);
|
|
|
|
model.NetworkFee = new Money(model.NetworkFee, MoneyUnit.BTC).ToUnit(MoneyUnit.Satoshi);
|
2023-03-13 02:12:58 +01:00
|
|
|
model.Rate = model.InvoiceCurrency is "BTC" or "SATS"
|
|
|
|
? null
|
|
|
|
: displayFormatter.Currency(paymentMethod.Rate / 100_000_000, model.InvoiceCurrency, DisplayFormatter.CurrencyFormat.Symbol);
|
2023-03-09 21:36:11 +01:00
|
|
|
}
|
2018-08-21 06:54:52 +02:00
|
|
|
|
2022-07-06 15:09:05 +02:00
|
|
|
public Task<IPaymentMethodDetails> CreatePaymentMethodDetails(InvoiceLogs logs,
|
|
|
|
ISupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod,
|
|
|
|
StoreData store, BTCPayNetworkBase network, object preparePaymentObject,
|
|
|
|
IEnumerable<PaymentMethodId> invoicePaymentMethods)
|
2018-08-21 06:54:52 +02:00
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
if (supportedPaymentMethod is TSupportedPaymentMethod method && network is TBTCPayNetwork correctNetwork)
|
2018-08-21 06:54:52 +02:00
|
|
|
{
|
2022-07-06 15:09:05 +02:00
|
|
|
return CreatePaymentMethodDetails(logs, method, paymentMethod, store, correctNetwork, preparePaymentObject, invoicePaymentMethods);
|
2018-08-21 06:54:52 +02:00
|
|
|
}
|
2019-05-29 16:33:31 +02:00
|
|
|
|
2018-08-21 06:54:52 +02:00
|
|
|
throw new NotSupportedException("Invalid supportedPaymentMethod");
|
|
|
|
}
|
2018-02-20 04:45:04 +01:00
|
|
|
|
2019-05-29 16:33:31 +02:00
|
|
|
object IPaymentMethodHandler.PreparePayment(ISupportedPaymentMethod supportedPaymentMethod, StoreData store,
|
|
|
|
BTCPayNetworkBase network)
|
2019-05-24 08:38:47 +02:00
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
if (supportedPaymentMethod is TSupportedPaymentMethod method)
|
|
|
|
{
|
|
|
|
return PreparePayment(method, store, network);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotSupportedException("Invalid supportedPaymentMethod");
|
2019-05-24 08:38:47 +02:00
|
|
|
}
|
2018-02-20 04:45:04 +01:00
|
|
|
}
|
|
|
|
}
|