2021-10-18 09:56:47 +02:00
|
|
|
#nullable enable
|
2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2019-08-29 17:24:42 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using BTCPayServer.Payments;
|
2021-12-31 08:59:02 +01:00
|
|
|
using BTCPayServer.Payments.Lightning;
|
2019-08-29 17:24:42 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
2021-11-10 10:50:39 +01:00
|
|
|
using NBitcoin;
|
2019-08-29 17:24:42 +02:00
|
|
|
using NBXplorer;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
{
|
|
|
|
public static class StoreDataExtensions
|
|
|
|
{
|
|
|
|
#pragma warning disable CS0618
|
2021-10-18 09:56:47 +02:00
|
|
|
public static PaymentMethodId? GetDefaultPaymentId(this StoreData storeData)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
2020-08-09 14:43:13 +02:00
|
|
|
PaymentMethodId.TryParse(storeData.DefaultCrypto, out var defaultPaymentId);
|
2021-10-18 09:56:47 +02:00
|
|
|
return defaultPaymentId;
|
2019-08-29 17:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static PaymentMethodId[] GetEnabledPaymentIds(this StoreData storeData, BTCPayNetworkProvider networks)
|
2021-11-04 08:21:01 +01:00
|
|
|
{
|
|
|
|
return GetEnabledPaymentMethods(storeData, networks).Select(method => method.PaymentId).ToArray();
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-11-04 08:21:01 +01:00
|
|
|
public static ISupportedPaymentMethod[] GetEnabledPaymentMethods(this StoreData storeData, BTCPayNetworkProvider networks)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
|
|
|
var excludeFilter = storeData.GetStoreBlob().GetExcludedPaymentMethods();
|
2021-09-24 07:16:25 +02:00
|
|
|
var paymentMethodIds = storeData.GetSupportedPaymentMethods(networks)
|
2021-11-04 08:21:01 +01:00
|
|
|
.Where(a => !excludeFilter.Match(a.PaymentId))
|
|
|
|
.OrderByDescending(a => a.PaymentId.CryptoCode == "BTC")
|
|
|
|
.ThenBy(a => a.PaymentId.CryptoCode)
|
|
|
|
.ThenBy(a => a.PaymentId.PaymentType == PaymentTypes.LightningLike ? 1 : 0)
|
|
|
|
.ToArray();
|
2019-08-29 17:24:42 +02:00
|
|
|
return paymentMethodIds;
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:48:12 +02:00
|
|
|
public static void SetDefaultPaymentId(this StoreData storeData, PaymentMethodId? defaultPaymentId)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
2020-12-29 10:33:44 +01:00
|
|
|
storeData.DefaultCrypto = defaultPaymentId?.ToString();
|
2019-08-29 17:24:42 +02:00
|
|
|
}
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
|
2020-06-28 10:55:27 +02:00
|
|
|
|
2019-08-29 17:24:42 +02:00
|
|
|
public static StoreBlob GetStoreBlob(this StoreData storeData)
|
|
|
|
{
|
2022-11-28 12:36:18 +01:00
|
|
|
var result = storeData.StoreBlob == null ? new StoreBlob() : new Serializer(null).ToObject<StoreBlob>(storeData.StoreBlob);
|
2019-08-29 17:24:42 +02:00
|
|
|
if (result.PreferredExchange == null)
|
2022-12-14 05:33:27 +01:00
|
|
|
result.PreferredExchange = result.GetRecommendedExchange();
|
2022-09-26 03:31:04 +02:00
|
|
|
if (result.PaymentMethodCriteria is null)
|
|
|
|
result.PaymentMethodCriteria = new List<PaymentMethodCriteria>();
|
2022-09-26 15:00:24 +02:00
|
|
|
result.PaymentMethodCriteria.RemoveAll(criteria => criteria?.PaymentMethod is null);
|
2019-08-29 17:24:42 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool SetStoreBlob(this StoreData storeData, StoreBlob storeBlob)
|
|
|
|
{
|
2019-11-15 10:53:20 +01:00
|
|
|
var original = new Serializer(null).ToString(storeData.GetStoreBlob());
|
|
|
|
var newBlob = new Serializer(null).ToString(storeBlob);
|
2019-08-29 17:24:42 +02:00
|
|
|
if (original == newBlob)
|
|
|
|
return false;
|
2022-11-28 12:36:18 +01:00
|
|
|
storeData.StoreBlob = newBlob;
|
2019-08-29 17:24:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerable<ISupportedPaymentMethod> GetSupportedPaymentMethods(this StoreData storeData, BTCPayNetworkProvider networks)
|
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(storeData);
|
2019-08-29 17:24:42 +02:00
|
|
|
#pragma warning disable CS0618
|
|
|
|
bool btcReturned = false;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(storeData.DerivationStrategies))
|
|
|
|
{
|
|
|
|
JObject strategies = JObject.Parse(storeData.DerivationStrategies);
|
|
|
|
foreach (var strat in strategies.Properties())
|
|
|
|
{
|
2020-08-09 14:43:13 +02:00
|
|
|
if (!PaymentMethodId.TryParse(strat.Name, out var paymentMethodId))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-21 16:39:44 +02:00
|
|
|
var network = networks.GetNetwork<BTCPayNetworkBase>(paymentMethodId.CryptoCode);
|
2019-08-29 17:24:42 +02:00
|
|
|
if (network != null)
|
|
|
|
{
|
|
|
|
if (network == networks.BTC && paymentMethodId.PaymentType == PaymentTypes.BTCLike && btcReturned)
|
|
|
|
continue;
|
|
|
|
if (strat.Value.Type == JTokenType.Null)
|
|
|
|
continue;
|
|
|
|
yield return
|
|
|
|
paymentMethodId.PaymentType.DeserializeSupportedPaymentMethod(network, strat.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void SetSupportedPaymentMethod(this StoreData storeData, ISupportedPaymentMethod supportedPaymentMethod)
|
|
|
|
{
|
|
|
|
storeData.SetSupportedPaymentMethod(null, supportedPaymentMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set or remove a new supported payment method for the store
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="paymentMethodId">The paymentMethodId</param>
|
|
|
|
/// <param name="supportedPaymentMethod">The payment method, or null to remove</param>
|
2021-10-18 09:56:47 +02:00
|
|
|
public static void SetSupportedPaymentMethod(this StoreData storeData, PaymentMethodId? paymentMethodId, ISupportedPaymentMethod? supportedPaymentMethod)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
|
|
|
if (supportedPaymentMethod != null && paymentMethodId != null && paymentMethodId != supportedPaymentMethod.PaymentId)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Incoherent arguments, this should never happen");
|
|
|
|
}
|
|
|
|
if (supportedPaymentMethod == null && paymentMethodId == null)
|
|
|
|
throw new ArgumentException($"{nameof(supportedPaymentMethod)} or {nameof(paymentMethodId)} should be specified");
|
|
|
|
if (supportedPaymentMethod != null && paymentMethodId == null)
|
|
|
|
{
|
|
|
|
paymentMethodId = supportedPaymentMethod.PaymentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma warning disable CS0618
|
|
|
|
JObject strategies = string.IsNullOrEmpty(storeData.DerivationStrategies) ? new JObject() : JObject.Parse(storeData.DerivationStrategies);
|
|
|
|
bool existing = false;
|
|
|
|
foreach (var strat in strategies.Properties().ToList())
|
|
|
|
{
|
2021-03-10 11:51:51 +01:00
|
|
|
if (!PaymentMethodId.TryParse(strat.Name, out var stratId))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-29 17:24:42 +02:00
|
|
|
if (stratId == paymentMethodId)
|
|
|
|
{
|
|
|
|
if (supportedPaymentMethod == null)
|
|
|
|
{
|
|
|
|
strat.Remove();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strat.Value = PaymentMethodExtensions.Serialize(supportedPaymentMethod);
|
|
|
|
}
|
|
|
|
existing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 03:11:58 +01:00
|
|
|
if (!existing && supportedPaymentMethod != null)
|
2019-08-29 17:24:42 +02:00
|
|
|
strategies.Add(new JProperty(supportedPaymentMethod.PaymentId.ToString(), PaymentMethodExtensions.Serialize(supportedPaymentMethod)));
|
|
|
|
storeData.DerivationStrategies = strategies.ToString();
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2023-02-02 01:42:41 +01:00
|
|
|
public static bool IsLightningEnabled(this StoreData storeData, BTCPayNetworkProvider networks, string cryptoCode)
|
2021-11-10 10:50:39 +01:00
|
|
|
{
|
2023-02-02 01:42:41 +01:00
|
|
|
return IsPaymentTypeEnabled(storeData, networks, cryptoCode, LightningPaymentType.Instance);
|
2021-11-10 10:50:39 +01:00
|
|
|
}
|
|
|
|
|
2023-02-02 01:42:41 +01:00
|
|
|
public static bool IsLNUrlEnabled(this StoreData storeData, BTCPayNetworkProvider networks, string cryptoCode)
|
|
|
|
{
|
|
|
|
return IsPaymentTypeEnabled(storeData, networks, cryptoCode, LNURLPayPaymentType.Instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsPaymentTypeEnabled(this StoreData storeData, BTCPayNetworkProvider networks, string cryptoCode, PaymentType paymentType)
|
2021-11-10 10:50:39 +01:00
|
|
|
{
|
|
|
|
var paymentMethods = storeData.GetSupportedPaymentMethods(networks);
|
|
|
|
var excludeFilters = storeData.GetStoreBlob().GetExcludedPaymentMethods();
|
2023-02-02 01:42:41 +01:00
|
|
|
return paymentMethods.Any(method =>
|
|
|
|
method.PaymentId.CryptoCode == cryptoCode &&
|
|
|
|
method.PaymentId.PaymentType == paymentType &&
|
|
|
|
!excludeFilters.Match(method.PaymentId));
|
2021-11-10 10:50:39 +01:00
|
|
|
}
|
2019-08-29 17:24:42 +02:00
|
|
|
}
|
|
|
|
}
|