fix file seller plugin

This commit is contained in:
Kukks
2023-09-07 10:04:18 +02:00
parent a15d6299be
commit 00c8f6003b
4 changed files with 82 additions and 74 deletions

View File

@@ -9,7 +9,7 @@
<PropertyGroup>
<Product>File Seller</Product>
<Description>Allows you to sell files through the point of sale/crowdfund apps.</Description>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
</PropertyGroup>
<!-- Plugin development properties -->
<PropertyGroup>

View File

@@ -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)
{

View File

@@ -15,7 +15,7 @@ using Newtonsoft.Json.Linq;
namespace BTCPayServer.Plugins.FileSeller
{
public class FileSellerService:EventHostedServiceBase
public class FileSellerService : EventHostedServiceBase
{
private readonly AppService _appService;
private readonly FileService _fileService;
@@ -46,28 +46,38 @@ namespace BTCPayServer.Plugins.FileSeller
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
if (evt is not InvoiceEvent invoiceEvent) return;
Dictionary<string, int> cartItems = null;
if (invoiceEvent.Name is not (InvoiceEvent.Completed or InvoiceEvent.MarkedCompleted or InvoiceEvent.Confirmed ))
List<PosCartItem> 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<string, int>();
if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode))
var items = cartItems ?? new List<PosCartItem>();
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 =>
@@ -87,7 +97,7 @@ namespace BTCPayServer.Plugins.FileSeller
}
}).Where(tuple => tuple.Data != null && tuple.Items.Any(item =>
item.AdditionalData?.ContainsKey("file") is true &&
items.ContainsKey(item.Id)));
items.Exists(cartItem => cartItem.Id == item.Id)));
var fileIds = new HashSet<string>();
@@ -95,11 +105,10 @@ namespace BTCPayServer.Plugins.FileSeller
{
foreach (var item1 in valueTuple.Items.Where(item =>
item.AdditionalData?.ContainsKey("file") is true &&
items.ContainsKey(item.Id)))
items.Exists(cartItem => cartItem.Id == item.Id)))
{
var fileId = item1.AdditionalData["file"].Value<string>();
fileIds.Add(fileId);
}
}
@@ -107,7 +116,8 @@ namespace BTCPayServer.Plugins.FileSeller
{
Id = fileIds.ToArray()
});
var productLinkTasks = loadedFiles.ToDictionary(file =>file,file => _fileService.GetTemporaryFileUrl(UrlToUse, file.Id, DateTimeOffset.MaxValue, true));
var productLinkTasks = loadedFiles.ToDictionary(file => file,
file => _fileService.GetTemporaryFileUrl(UrlToUse, file.Id, DateTimeOffset.MaxValue, true));
var res = await Task.WhenAll(productLinkTasks.Values);
@@ -119,25 +129,23 @@ namespace BTCPayServer.Plugins.FileSeller
.ToDictionary(tuple => tuple.FileName, tuple => tuple.Result);
var receiptData = new JObject();
receiptData.Add("Downloadable Content", JObject.FromObject(productTitleToFile) );
receiptData.Add("Downloadable Content", JObject.FromObject(productTitleToFile));
if (invoiceEvent.Invoice.Metadata.AdditionalData?.TryGetValue("receiptData",
out var existingReceiptData) is true && existingReceiptData is JObject existingReceiptDataObj )
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());
}
await base.ProcessEvent(evt, cancellationToken);
}
}