From 6eb36abe2e5b9a0f09b83daef96d935ac5d8df30 Mon Sep 17 00:00:00 2001 From: Kukks Date: Sat, 29 Dec 2018 11:52:07 +0100 Subject: [PATCH] start contrib perks --- .../Controllers/AppsController.Crowdsale.cs | 21 ++++- BTCPayServer/Controllers/AppsController.cs | 14 +++- .../Controllers/AppsPublicController.cs | 30 ++++++- BTCPayServer/Hubs/CrowdfundHub.cs | 17 ++-- .../AppViewModels/UpdateCrowdfundViewModel.cs | 3 + .../AppViewModels/ViewCrowdfundViewModel.cs | 5 +- .../Views/Apps/UpdateCrowdfund.cshtml | 82 +++++++++++++++++++ .../AppsPublic/Crowdfund/VueCrowdfund.cshtml | 2 +- .../wwwroot/crowdfund/services/fireworks.js | 2 +- 9 files changed, 156 insertions(+), 20 deletions(-) diff --git a/BTCPayServer/Controllers/AppsController.Crowdsale.cs b/BTCPayServer/Controllers/AppsController.Crowdsale.cs index e628182b3..432ce4f38 100644 --- a/BTCPayServer/Controllers/AppsController.Crowdsale.cs +++ b/BTCPayServer/Controllers/AppsController.Crowdsale.cs @@ -31,6 +31,7 @@ namespace BTCPayServer.Controllers public string NotificationUrl { get; set; } public string Tagline { get; set; } public string EmbeddedCSS { get; set; } + public string PerksTemplate { get; set; } } @@ -57,16 +58,31 @@ namespace BTCPayServer.Controllers CustomCSSLink = settings.CustomCSSLink, NotificationUrl = settings.NotificationUrl, Tagline = settings.Tagline, + PerksTemplate = settings.PerksTemplate }; return View(vm); } [HttpPost] [Route("{appId}/settings/crowdfund")] - public async Task UpdatePointOfSale(string appId, UpdateCrowdfundViewModel vm) + public async Task UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm) { if (_AppsHelper.GetCurrencyData(vm.TargetCurrency, false) == null) ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency"); + try + { + _AppsHelper.Parse(vm.PerksTemplate, vm.TargetCurrency); + } + catch + { + ModelState.AddModelError(nameof(vm.PerksTemplate), "Invalid template"); + } + if (!ModelState.IsValid) + { + return View(vm); + } + + var app = await GetOwnedApp(appId, AppType.Crowdfund); if (app == null) return NotFound(); @@ -84,7 +100,8 @@ namespace BTCPayServer.Controllers MainImageUrl = vm.MainImageUrl, EmbeddedCSS = vm.EmbeddedCSS, NotificationUrl = vm.NotificationUrl, - Tagline = vm.Tagline + Tagline = vm.Tagline, + PerksTemplate = vm.PerksTemplate }); await UpdateAppSettings(app); _EventAggregator.Publish(new CrowdfundAppUpdated() diff --git a/BTCPayServer/Controllers/AppsController.cs b/BTCPayServer/Controllers/AppsController.cs index 346b0c4e3..fac2c40f7 100644 --- a/BTCPayServer/Controllers/AppsController.cs +++ b/BTCPayServer/Controllers/AppsController.cs @@ -120,9 +120,17 @@ namespace BTCPayServer.Controllers } StatusMessage = "App successfully created"; CreatedAppId = id; - if (appType == AppType.PointOfSale) - return RedirectToAction(nameof(UpdatePointOfSale), new { appId = id }); - return RedirectToAction(nameof(ListApps)); + + switch (appType) + { + case AppType.PointOfSale: + return RedirectToAction(nameof(UpdatePointOfSale), new { appId = id }); + case AppType.Crowdfund: + return RedirectToAction(nameof(UpdateCrowdfund), new { appId = id }); + default: + return RedirectToAction(nameof(ListApps)); + } + } [HttpGet] diff --git a/BTCPayServer/Controllers/AppsPublicController.cs b/BTCPayServer/Controllers/AppsPublicController.cs index 98dc70337..128a15840 100644 --- a/BTCPayServer/Controllers/AppsPublicController.cs +++ b/BTCPayServer/Controllers/AppsPublicController.cs @@ -113,18 +113,40 @@ namespace BTCPayServer.Controllers return NotFound(); var settings = app.GetSettings(); var store = await _AppsHelper.GetStore(app); - + string title = null; + var price = 0.0m; + if (!string.IsNullOrEmpty(request.ChoiceKey)) + { + var choices = _AppsHelper.Parse(settings.PerksTemplate, settings.TargetCurrency); + var choice = choices.FirstOrDefault(c => c.Id == request.ChoiceKey); + if (choice == null) + return NotFound(); + title = choice.Title; + price = choice.Price.Value; + if (request.Amount > price) + price = request.Amount; + } + else + { + price = request.Amount; + title = settings.Title; + } + + store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id)); var invoice = await _InvoiceController.CreateInvoiceCore(new Invoice() { OrderId = $"{CrowdfundHubStreamer.CrowdfundInvoiceOrderIdPrefix}{appId}", Currency = settings.TargetCurrency, + ItemCode = request.ChoiceKey ?? string.Empty, + ItemDesc = title, BuyerEmail = request.Email, - Price = request.Amount, + Price = price, NotificationURL = settings.NotificationUrl, FullNotifications = true, ExtendedNotifications = true, - RedirectURL = HttpContext.Request.GetAbsoluteRoot()+ "/apps/{appId}/crowdfund", + RedirectURL = request.RedirectUrl, + }, store, HttpContext.Request.GetAbsoluteRoot()); if (request.RedirectToCheckout) @@ -195,7 +217,7 @@ namespace BTCPayServer.Controllers OrderId = orderId, NotificationURL = notificationUrl, RedirectURL = redirectUrl, - FullNotifications = true + FullNotifications = true, }, store, HttpContext.Request.GetAbsoluteRoot()); return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id }); } diff --git a/BTCPayServer/Hubs/CrowdfundHub.cs b/BTCPayServer/Hubs/CrowdfundHub.cs index 32e97f385..6be5cfd23 100644 --- a/BTCPayServer/Hubs/CrowdfundHub.cs +++ b/BTCPayServer/Hubs/CrowdfundHub.cs @@ -96,8 +96,7 @@ namespace BTCPayServer.Hubs entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1); var app = await _AppsHelper.GetApp(appId, AppType.Crowdfund, true); - var result = await GetInfo(app, _InvoiceRepository, _RateFetcher, - _BtcPayNetworkProvider); + var result = await GetInfo(app); entry.SetValue(result); return result; }); @@ -191,26 +190,27 @@ namespace BTCPayServer.Hubs } - private static async Task GetInfo(AppData appData, InvoiceRepository invoiceRepository, - RateFetcher rateFetcher, BTCPayNetworkProvider btcPayNetworkProvider, string statusMessage= null) + private async Task GetInfo(AppData appData, string statusMessage= null) { var settings = appData.GetSettings(); - var invoices = await GetInvoicesForApp(appData, invoiceRepository); + var invoices = await GetInvoicesForApp(appData, _InvoiceRepository); - var rateRules = appData.StoreData.GetStoreBlob().GetRateRules(btcPayNetworkProvider); + var rateRules = appData.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider); var currentAmount = await GetCurrentContributionAmount( invoices.Where(entity => entity.Status == InvoiceStatus.Complete).ToArray(), - settings.TargetCurrency, rateFetcher, rateRules); + settings.TargetCurrency, _RateFetcher, rateRules); var currentPendingAmount = await GetCurrentContributionAmount( invoices.Where(entity => entity.Status != InvoiceStatus.Complete).ToArray(), - settings.TargetCurrency, rateFetcher, rateRules); + settings.TargetCurrency, _RateFetcher, rateRules); var active = (settings.StartDate == null || DateTime.Now >= settings.StartDate) && (settings.EndDate == null || DateTime.Now <= settings.EndDate) && (!settings.EnforceTargetAmount || settings.TargetAmount > currentAmount); + + return new ViewCrowdfundViewModel() { Title = settings.Title, @@ -227,6 +227,7 @@ namespace BTCPayServer.Hubs TargetCurrency = settings.TargetCurrency, EnforceTargetAmount = settings.EnforceTargetAmount, StatusMessage = statusMessage, + Perks = _AppsHelper.Parse(settings.PerksTemplate, settings.TargetCurrency), Info = new ViewCrowdfundViewModel.CrowdfundInfo() { TotalContributors = invoices.Length, diff --git a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs index ed05024f4..f84b625a5 100644 --- a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs @@ -36,6 +36,9 @@ namespace BTCPayServer.Models.AppViewModels [Display(Name = "Do not allow additional contributions after target has been reached")] public bool EnforceTargetAmount { get; set; } + [Display(Name = "Contribution Perks Template")] + public string PerksTemplate { get; set; } + [MaxLength(500)] [Display(Name = "Custom bootstrap CSS file")] public string CustomCSSLink { get; set; } diff --git a/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs b/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs index fc80f8f23..3c35b2b35 100644 --- a/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs @@ -22,6 +22,7 @@ namespace BTCPayServer.Models.AppViewModels public CrowdfundInfo Info { get; set; } public string Tagline { get; set; } + public ViewPointOfSaleViewModel.Item[] Perks { get; set; } public class CrowdfundInfo @@ -46,6 +47,8 @@ namespace BTCPayServer.Models.AppViewModels public ViewCrowdfundViewModel ViewCrowdfundViewModel { get; set; } [Required] public decimal Amount { get; set; } public string Email { get; set; } - public bool RedirectToCheckout { get; set; } + public string ChoiceKey { get; set; } + public bool RedirectToCheckout { get; set; } + public string RedirectUrl { get; set; } } } diff --git a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml index 0f465b350..73b70bcb0 100644 --- a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml +++ b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml @@ -3,6 +3,24 @@ ViewData["Title"] = "Update Crowdfund"; }
+
@@ -61,6 +79,19 @@
+
+ + +
+
+
+
+ * + + +
@@ -104,5 +135,56 @@ + + + + + + + } diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml index 001da81ee..ba3239e4f 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml @@ -94,7 +94,7 @@

{{srvModel.tagline}}

- +
diff --git a/BTCPayServer/wwwroot/crowdfund/services/fireworks.js b/BTCPayServer/wwwroot/crowdfund/services/fireworks.js index 4915727e6..d6af58654 100644 --- a/BTCPayServer/wwwroot/crowdfund/services/fireworks.js +++ b/BTCPayServer/wwwroot/crowdfund/services/fireworks.js @@ -170,7 +170,7 @@ var resizeCanvas = function() { window.addEventListener("resize", resizeCanvas); // addClickListeners(); - handleInactiveUser(); + // handleInactiveUser(); })(); function handleInactiveUser() {