From a60072a431cddf19c88ea4da1e8fb94823663eb3 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Mon, 11 Mar 2024 14:18:47 +0100 Subject: [PATCH] do not have report name conflict with old plugin (#5826) * do not have report name conflict with old plugin * tryadd instead of add * Apply #5816 to crowdfund too --- .../Controllers/UICrowdfundController.cs | 61 +++++++++++++------ BTCPayServer/Services/ReportService.cs | 6 +- .../LegacyInvoiceExportReportProvider.cs | 2 +- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs b/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs index 8002cf4b0..4e6cc1d3f 100644 --- a/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs +++ b/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs @@ -132,26 +132,9 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers } JObject formResponseJObject = null; - - if (settings.FormId is not null) - { - var formData = await FormDataService.GetForm(settings.FormId); - if (formData is not null) - { - formResponseJObject = TryParseJObject(formResponse) ?? new JObject(); - var form = Form.Parse(formData.Config); - FormDataService.SetValues(form, formResponseJObject); - if (!FormDataService.Validate(form, ModelState)) - { - // someone tried to bypass validation - return RedirectToAction(nameof(ViewCrowdfund), new { appId }); - } - } - } - var store = await _appService.GetStore(app); - var title = settings.Title; decimal? price = request.Amount; + var title = settings.Title; Dictionary paymentMethods = null; ViewPointOfSaleViewModel.Item choice = null; if (!string.IsNullOrEmpty(request.ChoiceKey)) @@ -194,6 +177,48 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers price = request.Amount; } + + if (settings.FormId is not null) + { + var formData = await FormDataService.GetForm(settings.FormId); + if (formData is not null) + { + formResponseJObject = TryParseJObject(formResponse) ?? new JObject(); + var form = Form.Parse(formData.Config); + FormDataService.SetValues(form, formResponseJObject); + if (!FormDataService.Validate(form, ModelState)) + { + // someone tried to bypass validation + return RedirectToAction(nameof(ViewCrowdfund), new { appId }); + } + + + var amtField = form.GetFieldByFullName($"{FormDataService.InvoiceParameterPrefix}amount"); + if (amtField is null) + { + form.Fields.Add(new Field + { + Name = $"{FormDataService.InvoiceParameterPrefix}amount", + Type = "hidden", + Value = price?.ToString(), + Constant = true + }); + } + else + { + amtField.Value = price?.ToString(); + } + formResponseJObject = FormDataService.GetValues(form); + + var invoiceRequest = FormDataService.GenerateInvoiceParametersFromForm(form); + if (invoiceRequest.Amount is not null) + { + price = invoiceRequest.Amount.Value; + } + } + } + + if (!isAdmin && (settings.EnforceTargetAmount && info.TargetAmount.HasValue && price > (info.TargetAmount - (info.Info.CurrentAmount + info.Info.CurrentPendingAmount)))) diff --git a/BTCPayServer/Services/ReportService.cs b/BTCPayServer/Services/ReportService.cs index d20108209..7fe2ed8fe 100644 --- a/BTCPayServer/Services/ReportService.cs +++ b/BTCPayServer/Services/ReportService.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.Linq; -using BTCPayServer.Client.Models; using BTCPayServer.Services.Reporting; namespace BTCPayServer.Services @@ -11,10 +9,10 @@ namespace BTCPayServer.Services { foreach (var r in reportProviders) { - ReportProviders.Add(r.Name, r); + ReportProviders.TryAdd(r.Name, r); } } - public Dictionary ReportProviders { get; } = new Dictionary(); + public Dictionary ReportProviders { get; } = new(); } } diff --git a/BTCPayServer/Services/Reporting/LegacyInvoiceExportReportProvider.cs b/BTCPayServer/Services/Reporting/LegacyInvoiceExportReportProvider.cs index c5ac47047..b0f8af2da 100644 --- a/BTCPayServer/Services/Reporting/LegacyInvoiceExportReportProvider.cs +++ b/BTCPayServer/Services/Reporting/LegacyInvoiceExportReportProvider.cs @@ -15,7 +15,7 @@ public class LegacyInvoiceExportReportProvider : ReportProvider private readonly InvoiceRepository _invoiceRepository; - public override string Name { get; } = "Invoice Export (legacy)"; + public override string Name { get; } = "Legacy Invoice Export"; public override async Task Query(QueryContext queryContext, CancellationToken cancellation) {