mirror of
https://github.com/aljazceru/BTCPayServerPlugins.git
synced 2025-12-17 07:34:24 +01:00
fix file seller plugin
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Product>File Seller</Product>
|
<Product>File Seller</Product>
|
||||||
<Description>Allows you to sell files through the point of sale/crowdfund apps.</Description>
|
<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>
|
</PropertyGroup>
|
||||||
<!-- Plugin development properties -->
|
<!-- Plugin development properties -->
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public class FileSellerPlugin : BaseBTCPayServerPlugin
|
|||||||
{
|
{
|
||||||
public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } =
|
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)
|
public override void Execute(IServiceCollection applicationBuilder)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ using Newtonsoft.Json.Linq;
|
|||||||
|
|
||||||
namespace BTCPayServer.Plugins.FileSeller
|
namespace BTCPayServer.Plugins.FileSeller
|
||||||
{
|
{
|
||||||
public class FileSellerService:EventHostedServiceBase
|
public class FileSellerService : EventHostedServiceBase
|
||||||
{
|
{
|
||||||
private readonly AppService _appService;
|
private readonly AppService _appService;
|
||||||
private readonly FileService _fileService;
|
private readonly FileService _fileService;
|
||||||
@@ -46,28 +46,38 @@ namespace BTCPayServer.Plugins.FileSeller
|
|||||||
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (evt is not InvoiceEvent invoiceEvent) return;
|
if (evt is not InvoiceEvent invoiceEvent) return;
|
||||||
Dictionary<string, int> cartItems = null;
|
List<PosCartItem> cartItems = null;
|
||||||
if (invoiceEvent.Name is not (InvoiceEvent.Completed or InvoiceEvent.MarkedCompleted or InvoiceEvent.Confirmed ))
|
if (invoiceEvent.Name is not (InvoiceEvent.Completed or InvoiceEvent.MarkedCompleted
|
||||||
|
or InvoiceEvent.Confirmed))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var appIds = AppService.GetAppInternalTags(invoiceEvent.Invoice);
|
var appIds = AppService.GetAppInternalTags(invoiceEvent.Invoice);
|
||||||
|
|
||||||
if (!appIds.Any())
|
if (!appIds.Any())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(invoiceEvent.Invoice.Metadata.AdditionalData.TryGetValue("fileselleractivated", out var activated))
|
|
||||||
|
if (invoiceEvent.Invoice.Metadata.AdditionalData.TryGetValue("fileselleractivated", out var activated))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode) ||
|
if ((!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode) ||
|
||||||
AppService.TryParsePosCartItems(invoiceEvent.Invoice.Metadata.PosData, out cartItems)))
|
AppService.TryParsePosCartItems(invoiceEvent.Invoice.Metadata.PosData, out cartItems)))
|
||||||
{
|
{
|
||||||
var items = cartItems ?? new Dictionary<string, int>();
|
var items = cartItems ?? new List<PosCartItem>();
|
||||||
if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode))
|
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 =>
|
||||||
@@ -87,7 +97,7 @@ namespace BTCPayServer.Plugins.FileSeller
|
|||||||
}
|
}
|
||||||
}).Where(tuple => tuple.Data != null && tuple.Items.Any(item =>
|
}).Where(tuple => tuple.Data != null && tuple.Items.Any(item =>
|
||||||
item.AdditionalData?.ContainsKey("file") is true &&
|
item.AdditionalData?.ContainsKey("file") is true &&
|
||||||
items.ContainsKey(item.Id)));
|
items.Exists(cartItem => cartItem.Id == item.Id)));
|
||||||
|
|
||||||
var fileIds = new HashSet<string>();
|
var fileIds = new HashSet<string>();
|
||||||
|
|
||||||
@@ -95,11 +105,10 @@ namespace BTCPayServer.Plugins.FileSeller
|
|||||||
{
|
{
|
||||||
foreach (var item1 in valueTuple.Items.Where(item =>
|
foreach (var item1 in valueTuple.Items.Where(item =>
|
||||||
item.AdditionalData?.ContainsKey("file") is true &&
|
item.AdditionalData?.ContainsKey("file") is true &&
|
||||||
items.ContainsKey(item.Id)))
|
items.Exists(cartItem => cartItem.Id == item.Id)))
|
||||||
{
|
{
|
||||||
var fileId = item1.AdditionalData["file"].Value<string>();
|
var fileId = item1.AdditionalData["file"].Value<string>();
|
||||||
fileIds.Add(fileId);
|
fileIds.Add(fileId);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +116,8 @@ namespace BTCPayServer.Plugins.FileSeller
|
|||||||
{
|
{
|
||||||
Id = fileIds.ToArray()
|
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);
|
var res = await Task.WhenAll(productLinkTasks.Values);
|
||||||
|
|
||||||
@@ -119,25 +129,23 @@ namespace BTCPayServer.Plugins.FileSeller
|
|||||||
.ToDictionary(tuple => tuple.FileName, tuple => tuple.Result);
|
.ToDictionary(tuple => tuple.FileName, tuple => tuple.Result);
|
||||||
|
|
||||||
var receiptData = new JObject();
|
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",
|
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);
|
receiptData.Merge(existingReceiptDataObj);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invoiceEvent.Invoice.Metadata.SetAdditionalData("receiptData", receiptData);
|
invoiceEvent.Invoice.Metadata.SetAdditionalData("receiptData", receiptData);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invoiceEvent.Invoice.Metadata.SetAdditionalData("fileselleractivated", "true");
|
invoiceEvent.Invoice.Metadata.SetAdditionalData("fileselleractivated", "true");
|
||||||
await _invoiceRepository.UpdateInvoiceMetadata(invoiceEvent.InvoiceId, invoiceEvent.Invoice.StoreId,
|
await _invoiceRepository.UpdateInvoiceMetadata(invoiceEvent.InvoiceId, invoiceEvent.Invoice.StoreId,
|
||||||
invoiceEvent.Invoice.Metadata.ToJObject());
|
invoiceEvent.Invoice.Metadata.ToJObject());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await base.ProcessEvent(evt, cancellationToken);
|
await base.ProcessEvent(evt, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Submodule submodules/btcpayserver updated: b3df403980...cdffe9b355
Reference in New Issue
Block a user