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>
|
||||
<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>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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<FileSellerService> logger,
|
||||
AppService appService,
|
||||
public FileSellerService(EventAggregator eventAggregator,
|
||||
ILogger<FileSellerService> 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<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 =>
|
||||
var apps = (await _appService.GetApps(appIds)).Select(data =>
|
||||
{
|
||||
switch (data.AppType)
|
||||
{
|
||||
switch (data.AppType)
|
||||
{
|
||||
case PointOfSaleAppType.AppType:
|
||||
var possettings = data.GetSettings<PointOfSaleSettings>();
|
||||
return (Data: data, Settings: (object) possettings,
|
||||
Items: AppService.Parse(possettings.Template));
|
||||
case CrowdfundAppType.AppType:
|
||||
var cfsettings = data.GetSettings<CrowdfundSettings>();
|
||||
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<PointOfSaleSettings>();
|
||||
return (Data: data, Settings: (object) possettings,
|
||||
Items: AppService.Parse(possettings.Template));
|
||||
case CrowdfundAppType.AppType:
|
||||
var cfsettings = data.GetSettings<CrowdfundSettings>();
|
||||
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<string>();
|
||||
|
||||
foreach (var valueTuple in apps)
|
||||
var fileIds = new HashSet<string>();
|
||||
|
||||
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<string>();
|
||||
fileIds.Add(fileId);
|
||||
|
||||
}
|
||||
var fileId = item1.AdditionalData["file"].Value<string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Submodule submodules/btcpayserver updated: b3df403980...cdffe9b355
Reference in New Issue
Block a user