diff --git a/BTCPayServerPlugins.sln.DotSettings.user b/BTCPayServerPlugins.sln.DotSettings.user index 9d0765d..bfc1e32 100644 --- a/BTCPayServerPlugins.sln.DotSettings.user +++ b/BTCPayServerPlugins.sln.DotSettings.user @@ -1,4 +1,18 @@ + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + ExplicitlyExcluded + <SessionState ContinuousTestingMode="0" IsActive="True" Name="EnsureNewLightningInvoiceOnPartialPayment" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::4146B6DF-7BEE-4BD0-B6B1-77E7630A1B81::net8.0::BTCPayServer.Tests.UnitTest1.EnsureNewLightningInvoiceOnPartialPayment</TestId> + </TestAncestor> +</SessionState> True \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/BTCPayServer.Plugins.Bringin.csproj b/Plugins/BTCPayServer.Plugins.Bringin/BTCPayServer.Plugins.Bringin.csproj index 0b916b0..5abc8a1 100644 --- a/Plugins/BTCPayServer.Plugins.Bringin/BTCPayServer.Plugins.Bringin.csproj +++ b/Plugins/BTCPayServer.Plugins.Bringin/BTCPayServer.Plugins.Bringin.csproj @@ -36,6 +36,11 @@ - + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.Bringin/BringinCustodian.cs b/Plugins/BTCPayServer.Plugins.Bringin/BringinCustodian.cs index 9133fef..c83900c 100644 --- a/Plugins/BTCPayServer.Plugins.Bringin/BringinCustodian.cs +++ b/Plugins/BTCPayServer.Plugins.Bringin/BringinCustodian.cs @@ -1,176 +1,181 @@ -#nullable enable -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Sockets; -using System.Threading; -using System.Threading.Tasks; -using BTCPayServer.Abstractions.Custodians; -using BTCPayServer.Abstractions.Form; -using BTCPayServer.Client.Models; -using BTCPayServer.Forms; -using BTCPayServer.Payments; -using NBitcoin; -using Newtonsoft.Json.Linq; - -namespace BTCPayServer.Plugins.Bringin; - -public class BringinApiKeyFormComponentProvider:FormComponentProviderBase -{ - private readonly IHttpClientFactory _httpClientFactory; - public override string View => "Bringin/ApiKeyElement"; - - public BringinApiKeyFormComponentProvider(IHttpClientFactory httpClientFactory) - { - _httpClientFactory = httpClientFactory; - } - - public override void Validate(Form form, Field field) - { - if (field.Required) - { - ValidateField(field); - } - if(field.ValidationErrors.Any()) - return; - var httpClient = _httpClientFactory.CreateClient("bringin"); - httpClient.BaseAddress = new Uri("https://dev.bringin.xyz"); - httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", GetValue(form, field)); - try - { - var userId = new BringinClient(GetValue(form, field), httpClient).GetUserId().GetAwaiter().GetResult(); - if(userId is null) - field.ValidationErrors.Add("Invalid API Key"); - } - catch (Exception e) - { - field.ValidationErrors.Add("Invalid API Key"); - } - } - - public override void Register(Dictionary typeToComponentProvider) - { - typeToComponentProvider.Add("bringin-apikey", this); - } -} - - -public class BringinCustodian : ICustodian, ICanDeposit -{ - private readonly IHttpClientFactory _httpClientFactory; - private readonly FormDataService _formDataService; - public string Code => "bringin"; - - public string Name => "Bringin"; - - public BringinCustodian(IHttpClientFactory httpClientFactory, FormDataService formDataService) - { - _httpClientFactory = httpClientFactory; - _formDataService = formDataService; - } - - public async Task> GetAssetBalancesAsync(JObject config, - CancellationToken cancellationToken) - { - var bringinClient = FromConfig(config); - if (bringinClient is null) - throw new BadConfigException(Array.Empty()); - - var balance = await bringinClient.GetFiatBalance(); - return new Dictionary() - { - {"EUR", balance} - }; - } - - public async Task GetConfigForm(JObject config, CancellationToken cancellationToken = default) - { - - var f = new Form(); - - var fieldset = Field.CreateFieldset(); - fieldset.Label = "Connection details"; - fieldset.Fields.Add(new Field() - { - Name = "apiKey", - Label = "API Key", - Type = "bringin-apikey", - Value = config["apiKey"]?.Value(), - Required = true, - HelpText = "Enter your Bringin API Key which can be obtained from here" - }); - // fieldset.Fields.Add(new Field() - // { - // Name = "server", - // Label = "Bringin Server (optional)", - // Type = "password", - // Value = config["server"]?.Value(), - // Required = false, - // OriginalValue = "https://dev.bringin.xyz", - // HelpText = "Enter the Bringin server URL. This is optional and defaults to https://dev.bringin.xyz" - // }); - // - f.Fields.Add(fieldset); - return f; - } - - public async Task GetDepositAddressAsync(string paymentMethod, JObject config, - CancellationToken cancellationToken) - { - var amount = config["depositAddressConfig"]?.Value(); - - var bringinClient = FromConfig(config); - if (bringinClient is null) - throw new Exception("Invalid API Key"); - if (amount is null or <= 0) - { - var rate = await bringinClient.GetRate(); - //rate.bringinRate is the price for 1 BTC in EUR - //100eur = 1/rate.bringinRate BTC - amount = 100m / rate.BringinPrice; - } - // var rate = await bringinClient.GetRate(); - var host = await Dns.GetHostEntryAsync(Dns.GetHostName(), cancellationToken); - var ipToUse = host.AddressList.FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork)?.ToString(); - var request = new BringinClient.CreateOrderRequest() - { - SourceAmount = Money.Coins(amount.Value).Satoshi, - IP = ipToUse, - PaymentMethod = "LIGHTNING" - }; - var order = await bringinClient.PlaceOrder(request); - return new DepositAddressData() - { - Address = order.Invoice - }; - } - - public string[] GetDepositablePaymentMethods() - { - return new[] {new PaymentMethodId("BTC", LightningPaymentType.Instance).ToString()}; - } - - private BringinClient? FromConfig(JObject config) - { - Uri backend = new Uri("https://dev.bringin.xyz"); - if (config.TryGetValue("apiKey", out var apiKey)) - { - // if (config.TryGetValue("server", out var serverToken) && serverToken.Value() is { } server && - // !string.IsNullOrEmpty(server)) - // { - // backend = new Uri(server); - // } - - var httpClient = _httpClientFactory.CreateClient("bringin"); - httpClient.BaseAddress = backend; - - httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", apiKey.Value()); - return new BringinClient(apiKey.Value(), httpClient); - } - - return null; - } -} \ No newline at end of file +// #nullable enable +// using System; +// using System.Collections.Generic; +// using System.ComponentModel.DataAnnotations; +// using System.Linq; +// using System.Net; +// using System.Net.Http; +// using System.Net.Sockets; +// using System.Threading; +// using System.Threading.Tasks; +// using BTCPayServer.Abstractions.Custodians; +// using BTCPayServer.Abstractions.Form; +// using BTCPayServer.Client.Models; +// using BTCPayServer.Forms; +// using BTCPayServer.Payments; +// using NBitcoin; +// using Newtonsoft.Json.Linq; +// +// namespace BTCPayServer.Plugins.Bringin; +// +// public class BringinApiKeyFormComponentProvider:FormComponentProviderBase +// { +// private readonly IHttpClientFactory _httpClientFactory; +// public override string View => "Bringin/ApiKeyElement"; +// +// public BringinApiKeyFormComponentProvider(IHttpClientFactory httpClientFactory) +// { +// _httpClientFactory = httpClientFactory; +// } +// +// public override void Validate(Form form, Field field) +// { +// if (field.Required) +// { +// ValidateField(field); +// } +// if(field.ValidationErrors.Any()) +// return; +// var httpClient = _httpClientFactory.CreateClient("bringin"); +// httpClient.BaseAddress = new Uri("https://dev.bringin.xyz"); +// httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", GetValue(form, field)); +// try +// { +// var userId = new BringinClient(GetValue(form, field), httpClient).GetUserId().GetAwaiter().GetResult(); +// if(userId is null) +// field.ValidationErrors.Add("Invalid API Key"); +// } +// catch (Exception e) +// { +// field.ValidationErrors.Add("Invalid API Key"); +// } +// } +// +// public override void Register(Dictionary typeToComponentProvider) +// { +// typeToComponentProvider.Add("bringin-apikey", this); +// } +// } +// +// +// public class BringinCustodian : ICustodian, ICanDeposit +// { +// private readonly IHttpClientFactory _httpClientFactory; +// private readonly FormDataService _formDataService; +// public string Code => "bringin"; +// +// public string Name => "Bringin"; +// +// public BringinCustodian(IHttpClientFactory httpClientFactory, FormDataService formDataService) +// { +// _httpClientFactory = httpClientFactory; +// _formDataService = formDataService; +// } +// +// public async Task> GetAssetBalancesAsync(JObject config, +// CancellationToken cancellationToken) +// { +// var bringinClient = FromConfig(config); +// if (bringinClient is null) +// throw new BadConfigException(Array.Empty()); +// +// var balance = await bringinClient.GetFiatBalance(); +// return new Dictionary() +// { +// {"EUR", balance} +// }; +// } +// +// public async Task GetConfigForm(JObject config, CancellationToken cancellationToken = default) +// { +// +// var f = new Form(); +// +// var fieldset = Field.CreateFieldset(); +// fieldset.Label = "Connection details"; +// fieldset.Fields.Add(new Field() +// { +// Name = "apiKey", +// Label = "API Key", +// Type = "bringin-apikey", +// Value = config["apiKey"]?.Value(), +// Required = true, +// HelpText = "Enter your Bringin API Key which can be obtained from here" +// }); +// +// var fieldset2 = Field.CreateFieldset(); +// fieldset.Label = "Conversion details"; +// +// // fieldset.Fields.Add(new Field() +// // { +// // Name = "server", +// // Label = "Bringin Server (optional)", +// // Type = "password", +// // Value = config["server"]?.Value(), +// // Required = false, +// // OriginalValue = "https://dev.bringin.xyz", +// // HelpText = "Enter the Bringin server URL. This is optional and defaults to https://dev.bringin.xyz" +// // }); +// // +// f.Fields.Add(fieldset); +// return f; +// } +// +// public async Task GetDepositAddressAsync(string paymentMethod, JObject config, +// CancellationToken cancellationToken) +// { +// var amount = config["depositAddressConfig"]?.Value(); +// +// var bringinClient = FromConfig(config); +// if (bringinClient is null) +// throw new Exception("Invalid API Key"); +// if (amount is null or <= 0) +// { +// var rate = await bringinClient.GetRate(); +// //rate.bringinRate is the price for 1 BTC in EUR +// //100eur = 1/rate.bringinRate BTC +// amount = 100m / rate.BringinPrice; +// } +// // var rate = await bringinClient.GetRate(); +// var host = await Dns.GetHostEntryAsync(Dns.GetHostName(), cancellationToken); +// var ipToUse = host.AddressList.FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork)?.ToString(); +// var request = new BringinClient.CreateOrderRequest() +// { +// SourceAmount = Money.Coins(amount.Value).Satoshi, +// IP = ipToUse, +// PaymentMethod = "LIGHTNING" +// }; +// var order = await bringinClient.PlaceOrder(request); +// return new DepositAddressData() +// { +// Address = order.Invoice +// }; +// } +// +// public string[] GetDepositablePaymentMethods() +// { +// return new[] {new PaymentMethodId("BTC", LightningPaymentType.Instance).ToString()}; +// } +// +// private BringinClient? FromConfig(JObject config) +// { +// Uri backend = new Uri("https://dev.bringin.xyz"); +// if (config.TryGetValue("apiKey", out var apiKey)) +// { +// // if (config.TryGetValue("server", out var serverToken) && serverToken.Value() is { } server && +// // !string.IsNullOrEmpty(server)) +// // { +// // backend = new Uri(server); +// // } +// +// Uri backend = new Uri("https://dev.bringin.xyz"); +// var httpClient = _httpClientFactory.CreateClient("bringin"); +// httpClient.BaseAddress = backend; +// +// httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", apiKey.Value()); +// return new BringinClient(apiKey.Value(), httpClient); +// } +// +// return null; +// } +// } \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/BringinPlugin.cs b/Plugins/BTCPayServer.Plugins.Bringin/BringinPlugin.cs index 1c22955..3917a06 100644 --- a/Plugins/BTCPayServer.Plugins.Bringin/BringinPlugin.cs +++ b/Plugins/BTCPayServer.Plugins.Bringin/BringinPlugin.cs @@ -1,10 +1,14 @@ -using System; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Contracts; -using BTCPayServer.Abstractions.Custodians; using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Models; -using BTCPayServer.Forms; +using BTCPayServer.Abstractions.Services; +using BTCPayServer.Client; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace BTCPayServer.Plugins.Bringin; @@ -17,10 +21,50 @@ public class BringinPlugin : BaseBTCPayServerPlugin public override void Execute(IServiceCollection applicationBuilder) { - applicationBuilder.AddStartupTask(); - applicationBuilder.AddSingleton(); - applicationBuilder.AddSingleton(); - + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(s => s.GetService()); + applicationBuilder.AddSingleton(new UIExtension("Bringin/BringinDashboardWidget", "dashboard")); + applicationBuilder.AddSingleton(new UIExtension("Bringin/Nav", + "store-integrations-nav")); + } +} + +[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] +[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] +[Route("plugins/{storeId}/Bringin")] +public class BringinController : Controller +{ + private readonly BringinService _bringinService; + + public BringinController(BringinService bringinService) + { + _bringinService = bringinService; } + [HttpGet("")] + public async Task Edit() + { + return View(); + } + + [HttpGet("callback")] + public async Task Callback(string storeId, string apiKey) + { + //truncate with showing only first 3 letters on start ond end + + var truncatedApikey = apiKey.Substring(0, 3) + "***" + apiKey.Substring(apiKey.Length - 3); + + return View("Confirm", + new ConfirmModel("Confirm Bringin API Key", + $"You are about to set your Bringin API key to {truncatedApikey}", "Set", "btn-primary")); + } + + [HttpPost("callback")] + public async Task CallbackConfirm(string storeId, string apiKey) + { + var vm = await _bringinService.Update(storeId); + vm.ApiKey = apiKey; + await _bringinService.Update(storeId, vm); + return RedirectToAction("Edit", new {storeId}); + } } \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/BringinService.cs b/Plugins/BTCPayServer.Plugins.Bringin/BringinService.cs new file mode 100644 index 0000000..2e69c14 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/BringinService.cs @@ -0,0 +1,495 @@ +#nullable enable +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; +using AsyncKeyedLock; +using BTCPayServer.Client.Models; +using BTCPayServer.Data; +using BTCPayServer.Data.Payouts.LightningLike; +using BTCPayServer.Events; +using BTCPayServer.HostedServices; +using BTCPayServer.Lightning; +using BTCPayServer.Payments; +using BTCPayServer.Services; +using BTCPayServer.Services.Stores; +using Microsoft.Extensions.Logging; +using NBitcoin; +using Newtonsoft.Json.Linq; +using Org.BouncyCastle.Bcpg.OpenPgp; +using MarkPayoutRequest = BTCPayServer.HostedServices.MarkPayoutRequest; +using PayoutData = BTCPayServer.Data.PayoutData; + +namespace BTCPayServer.Plugins.Bringin; + +public class BringinService : EventHostedServiceBase +{ + private readonly ILogger _logger; + private readonly StoreRepository _storeRepository; + private readonly PullPaymentHostedService _pullPaymentHostedService; + private readonly BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings; + private readonly IHttpClientFactory _httpClientFactory; + private readonly BTCPayNetworkProvider _btcPayNetworkProvider; + private ConcurrentDictionary _settings; + private readonly AsyncKeyedLocker _storeLocker = new(); + + public BringinService(EventAggregator eventAggregator, ILogger logger, + StoreRepository storeRepository, + PullPaymentHostedService pullPaymentHostedService, + BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings, + IHttpClientFactory httpClientFactory, BTCPayNetworkProvider btcPayNetworkProvider) : base(eventAggregator, logger) + { + _logger = logger; + _storeRepository = storeRepository; + _pullPaymentHostedService = pullPaymentHostedService; + _btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings; + _httpClientFactory = httpClientFactory; + _btcPayNetworkProvider = btcPayNetworkProvider; + } + + protected override void SubscribeToEvents() + { + base.SubscribeToEvents(); + Subscribe(); + Subscribe(); + Subscribe(); + } + + public override async Task StartAsync(CancellationToken cancellationToken) + { + _settings = new ConcurrentDictionary( + await _storeRepository.GetSettingsAsync(BringinStoreSettings.BringinSettings)); + await CheckPendingPayouts(); + _ = PeriodicallyCheckEditModes(); + await base.StartAsync(cancellationToken); + } + + + private async Task PeriodicallyCheckEditModes() + { + while (!CancellationToken.IsCancellationRequested) + { + foreach (var (storeId, (disposable, _, expiry)) in _editModes) + { + if (expiry < DateTimeOffset.Now) + { + await CancelEdit(storeId); + } + } + + await Task.Delay(TimeSpan.FromSeconds(10)); + } + } + + + private async Task HandleStoreAction(string storeId, Func action) + { + using (await _storeLocker.LockAsync(storeId)) + { + if (_settings.TryGetValue(storeId, out var bringinStoreSettings)) + { + await action(bringinStoreSettings); + } + } + } + + + protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) + { + var storeId = evt switch + { + StoreRemovedEvent storeRemovedEvent => storeRemovedEvent.StoreId, + InvoiceEvent invoiceEvent => invoiceEvent.Invoice.StoreId, + PayoutEvent payoutEvent => payoutEvent.Payout.StoreDataId, + _ => null + }; + + if (storeId is not null) + { + _ = HandleStoreAction(storeId, async bringinStoreSettings => + { + switch (evt) + { + case StoreRemovedEvent storeRemovedEvent: + _settings.TryRemove(storeRemovedEvent.StoreId, out _); + break; + case InvoiceEvent + { + EventCode: + InvoiceEventCode.Completed or InvoiceEventCode.MarkedCompleted + } + invoiceEvent when bringinStoreSettings.Enabled: + + var pmPayments = invoiceEvent.Invoice.GetPayments("BTC", true) + .GroupBy(payment => payment.GetPaymentMethodId()); + var update = false; + foreach (var pmPayment in pmPayments) + { + var methodId = pmPayment.Key; + if (methodId.PaymentType == PaymentTypes.LNURLPay) + methodId = new PaymentMethodId(methodId.CryptoCode, PaymentTypes.LightningLike); + if (!bringinStoreSettings.MethodSettings.TryGetValue(methodId.ToString(), + out var methodSettings)) + { + continue; + } + + methodSettings.CurrentBalance += + pmPayment.Sum(payment => payment.GetCryptoPaymentData().GetValue()); + update = true; + } + + if (update) + { + await _storeRepository.UpdateSetting(invoiceEvent.Invoice.StoreId, + BringinStoreSettings.BringinSettings, bringinStoreSettings); + await CheckIfNewPayoutsNeeded(invoiceEvent.Invoice.StoreId, bringinStoreSettings); + } + + break; + case PayoutEvent payoutEvent: + + if (HandlePayoutState(payoutEvent.Payout)) + { + await CheckIfNewPayoutsNeeded(payoutEvent.Payout.StoreDataId, bringinStoreSettings); + } + + break; + } + }); + } + + await base.ProcessEvent(evt, cancellationToken); + } + + private async Task CheckIfNewPayoutsNeeded(string storeId, BringinStoreSettings bringinStoreSetting) + { + if (!bringinStoreSetting.Enabled) + return false; + var result = false; + // check if there are any payouts that need to be created by looking at the balance and threshold + // for onchain, we may also try and cancel a payout if there is a pending balance so that we dont needlessly create multiple transactions + + foreach (var methodSetting in bringinStoreSetting.MethodSettings) + { + var pmi = PaymentMethodId.TryParse(methodSetting.Key); + if (pmi is null) + { + continue; + } + + // if there is a pending payout, try and cancel it if this is onchain as we want to save needless tx fees + if (methodSetting.Value.PendingPayouts.Count > 0 && pmi.PaymentType == BitcoinPaymentType.Instance) + { + var cancelResult = await _pullPaymentHostedService.Cancel( + new PullPaymentHostedService.CancelRequest(methodSetting.Value.PendingPayouts.ToArray(), + new[] {storeId})); + + if (cancelResult.Values.Any(value => value == MarkPayoutRequest.PayoutPaidResult.Ok)) + { + continue; + } + } + + + if (SupportedMethods.All(supportedMethod => supportedMethod.PaymentMethod != pmi)) + { + //only LN is supported for now + continue; + } + + var supportedMethod = SupportedMethods.First(supportedMethod => supportedMethod.PaymentMethod == pmi); + var bringinClient = bringinStoreSetting.CreateClient(_httpClientFactory); + + var host = await Dns.GetHostEntryAsync(Dns.GetHostName(), CancellationToken.None); + var ipToUse = host.AddressList + .FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + + var thresholdAmount = methodSetting.Value.Threshold; + if (methodSetting.Value.FiatThreshold) + { + var rate = await bringinClient.GetRate(); + thresholdAmount = methodSetting.Value.Threshold / rate.BringinPrice; + } + + if (methodSetting.Value.CurrentBalance >= thresholdAmount) + { + var request = new BringinClient.CreateOrderRequest() + { + SourceAmount = Money.Coins(methodSetting.Value.CurrentBalance).Satoshi, + IP = ipToUse, + PaymentMethod = supportedMethod.bringinMethod + }; + var order = await bringinClient.PlaceOrder(request); + var orderMoney = Money.Satoshis(order.Amount); +var network = _btcPayNetworkProvider.GetNetwork(pmi.CryptoCode); + var claim = await _pullPaymentHostedService.Claim(new ClaimRequest() + { + PaymentMethodId = pmi, + StoreId = storeId, + Destination = new BoltInvoiceClaimDestination(order.Invoice, BOLT11PaymentRequest.Parse(order.Invoice, network.NBitcoinNetwork)), + Value = orderMoney.ToUnit(MoneyUnit.BTC), + PreApprove = true, + Metadata = JObject.FromObject(new + { + Source = "Bringin" + }) + }); + if (claim.Result == ClaimRequest.ClaimResult.Ok) + { + methodSetting.Value.CurrentBalance -= orderMoney.ToUnit(MoneyUnit.BTC); + methodSetting.Value.PendingPayouts.Add(claim.PayoutData.Id); + result = true; + } + } + } + + if (result) + { + await _storeRepository.UpdateSetting(storeId, BringinStoreSettings.BringinSettings, + bringinStoreSetting); + _settings.AddOrReplace(storeId, bringinStoreSetting); + } + + return result; + } + + public async Task CreateOrder(string storeId, PaymentMethodId paymentMethodId, Money amountBtc, bool payout) + { + if (SupportedMethods.All(supportedMethod => supportedMethod.PaymentMethod != paymentMethodId)) + { + throw new NotSupportedException("Only LN is supported for now"); + + } + var settings = _settings[storeId]; + var bringinClient = settings.CreateClient(_httpClientFactory); + + + var host = await Dns.GetHostEntryAsync(Dns.GetHostName(), CancellationToken.None); + var ipToUse = host.AddressList + .FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + + + + var supportedMethod = SupportedMethods.First(supportedMethod => supportedMethod.PaymentMethod == paymentMethodId); + //check if amount is enough + if (supportedMethod.FiatMinimumAmount > 0) + { + + var rate = await bringinClient.GetRate(); + var thresholdAmount = supportedMethod.FiatMinimumAmount / rate.BringinPrice; + if (amountBtc.ToDecimal(MoneyUnit.BTC) < thresholdAmount) + { + throw new Exception($"Amount is too low. Minimum amount is {Money.Coins(thresholdAmount)} BTC"); + } + + } + + var request = new BringinClient.CreateOrderRequest() + { + SourceAmount = amountBtc.Satoshi, + IP = ipToUse, + PaymentMethod = supportedMethod.bringinMethod + }; + var order = await bringinClient.PlaceOrder(request); + var orderMoney = Money.Satoshis(order.Amount); + + if (!payout) + { + return order.Invoice; + } + var network = _btcPayNetworkProvider.GetNetwork(paymentMethodId.CryptoCode); + + var claim = await _pullPaymentHostedService.Claim(new ClaimRequest() + { + PaymentMethodId = paymentMethodId, + StoreId = storeId, + Destination = new BoltInvoiceClaimDestination(order.Invoice, BOLT11PaymentRequest.Parse(order.Invoice, network.NBitcoinNetwork)), + Value = orderMoney.ToUnit(MoneyUnit.BTC), + PreApprove = true, + Metadata = JObject.FromObject(new + { + Source = "Bringin" + }) + }); + if (claim.Result != ClaimRequest.ClaimResult.Ok) + { + throw new Exception($"Could not create payout because {ClaimRequest.GetErrorMessage(claim.Result)}"); + } + return claim?.PayoutData?.Id; + + + } + + public bool IsInEditMode(string storeId) + { + return _editModes.ContainsKey(storeId); + } + + private async Task CheckPendingPayouts() + { + var payoutsIdsToCheck = _settings.SelectMany(pair => + pair.Value.MethodSettings.Values.SelectMany(settings => settings.PendingPayouts)); + var payouts = await _pullPaymentHostedService.GetPayouts(new PullPaymentHostedService.PayoutQuery() + { + PayoutIds = payoutsIdsToCheck.ToArray() + }); + var storesToUpdate = new HashSet(); + foreach (var payout in payouts.Where(HandlePayoutState)) + { + storesToUpdate.Add(payout.StoreDataId); + } + + foreach (var (storeId, bringinStoreSettings) in _settings) + { + if (await CheckIfNewPayoutsNeeded(storeId, bringinStoreSettings)) + { + //the method updates so no need to do it again + storesToUpdate.Remove(storeId); + } + } + + + foreach (var storeId in storesToUpdate) + { + await _storeRepository.UpdateSetting(storeId, BringinStoreSettings.BringinSettings, _settings[storeId]); + } + } + + private bool HandlePayoutState(PayoutData payout) + { + switch (payout.State) + { + case PayoutState.Completed: + // remove from pending payouts list in a setting + return _settings[payout.StoreDataId].MethodSettings[payout.GetPaymentMethodId().ToString()] + .PendingPayouts.Remove(payout.Id); + case PayoutState.Cancelled: + // remove from pending payouts list in a setting and add to a balance + var result = _settings[payout.StoreDataId].MethodSettings[payout.GetPaymentMethodId().ToString()] + .PendingPayouts.Remove(payout.Id); + var pmi = payout.GetPaymentMethodId(); + if (_settings[payout.StoreDataId].MethodSettings + .TryGetValue(pmi.ToString(), out var methodSettings)) + { + methodSettings.CurrentBalance += payout.GetBlob(_btcPayNetworkJsonSerializerSettings).Amount; + result = true; + } + + return result; + } + + return false; + } + + + public async Task Get(string storeId) + { + return _settings.TryGetValue(storeId, out var bringinStoreSettings) ? bringinStoreSettings : null; + } + + public record SupportedMethodOptions(PaymentMethodId PaymentMethod, bool FiatMinimum, decimal FiatMinimumAmount, string bringinMethod); + + public static readonly SupportedMethodOptions[] SupportedMethods = new[] + { + new SupportedMethodOptions(new PaymentMethodId("BTC", LightningPaymentType.Instance), true, 100m, "LIGHTNING") + }; + + private ConcurrentDictionary _editModes = new(); + + public async Task Update(string storeId) + { + var isNew = false; + var result = _editModes.GetOrAdd(storeId, (s) => + { + var storeLock = _storeLocker.Lock(s); + var result = (_settings.TryGetValue(s, out var bringinStoreSettings) + ? JObject.FromObject(bringinStoreSettings).ToObject() + : new BringinStoreSettings())!; + + // add or remove any missing methods in result + foreach (var supportedMethod in SupportedMethods) + { + if (!result.MethodSettings.ContainsKey(supportedMethod.PaymentMethod.ToString())) + { + result.MethodSettings.Add(supportedMethod.PaymentMethod.ToString(), + new BringinStoreSettings.PaymentMethodSettings() + { + FiatThreshold = supportedMethod.FiatMinimum, + Threshold = supportedMethod.FiatMinimum ? supportedMethod.FiatMinimumAmount : 0.1m + }); + } + } + + result.MethodSettings = result.MethodSettings.Where(pair => + SupportedMethods.Any(supportedMethod => supportedMethod.PaymentMethod.ToString() == pair.Key)) + .ToDictionary(pair => pair.Key, pair => pair.Value); + isNew = true; + return (storeLock, result, DateTimeOffset.Now.AddMinutes(5)); + }); + result.Expiry = DateTimeOffset.Now.AddMinutes(5); + if (_storeLocker.IsInUse(storeId)) + { + if (isNew) + EditModeChanged?.Invoke(this, (storeId, true)); + return result.Item2; + } + + _editModes.Remove(storeId, out _); + return await Update(storeId); + } + + public EventHandler<(string storeId, bool editMode)> EditModeChanged; + + public async Task Update(string storeId, BringinStoreSettings bringinStoreSettings) + { + if (!_editModes.Remove(storeId, out var editModeLock)) + return; + editModeLock.Item1.Dispose(); + + await _storeRepository.UpdateSetting(storeId, BringinStoreSettings.BringinSettings, bringinStoreSettings); + _settings.AddOrReplace(storeId, bringinStoreSettings); + + EditModeChanged?.Invoke(this, (storeId, false)); + } + + public async Task CancelEdit(string storeId) + { + if (!_editModes.Remove(storeId, out var editModeLock)) + return false; + editModeLock.Item1.Dispose(); + EditModeChanged?.Invoke(this, (storeId, false)); + return true; + } + + public class BringinStoreSettings + { + public const string BringinSettings = "BringinSettings"; + public bool Enabled { get; set; } = true; + public string ApiKey { get; set; } + public Dictionary MethodSettings { get; set; } = new(); + + public class PaymentMethodSettings + { + public decimal Threshold { get; set; } + public bool FiatThreshold { get; set; } + public decimal PercentageToForward { get; set; } = 99; + public decimal CurrentBalance { get; set; } = 0m; + public List PendingPayouts { get; set; } = new(); + } + + public BringinClient CreateClient(IHttpClientFactory httpClientFactory) + { + var backend = new Uri("https://dev.bringin.xyz"); + var httpClient = httpClientFactory.CreateClient("bringin"); + httpClient.BaseAddress = backend; + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", ApiKey); + return new BringinClient(ApiKey, httpClient); + } + } +} \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Components/BringinWidget.razor b/Plugins/BTCPayServer.Plugins.Bringin/Components/BringinWidget.razor new file mode 100644 index 0000000..a52f780 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/Components/BringinWidget.razor @@ -0,0 +1,563 @@ +@using System.Threading +@using BTCPayServer.Abstractions.Extensions +@using BTCPayServer.Data +@using BTCPayServer.Payments +@using BTCPayServer.PayoutProcessors +@using BTCPayServer.Services +@using BTCPayServer.Services.Stores +@using Microsoft.AspNetCore.Http +@using Microsoft.AspNetCore.Routing +@using NBitcoin +@implements IAsyncDisposable; + +@code { + private BringinService.BringinStoreSettings? _settings; + private bool _isLoaded = false; + private CancellationTokenSource _cts = new CancellationTokenSource(); + + [Inject] private IHttpContextAccessor HttpContextAccessor { get; set; } + [Inject] private DisplayFormatter DisplayFormatter { get; set; } + [Inject] private BringinService BringinService { get; set; } + [Inject] private LinkGenerator LinkGenerator { get; set; } + [Inject] private StoreRepository StoreRepository { get; set; } + [Inject] private BTCPayNetworkProvider BTCPayNetworkProvider { get; set; } + [Inject] private IHttpClientFactory HttpClientFactory { get; set; } + [Inject] private PayoutProcessorService PayoutProcessorService { get; set; } + [Parameter] public string StoreId { get; set; } + private decimal? LastFiatBalance { get; set; } + private DateTimeOffset? LastDataFetch { get; set; } + private decimal? LastFiatRate { get; set; } + private string SaveError { get; set; } + + private bool ApiKeyError + { + get => _apiKeyError; + set + { + if (_apiKeyError == value) + return; + _apiKeyError = value; + InvokeAsync(StateHasChanged); + } + } + + private bool IsLoaded + { + get => _isLoaded; + set + { + if (_isLoaded == value) + return; + _isLoaded = value; + InvokeAsync(StateHasChanged); + } + } + + private bool EditMode + { + get => _editMode; + set + { + if (_editMode == value) + return; + _editMode = value; + InvokeAsync(StateHasChanged); + } + } + + private bool _editMode; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + PmiLink = $"A payout processor has not been configured for this payment method. Payouts generated by Bringin will not be automatically handled. Configure now"; + _callbackLink = LinkGenerator.GetUriByAction(HttpContextAccessor.HttpContext, "Callback", "Bringin", new {StoreId}); + _settings = BringinService.IsInEditMode(StoreId)? await BringinService.Update(StoreId): await BringinService.Get(StoreId); + _pms = (await StoreRepository.FindStore(StoreId)).GetSupportedPaymentMethods(BTCPayNetworkProvider).Select(method => method.PaymentId).Where(id => id.CryptoCode == "BTC").ToArray(); + _pps = (await PayoutProcessorService.GetProcessors(new PayoutProcessorService.PayoutProcessorQuery() + { + Stores = new[] {StoreId}, + PaymentMethods = _pms.Select(p => p.ToString()).ToArray() + })).Select(data => PaymentMethodId.TryParse(data.PaymentMethod)).Where(id => id is not null).ToArray(); + EditMode = BringinService.IsInEditMode(StoreId); + IsLoaded = true; + _ = FetchBalanceAndRate(); + BringinService.EditModeChanged += EditModeChanged; + } + + await base.OnAfterRenderAsync(firstRender); + } + + private string _callbackLink; + + private void EditModeChanged(object sender, (string storeId, bool editMode) e) + { + if (e.storeId != StoreId) + { + return; + } + + if (EditMode == e.editMode) + return; + _ = e.editMode ? Edit() : CancelEdit(); + InvokeAsync(StateHasChanged); + } + + private async Task Edit() + { + if (_saving) + return; + SaveError = null; + ApiKeyError = false; + _settings = await BringinService.Update(StoreId); + await TestApiKey(); + EditMode = true; + } + + private async Task CancelEdit() + { + if (_saving) + return; + SaveError = null; + await BringinService.CancelEdit(StoreId); + EditMode = false; + _settings = await BringinService.Get(StoreId); + } + + private async Task TestApiKey() + { + ApiKeyError = false; + if (string.IsNullOrEmpty(_settings?.ApiKey)) + return; + try + { + var userId = await _settings.CreateClient(HttpClientFactory).GetUserId(); + ApiKeyError = string.IsNullOrEmpty(userId); + } + catch (Exception e) + { + ApiKeyError = true; + } + } + + private bool _saving; + private PaymentMethodId[] _pms; + private PaymentMethodId[] _pps; + private bool _apiKeyError; + + private async Task Save() + { + if (_saving) + return; + _saving = true; + await TestApiKey(); + if (ApiKeyError) + return; + SaveError = null; + if (!EditMode) + return; + await BringinService.Update(StoreId, _settings); + EditMode = false; + _saving = false; + fetcherCTS?.Cancel(); + } + + CancellationTokenSource fetcherCTS; + + + private async Task FetchBalanceAndRate() + { + if (_cts.IsCancellationRequested) + return; + fetcherCTS = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token); + try + { + if (_settings?.ApiKey is null || EditMode) + return; + var client = _settings.CreateClient(HttpClientFactory); + LastFiatBalance = await client.GetFiatBalance(); + LastFiatRate = (await client.GetRate()).BringinPrice; + LastDataFetch = DateTimeOffset.UtcNow; + _ = InvokeAsync(StateHasChanged); + } + finally + { + try + { + await Task.Delay(TimeSpan.FromMinutes(1), fetcherCTS.Token); + } + catch + { + } + + await FetchBalanceAndRate(); + } + } + + + private void UpdateDestinationValue(BringinService.BringinStoreSettings.PaymentMethodSettings settings, object eValue) + { + var newValue = Math.Min(100, Math.Round(Convert.ToDecimal(eValue), 2)); + settings.PercentageToForward = newValue; + } + + public async ValueTask DisposeAsync() + { + await _cts.CancelAsync(); + BringinService.EditModeChanged -= EditModeChanged; + } + + public string ApiKey + { + get => _settings.ApiKey; + set + { + if (_settings.ApiKey == value) + return; + _settings.ApiKey = value; + InvokeAsync(StateHasChanged); + } + } + + public string TimeAgo + { + get + { + var res = LastDataFetch is null ? "" : LastDataFetch.Value.ToTimeAgo(); + + Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(_ => InvokeAsync(StateHasChanged)); + + return res; + } + } + + private Task CreateManual() + { + SaveError = null; + _manualOrder = true; + return InvokeAsync(StateHasChanged); + } + + private Task CancelManual() + { + SaveError = null; + _manualOrder = false; + ManualOrderResult = null; + return InvokeAsync(StateHasChanged); + } + + public async Task SubmitOrder() + { + _saving = true; + + await InvokeAsync(StateHasChanged); + var pm = PaymentMethodId.TryParse(ManualOrderPaymentMethod); + if (pm is null) + { + SaveError = "Invalid payment method"; + return; + } + + try + { + SaveError = null; + ManualOrderResult = await BringinService.CreateOrder(StoreId, pm, Money.Coins(ManualOrderAmount.Value), true); + } + catch (Exception e) + { + SaveError = e.Message; + _saving = false; + } + await InvokeAsync(StateHasChanged); + } + + private string ManualOrderResult = null; + + private bool _manualOrder = false; + private string ManualOrderPaymentMethod = null; + private decimal? ManualOrderAmount = null; + + // private bool ManualOrderPayout = true; + + public string PmiLink; +} + + + + @if (!IsLoaded) + { + Loading Bringin offramp + } + else + { + + + Bringin Off-ramp + @if (EditMode) + { + + } + else if (_settings?.Enabled is true) + { + + } + else if (_settings is not null) + { + + } + + + + + @if (_manualOrder) + { + Cancel order + } + else if (_settings is not null && !EditMode) + { + Edit + Manual order + } + else if (_settings is not null && EditMode) + { + if (ApiKeyError) + { + Test API Key + } + + if (!string.IsNullOrEmpty(_settings.ApiKey) && !ApiKeyError) + { + Save + } + + Cancel edit + } + + + + + @if (_settings is null) + { + + Bringin is a service that allows you to automatically convert your BTC to EUR and send it to your bank account. Start configuring it by clicking on the button below. + + + Configure + + } + else if (_manualOrder) + { + var items = new List(); + items.AddRange(BringinService.SupportedMethods.Where(s => _pms.Contains(s.PaymentMethod)).Select(s => s.PaymentMethod)); + + + + + @if (!string.IsNullOrEmpty(SaveError)) + { + @SaveError + } + + @if (!string.IsNullOrEmpty(ManualOrderResult)) + { + Payout created: @ManualOrderResult + + + Go back + + } + else + { + + @* Payment method *@ + + @foreach (var opt in items) + { + Select a payment method + @opt.ToPrettyString() + } + + + @if (!string.IsNullOrEmpty(ManualOrderPaymentMethod)) + { + // var fiat = BringinService.SupportedMethods.First(s => s.PaymentMethod.ToString() == ManualOrderPaymentMethod)?.FiatMinimum is true; + + + Amount + + + BTC + + + + @* *@ + @* *@ + @* *@ + @* Create as Payout *@ + @* *@ + @* *@ + + + Create order + + } + } + + + } + else if (!EditMode) + { + @if (LastFiatBalance is not null) + { + + + Balance + + + @LastFiatBalance + EUR + @TimeAgo + + + + } + + @foreach (var method in _settings.MethodSettings) + { + var pmi = PaymentMethodId.TryParse(method.Key); + if (pmi is null) + continue; + if (!_pms.Contains(pmi)) + continue; + + + + @pmi.ToPrettyString() + + + @if (LastFiatRate is null || !method.Value.FiatThreshold) + { + + @DisplayFormatter.Currency(method.Value.CurrentBalance, "BTC", DisplayFormatter.CurrencyFormat.None) + BTC + pending to forward once @DisplayFormatter.Currency(method.Value.Threshold, method.Value.FiatThreshold ? "EUR" : "BTC") is reached. + + + } + else if (LastFiatRate is not null && method.Value.FiatThreshold) + { + var balanceInFiat = method.Value.CurrentBalance * LastFiatRate; + var thresholdinBtc = method.Value.Threshold / LastFiatRate; + var percentage = (balanceInFiat / method.Value.Threshold) * 100m; + + + @DisplayFormatter.Currency(method.Value.CurrentBalance, "BTC", DisplayFormatter.CurrencyFormat.None) + BTC + (@DisplayFormatter.Currency(balanceInFiat.Value, "EUR", DisplayFormatter.CurrencyFormat.Code)) pending to forward once @DisplayFormatter.Currency(thresholdinBtc.Value, "BTC", DisplayFormatter.CurrencyFormat.Code) (@DisplayFormatter.Currency(method.Value.Threshold, "EUR")) is reached. + + } + @if (method.Value.PendingPayouts.Any()) + { + + @method.Value.PendingPayouts.Count + pending payouts + + } + @if (!_pps.Contains(pmi) && PmiLink is not null) + { + @((MarkupString)PmiLink) + } + + } + } + else + { + @if (!string.IsNullOrEmpty(SaveError)) + { + @SaveError + } + + @if (string.IsNullOrEmpty(_settings.ApiKey) || ApiKeyError) + { + + + + + API Key + InvokeAsync(StateHasChanged)" @bind="ApiKey"/> + You can get one here + + + + + } + else + { + + + + + API Key + + You can get one here + @if (ApiKeyError) + { + Invalid API Key + } + + + + + Enabled + + + + + + + @foreach (var method in _settings.MethodSettings) + { + var pmId = PaymentMethodId.TryParse(method.Key); + if (pmId is null) + continue; + var supportedMethod = BringinService.SupportedMethods.FirstOrDefault(s => s.PaymentMethod.ToString() == method.Key); + + @pmId.ToPrettyString() + + + Percentage + UpdateDestinationValue(method.Value, e.Value))" min="0" step='0.01' class="form-range" max="100"/> + + UpdateDestinationValue(method.Value, e.Value))" class="form-control form-control-sm"/> + % + + Every time an invoice becomes Settled, we take the sum of all settled payments of this payment method, get the specified percentage of it and add it to the current balance. + + + Threshold + + + @(method.Value.FiatThreshold ? "EUR" : "BTC") + + Once the threshold is reached, we create a payout sending the balance to Bringin to be converted. + + + @if (supportedMethod?.FiatMinimum is not true) + { + + + + Threshold in EUR + + + } + + + + } + + } + } + } + \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Components/_Imports.razor b/Plugins/BTCPayServer.Plugins.Bringin/Components/_Imports.razor new file mode 100644 index 0000000..966c28b --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/Components/_Imports.razor @@ -0,0 +1,8 @@ +@using System.Net.Http +@using Microsoft.AspNetCore.Authorization +@using Microsoft.AspNetCore.Components.Authorization +@using Microsoft.AspNetCore.Components.Forms +@using Microsoft.AspNetCore.Components.Routing +@using Microsoft.AspNetCore.Components.Web +@using Microsoft.JSInterop +@using System.IO \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Views/Bringin/Edit.cshtml b/Plugins/BTCPayServer.Plugins.Bringin/Views/Bringin/Edit.cshtml new file mode 100644 index 0000000..bc20c31 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/Views/Bringin/Edit.cshtml @@ -0,0 +1,6 @@ +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@{ + ViewData.SetActivePage("Bringin", "Bringin", "Bringin"); +} + \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/ApiKeyElement.cshtml b/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/ApiKeyElement.cshtml deleted file mode 100644 index ea11d45..0000000 --- a/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/ApiKeyElement.cshtml +++ /dev/null @@ -1,28 +0,0 @@ -@model BTCPayServer.Abstractions.Form.Field -@{ - var isInvalid = ViewContext.ModelState[Model.Name]?.ValidationState is Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; - var errors = isInvalid ? ViewContext.ModelState[Model.Name].Errors : null; -} - - - @Safe.Raw(Model.Label) - - - @(isInvalid && errors.Any() ? errors.First().ErrorMessage : string.Empty) - @if (!string.IsNullOrEmpty(Model.HelpText)) - { - - @Safe.Raw(Model.HelpText) - - } - diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/BringinDashboardWidget.cshtml b/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/BringinDashboardWidget.cshtml new file mode 100644 index 0000000..d9293fa --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/BringinDashboardWidget.cshtml @@ -0,0 +1,9 @@ +@using BTCPayServer.Plugins.Bringin.Components +@using BTCPayServer.Abstractions.Contracts +@model BTCPayServer.Models.StoreViewModels.StoreDashboardViewModel +@inject IScopeProvider ScopeProvider + +@(await Html.RenderComponentAsync(RenderMode.ServerPrerendered, new +{ + StoreId = ScopeProvider.GetCurrentStoreId() +})) \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/Nav.cshtml b/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/Nav.cshtml new file mode 100644 index 0000000..f4035df --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Bringin/Views/Shared/Bringin/Nav.cshtml @@ -0,0 +1,23 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Abstractions.Extensions +@using BTCPayServer.Client +@using Microsoft.AspNetCore.Mvc.TagHelpers +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + + + + + + + + + + Bringin + + +} diff --git a/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj b/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj index 78a2df7..4a3e954 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj +++ b/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj @@ -11,7 +11,7 @@ Prism Automated value splits for Bitcoin. - 1.2.5 + 1.2.6 true diff --git a/Plugins/BTCPayServer.Plugins.Prism/Components/PrismEdit.razor b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismEdit.razor index 9fc08a7..8a134d7 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/Components/PrismEdit.razor +++ b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismEdit.razor @@ -4,16 +4,12 @@ @using BTCPayServer.HostedServices @using BTCPayServer.Payments @using BTCPayServer.PayoutProcessors -@using BTCPayServer.Services.Custodian.Client @using Microsoft.AspNetCore.Http @using Microsoft.AspNetCore.Routing @using Microsoft.Extensions.Logging @using NBitcoin @using LightningAddressData = BTCPayServer.Data.LightningAddressData @using MarkPayoutRequest = BTCPayServer.HostedServices.MarkPayoutRequest -@using System.Collections -@using BTCPayServer.Abstractions.Custodians -@using BTCPayServer.Abstractions.Extensions @inject IPluginHookService PluginHookService @inject LightningAddressService LightningAddressService @inject PayoutProcessorService PayoutProcessorService @@ -22,8 +18,6 @@ @inject LinkGenerator LinkGenerator @inject PullPaymentHostedService PullPaymentHostedService @inject IHttpContextAccessor HttpContextAccessor -@inject CustodianAccountRepository CustodianAccountRepository -@inject IEnumerable Custodians @inject ILogger Logger @implements IDisposable @@ -47,10 +41,6 @@ else { @destination } - @foreach (var destination in CustodianDestinations) - { - @destination.Value - } @@ -215,7 +205,6 @@ else public PaymentMethodId pmichain { get; set; } = new("BTC", PaymentTypes.BTCLike); public bool NoPayoutProcessors { get; set; } - public Dictionary CustodianDestinations { get; set; } private string PrismEditButtonsFilter { get; set; } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -247,34 +236,6 @@ else await Task.WhenAll(tasks); Settings = await fetchSettings; Users = await fetchLnAddresses; - CustodianDestinations = await FetchCustodians(); - - async Task> FetchCustodians() - { - var result = new Dictionary(); - var custodianConfigs = await CustodianAccountRepository.FindByStoreId(StoreId); - foreach (var custodianConfig in custodianConfigs) - { - var custodian = Custodians.GetCustodianByCode(custodianConfig.CustodianCode); - if(custodian is not ICanDeposit canDeposit) - continue; - foreach (var depositablePaymentMethod in canDeposit.GetDepositablePaymentMethods()) - { - var pmi = PaymentMethodId.TryParse(depositablePaymentMethod); - if(pmi is null || pmi.CryptoCode != "BTC") - continue; - - var custodianDestination = new CustodianDestinationValidator.CustodianDestination() - { - CustodianId = custodianConfig.Id, - PaymentMethod = pmi.ToString() - }; - result.TryAdd(custodianDestination.ToString(), $"{custodianConfig.Name} {pmi.ToPrettyString()} (Custodian)"); - } - - } - return result; - } EditContext = new EditContext(Settings); MessageStore = new ValidationMessageStore(EditContext); diff --git a/Plugins/BTCPayServer.Plugins.Prism/CustodianDestinationValidator.cs b/Plugins/BTCPayServer.Plugins.Prism/CustodianDestinationValidator.cs deleted file mode 100644 index 81f30a2..0000000 --- a/Plugins/BTCPayServer.Plugins.Prism/CustodianDestinationValidator.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using BTCPayServer.Abstractions.Contracts; -using BTCPayServer.Abstractions.Custodians; -using BTCPayServer.Abstractions.Extensions; -using BTCPayServer.Data; -using BTCPayServer.Payments; -using BTCPayServer.Services.Custodian.Client; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json.Linq; - -namespace BTCPayServer.Plugins.Prism; - -public class CustodianDestinationValidator : IPluginHookFilter -{ - private readonly IServiceProvider _serviceProvider; - public string Hook => "prism-destination-validate"; - - public CustodianDestinationValidator(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public async Task Execute(object args) - { - var result = new PrismDestinationValidationResult(); - if (args is not string args1 || !args1.StartsWith("custodian:")) return args; - - try - { - var custodianDestination = - JObject.Parse(args1.Substring("custodian:".Length)).ToObject(); - var custodianPaymentMethod = custodianDestination.PaymentMethod is null - ? new PaymentMethodId("BTC", PaymentTypes.LightningLike) - : PaymentMethodId.Parse(custodianDestination.PaymentMethod); - - result.PaymentMethod = custodianPaymentMethod; - await using var ctx = _serviceProvider.GetService().CreateContext(); - var custodianAccountData = ctx.CustodianAccount.SingleOrDefault(data => data.Id == custodianDestination.CustodianId); - if (custodianAccountData is null) - { - result.Success = false; - return result; - } - - var custdodian = _serviceProvider.GetServices().GetCustodianByCode(custodianAccountData.CustodianCode); - if (custdodian is null) - { - result.Success = false; - return result; - } - - if (custdodian is ICanDeposit canDeposit && - canDeposit.GetDepositablePaymentMethods() is { } paymentMethods && - paymentMethods.Any(s => PaymentMethodId.TryParse(s) == custodianPaymentMethod)) - { - result.Success = true; - return result; - } - - result.Success = false; - return result; - } - catch (Exception e) - { - result.Success = false; - return result; - } - } - - - public class CustodianDestination - { - public string CustodianId { get; set; } - public string PaymentMethod { get; set; } - - override public string ToString() - { - return $"custodian:{JObject.FromObject(this)}"; - } - } -} \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Prism/CustodianPrismClaimCreate.cs b/Plugins/BTCPayServer.Plugins.Prism/CustodianPrismClaimCreate.cs deleted file mode 100644 index 474a45f..0000000 --- a/Plugins/BTCPayServer.Plugins.Prism/CustodianPrismClaimCreate.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using BTCPayServer.Abstractions.Contracts; -using BTCPayServer.Abstractions.Custodians; -using BTCPayServer.Abstractions.Extensions; -using BTCPayServer.Data; -using BTCPayServer.HostedServices; -using BTCPayServer.Payments; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json.Linq; - -namespace BTCPayServer.Plugins.Prism; - -public class CustodianPrismClaimCreate : IPluginHookFilter -{ - private readonly IServiceProvider _serviceProvider; - public string Hook => "prism-claim-create"; - - public CustodianPrismClaimCreate(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public async Task Execute(object args) - { - if (args is not ClaimRequest claimRequest) - { - return args; - } - - if (claimRequest.Destination?.ToString() is not { } args1 || !args1.StartsWith("custodian:")) return args; - - try - { - - var custodianDestination = JObject.Parse(args1.Substring("custodian:".Length)) - .ToObject(); - var custodianPaymentMethod = custodianDestination.PaymentMethod is null - ? new PaymentMethodId("BTC", PaymentTypes.LightningLike) - : PaymentMethodId.Parse(custodianDestination.PaymentMethod); - - await using var ctx = _serviceProvider.GetRequiredService().CreateContext(); - var custodianAccountData = await ctx.CustodianAccount.SingleOrDefaultAsync(data => data.Id == custodianDestination.CustodianId); - if (custodianAccountData is null) - { - return null; - } - - var custdodian = _serviceProvider.GetServices().GetCustodianByCode(custodianAccountData.CustodianCode); - if (custdodian is null) - { - return null; - } - - if (custdodian is not ICanDeposit canDeposit || - canDeposit.GetDepositablePaymentMethods() is { } paymentMethods && - paymentMethods.Any(s => PaymentMethodId.TryParse(s) == custodianPaymentMethod)) - { - return null; - } - - var handler = _serviceProvider.GetServices().FindPayoutHandler(custodianPaymentMethod); - if (handler is null) - { - return null; - } - - var config = custodianAccountData.GetBlob(); - config["depositAddressConfig"] = JToken.FromObject(new - { - amount = claimRequest.Value - }); - var depositAddressAsync = - await canDeposit.GetDepositAddressAsync(custodianPaymentMethod.ToString(),config, CancellationToken.None); - if (depositAddressAsync.Address is null) - { - return null; - } - - var claimDestination = await handler.ParseClaimDestination(custodianPaymentMethod, - depositAddressAsync.Address, CancellationToken.None); - if (claimDestination.destination is null) - { - - return null; - } - - - claimRequest.Destination = claimDestination.destination; - claimRequest.PaymentMethodId = custodianPaymentMethod; - - return claimRequest; - - } - catch (Exception e) - { - - return null; - } - } -} \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Prism/OpenSatsDestinationValidator.cs b/Plugins/BTCPayServer.Plugins.Prism/OpenSatsDestinationValidator.cs new file mode 100644 index 0000000..ac52334 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Prism/OpenSatsDestinationValidator.cs @@ -0,0 +1,96 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Payments; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.Prism; + +public class OpenSatsDestinationValidator : IPluginHookFilter +{ + private readonly IServiceProvider _serviceProvider; + public string Hook => "prism-destination-validate"; + + public OpenSatsDestinationValidator(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public async Task Execute(object args) + { + var result = new PrismDestinationValidationResult(); + if (args is not string args1 || !args1.StartsWith("opensats")) return args; + + try + { + + var parts = args1.Split(":", StringSplitOptions.RemoveEmptyEntries); + var project = "opensats"; + var paymentMethod = new PaymentMethodId("BTC", PaymentTypes.LightningLike); + if (parts.Length > 1) + { + project = parts[1]; + } + + if (parts.Length > 2) + { + paymentMethod = PaymentMethodId.Parse(parts[2]); + } + + + var handler = _serviceProvider.GetServices().FindPayoutHandler(paymentMethod); + if (handler is null) + { + result.Success = false; + } + + var httpClientFactory = _serviceProvider.GetRequiredService(); + var httpClient = httpClientFactory.CreateClient("opensats"); + + var content = new StringContent(JObject.FromObject(new + { + btcpay = project, + name = "kukks <3 you" + }).ToString(), Encoding.UTF8, "application/json"); + var xResult = await httpClient.PostAsync("https://opensats.org/api/btcpay",content).ConfigureAwait(false); + + var rawInvoice = JObject.Parse(await xResult.Content.ReadAsStringAsync().ConfigureAwait(false)); + var invoiceUrl = $"{rawInvoice.Value("checkoutLink").TrimEnd('/')}/{paymentMethod}/status"; + var invoiceBtcpayModel = JObject.Parse(await httpClient.GetStringAsync(invoiceUrl).ConfigureAwait(false)); + var destination = invoiceBtcpayModel.Value("btcAddress"); + + var claimDestination = await handler.ParseClaimDestination(paymentMethod,destination, CancellationToken.None); + if (claimDestination.destination is null) + { + + result.Success = false; + } + + + result.Success = true; + result.PaymentMethod = paymentMethod; + return result; + } + catch (Exception e) + { + result.Success = false; + return result; + } + } + + + public class CustodianDestination + { + public string CustodianId { get; set; } + public string PaymentMethod { get; set; } + + override public string ToString() + { + return $"custodian:{JObject.FromObject(this)}"; + } + } +} \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Prism/OpenSatsPrismClaimCreate.cs b/Plugins/BTCPayServer.Plugins.Prism/OpenSatsPrismClaimCreate.cs new file mode 100644 index 0000000..8ecdc46 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Prism/OpenSatsPrismClaimCreate.cs @@ -0,0 +1,98 @@ +using System; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Custodians; +using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Data; +using BTCPayServer.HostedServices; +using BTCPayServer.Payments; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using NBitcoin; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.Prism; + +public class OpenSatsPrismClaimCreate : IPluginHookFilter +{ + private readonly IServiceProvider _serviceProvider; + public string Hook => "prism-claim-create"; + + public OpenSatsPrismClaimCreate(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public async Task Execute(object args) + { + if (args is not ClaimRequest claimRequest) + { + return args; + } + + if (claimRequest.Destination?.ToString() is not { } args1 || !args1.StartsWith("opensats")) return args; + + try + { + + var parts = args1.Split(":", StringSplitOptions.RemoveEmptyEntries); + var project = "opensats"; + var paymentMethod = new PaymentMethodId("BTC", PaymentTypes.LightningLike); + if (parts.Length > 1) + { + project = parts[1]; + } + + if (parts.Length > 2) + { + paymentMethod = PaymentMethodId.Parse(parts[2]); + } + + + var handler = _serviceProvider.GetServices().FindPayoutHandler(paymentMethod); + if (handler is null) + { + return null; + } + + var httpClientFactory = _serviceProvider.GetRequiredService(); + var httpClient = httpClientFactory.CreateClient("opensats"); + + var content = new StringContent(JObject.FromObject(new + { + project_name = project, + project_slug = project, + name = "kukks <3 you" + }).ToString(), Encoding.UTF8, "application/json"); + var result = await httpClient.PostAsync("https://opensats.org/api/btcpay",content).ConfigureAwait(false); + + var rawInvoice = JObject.Parse(await result.Content.ReadAsStringAsync().ConfigureAwait(false)); + var invoiceUrl = $"{rawInvoice.Value("checkoutLink").TrimEnd('/')}/{paymentMethod}/status"; + var invoiceBtcpayModel = JObject.Parse(await httpClient.GetStringAsync(invoiceUrl).ConfigureAwait(false)); + var destination = invoiceBtcpayModel.Value("btcAddress"); + + var claimDestination = await handler.ParseClaimDestination(paymentMethod,destination, CancellationToken.None); + if (claimDestination.destination is null) + { + + return null; + } + + + claimRequest.Destination = claimDestination.destination; + claimRequest.PaymentMethodId = paymentMethod; + + return claimRequest; + + } + catch (Exception) + { + + return null; + } + } +} \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Prism/PrismPlugin.cs b/Plugins/BTCPayServer.Plugins.Prism/PrismPlugin.cs index c42ede0..b3ba269 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/PrismPlugin.cs +++ b/Plugins/BTCPayServer.Plugins.Prism/PrismPlugin.cs @@ -22,12 +22,12 @@ public class PrismPlugin : BaseBTCPayServerPlugin "store-integrations-nav")); applicationBuilder.AddSingleton(); applicationBuilder.AddHostedService(provider => provider.GetRequiredService()); - applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(); applicationBuilder.AddSingleton(); applicationBuilder.AddSingleton(); applicationBuilder.AddSingleton(); applicationBuilder.AddSingleton(); - applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(); base.Execute(applicationBuilder); } diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/Coordinator/WabisabiScriptResolver.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/Coordinator/WabisabiScriptResolver.cs index 0e4c68c..a2f5584 100644 --- a/Plugins/BTCPayServer.Plugins.Wabisabi/Coordinator/WabisabiScriptResolver.cs +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/Coordinator/WabisabiScriptResolver.cs @@ -85,8 +85,7 @@ public class WabisabiScriptResolver: WabiSabiConfig.CoordinatorScriptResolver } var content = new StringContent(JObject.FromObject(new { - project_name = value, - project_slug = value, + btcpay = value, name = "kukks <3 you" }).ToString(), Encoding.UTF8, "application/json"); var result = await httpClient.PostAsync("https://opensats.org/api/btcpay",content, cancellationToken).ConfigureAwait(false);
+ Bringin is a service that allows you to automatically convert your BTC to EUR and send it to your bank account. Start configuring it by clicking on the button below. +
@((MarkupString)PmiLink)
You can get one here
Every time an invoice becomes Settled, we take the sum of all settled payments of this payment method, get the specified percentage of it and add it to the current balance.
Once the threshold is reached, we create a payout sending the balance to Bringin to be converted.