From 00c8f6003b3fa3312575ddce489941bf45b70790 Mon Sep 17 00:00:00 2001 From: Kukks Date: Thu, 7 Sep 2023 10:04:18 +0200 Subject: [PATCH] fix file seller plugin --- .../BTCPayServer.Plugins.FileSeller.csproj | 2 +- .../FileSellerPlugin.cs | 2 +- .../FileSellerService.cs | 150 +++++++++--------- submodules/btcpayserver | 2 +- 4 files changed, 82 insertions(+), 74 deletions(-) diff --git a/Plugins/BTCPayServer.Plugins.FileSeller/BTCPayServer.Plugins.FileSeller.csproj b/Plugins/BTCPayServer.Plugins.FileSeller/BTCPayServer.Plugins.FileSeller.csproj index 93e93d0..3b4bcac 100644 --- a/Plugins/BTCPayServer.Plugins.FileSeller/BTCPayServer.Plugins.FileSeller.csproj +++ b/Plugins/BTCPayServer.Plugins.FileSeller/BTCPayServer.Plugins.FileSeller.csproj @@ -9,7 +9,7 @@ File Seller Allows you to sell files through the point of sale/crowdfund apps. - 1.0.1 + 1.0.2 diff --git a/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerPlugin.cs b/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerPlugin.cs index b58861c..eacbd51 100644 --- a/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerPlugin.cs +++ b/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerPlugin.cs @@ -9,7 +9,7 @@ public class FileSellerPlugin : BaseBTCPayServerPlugin { public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = { - new() { Identifier = nameof(BTCPayServer), Condition = ">=1.11.1" } + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.11.4" } }; public override void Execute(IServiceCollection applicationBuilder) { diff --git a/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerService.cs b/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerService.cs index d31afb3..d6f2e72 100644 --- a/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerService.cs +++ b/Plugins/BTCPayServer.Plugins.FileSeller/FileSellerService.cs @@ -15,16 +15,16 @@ using Newtonsoft.Json.Linq; namespace BTCPayServer.Plugins.FileSeller { - public class FileSellerService:EventHostedServiceBase + public class FileSellerService : EventHostedServiceBase { private readonly AppService _appService; private readonly FileService _fileService; private readonly InvoiceRepository _invoiceRepository; private readonly StoredFileRepository _storedFileRepository; - public FileSellerService(EventAggregator eventAggregator, - ILogger logger, - AppService appService, + public FileSellerService(EventAggregator eventAggregator, + ILogger logger, + AppService appService, FileService fileService, InvoiceRepository invoiceRepository, StoredFileRepository storedFileRepository) : base(eventAggregator, logger) @@ -46,98 +46,106 @@ namespace BTCPayServer.Plugins.FileSeller protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) { if (evt is not InvoiceEvent invoiceEvent) return; - Dictionary cartItems = null; - if (invoiceEvent.Name is not (InvoiceEvent.Completed or InvoiceEvent.MarkedCompleted or InvoiceEvent.Confirmed )) + List cartItems = null; + if (invoiceEvent.Name is not (InvoiceEvent.Completed or InvoiceEvent.MarkedCompleted + or InvoiceEvent.Confirmed)) { return; } + var appIds = AppService.GetAppInternalTags(invoiceEvent.Invoice); if (!appIds.Any()) { return; } - if(invoiceEvent.Invoice.Metadata.AdditionalData.TryGetValue("fileselleractivated", out var activated)) + + if (invoiceEvent.Invoice.Metadata.AdditionalData.TryGetValue("fileselleractivated", out var activated)) { return; } + if ((!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode) || AppService.TryParsePosCartItems(invoiceEvent.Invoice.Metadata.PosData, out cartItems))) { - var items = cartItems ?? new Dictionary(); - if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode)) + var items = cartItems ?? new List(); + if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode) && + !items.Exists(cartItem => cartItem.Id == invoiceEvent.Invoice.Metadata.ItemCode)) { - items.TryAdd(invoiceEvent.Invoice.Metadata.ItemCode, 1); + items.Add(new PosCartItem() + { + Id = invoiceEvent.Invoice.Metadata.ItemCode, + Count = 1, + Price = invoiceEvent.Invoice.Price + }); } - var apps = (await _appService.GetApps(appIds)).Select(data => + var apps = (await _appService.GetApps(appIds)).Select(data => + { + switch (data.AppType) { - switch (data.AppType) - { - case PointOfSaleAppType.AppType: - var possettings = data.GetSettings(); - return (Data: data, Settings: (object) possettings, - Items: AppService.Parse(possettings.Template)); - case CrowdfundAppType.AppType: - var cfsettings = data.GetSettings(); - return (Data: data, Settings: cfsettings, - Items: AppService.Parse(cfsettings.PerksTemplate)); - default: - return (null, null, null); - } - }).Where(tuple => tuple.Data != null && tuple.Items.Any(item => - item.AdditionalData?.ContainsKey("file") is true && - items.ContainsKey(item.Id))); + case PointOfSaleAppType.AppType: + var possettings = data.GetSettings(); + return (Data: data, Settings: (object) possettings, + Items: AppService.Parse(possettings.Template)); + case CrowdfundAppType.AppType: + var cfsettings = data.GetSettings(); + return (Data: data, Settings: cfsettings, + Items: AppService.Parse(cfsettings.PerksTemplate)); + default: + return (null, null, null); + } + }).Where(tuple => tuple.Data != null && tuple.Items.Any(item => + item.AdditionalData?.ContainsKey("file") is true && + items.Exists(cartItem => cartItem.Id == item.Id))); - var fileIds = new HashSet(); - - foreach (var valueTuple in apps) + var fileIds = new HashSet(); + + foreach (var valueTuple in apps) + { + foreach (var item1 in valueTuple.Items.Where(item => + item.AdditionalData?.ContainsKey("file") is true && + items.Exists(cartItem => cartItem.Id == item.Id))) { - foreach (var item1 in valueTuple.Items.Where(item => - item.AdditionalData?.ContainsKey("file") is true && - items.ContainsKey(item.Id))) - { - var fileId = item1.AdditionalData["file"].Value(); - fileIds.Add(fileId); - - } + var fileId = item1.AdditionalData["file"].Value(); + fileIds.Add(fileId); + } + } + + var loadedFiles = await _storedFileRepository.GetFiles(new StoredFileRepository.FilesQuery() + { + Id = fileIds.ToArray() + }); + var productLinkTasks = loadedFiles.ToDictionary(file => file, + file => _fileService.GetTemporaryFileUrl(UrlToUse, file.Id, DateTimeOffset.MaxValue, true)); + + var res = await Task.WhenAll(productLinkTasks.Values); + + + if (res.Any(s => !string.IsNullOrEmpty(s))) + { + var productTitleToFile = productLinkTasks.Select(pair => (pair.Key.FileName, pair.Value.Result)) + .Where(s => s.Result is not null) + .ToDictionary(tuple => tuple.FileName, tuple => tuple.Result); + + var receiptData = new JObject(); + receiptData.Add("Downloadable Content", JObject.FromObject(productTitleToFile)); + + if (invoiceEvent.Invoice.Metadata.AdditionalData?.TryGetValue("receiptData", + out var existingReceiptData) is true && + existingReceiptData is JObject existingReceiptDataObj) + { + receiptData.Merge(existingReceiptDataObj); } - var loadedFiles = await _storedFileRepository.GetFiles(new StoredFileRepository.FilesQuery() - { - Id = fileIds.ToArray() - }); - var productLinkTasks = loadedFiles.ToDictionary(file =>file,file => _fileService.GetTemporaryFileUrl(UrlToUse, file.Id, DateTimeOffset.MaxValue, true)); - - var res = await Task.WhenAll(productLinkTasks.Values); - - - if (res.Any(s => !string.IsNullOrEmpty(s))) - { - var productTitleToFile = productLinkTasks.Select(pair => (pair.Key.FileName, pair.Value.Result)) - .Where(s => s.Result is not null) - .ToDictionary(tuple => tuple.FileName, tuple => tuple.Result); - - var receiptData = new JObject(); - receiptData.Add("Downloadable Content", JObject.FromObject(productTitleToFile) ); - - if (invoiceEvent.Invoice.Metadata.AdditionalData?.TryGetValue("receiptData", - out var existingReceiptData) is true && existingReceiptData is JObject existingReceiptDataObj ) - { - receiptData.Merge(existingReceiptDataObj); - - } - - invoiceEvent.Invoice.Metadata.SetAdditionalData("receiptData", receiptData); - - } - invoiceEvent.Invoice.Metadata.SetAdditionalData("fileselleractivated", "true"); - await _invoiceRepository.UpdateInvoiceMetadata(invoiceEvent.InvoiceId, invoiceEvent.Invoice.StoreId, - invoiceEvent.Invoice.Metadata.ToJObject()); - - + invoiceEvent.Invoice.Metadata.SetAdditionalData("receiptData", receiptData); + } + invoiceEvent.Invoice.Metadata.SetAdditionalData("fileselleractivated", "true"); + await _invoiceRepository.UpdateInvoiceMetadata(invoiceEvent.InvoiceId, invoiceEvent.Invoice.StoreId, + invoiceEvent.Invoice.Metadata.ToJObject()); } + await base.ProcessEvent(evt, cancellationToken); } } diff --git a/submodules/btcpayserver b/submodules/btcpayserver index b3df403..cdffe9b 160000 --- a/submodules/btcpayserver +++ b/submodules/btcpayserver @@ -1 +1 @@ -Subproject commit b3df403980f007d73ef8b72fc425883339043943 +Subproject commit cdffe9b3551fb1c046f3572290c6b413d8060537