From a73383cd8792d4282b982c5ba6bbbd855929f7f3 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 25 Oct 2021 16:54:36 +0900 Subject: [PATCH] When creating a new apps, the default currency of the store should be used --- .../Controllers/AppsController.Crowdfund.cs | 12 +++---- .../Controllers/AppsController.PointOfSale.cs | 16 +++++----- BTCPayServer/Controllers/AppsController.cs | 31 ++++++++++++++++++- .../AppViewModels/UpdateCrowdfundViewModel.cs | 3 +- .../UpdatePointOfSaleViewModel.cs | 1 - .../Views/Apps/UpdateCrowdfund.cshtml | 4 +-- .../Views/Apps/UpdatePointOfSale.cshtml | 6 ++-- 7 files changed, 50 insertions(+), 23 deletions(-) diff --git a/BTCPayServer/Controllers/AppsController.Crowdfund.cs b/BTCPayServer/Controllers/AppsController.Crowdfund.cs index 76c3b4d85..e7b8b79e3 100644 --- a/BTCPayServer/Controllers/AppsController.Crowdfund.cs +++ b/BTCPayServer/Controllers/AppsController.Crowdfund.cs @@ -1,4 +1,5 @@ using System; +using BTCPayServer.Data; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Models.AppViewModels; @@ -66,7 +67,11 @@ namespace BTCPayServer.Controllers [Route("{appId}/settings/crowdfund")] public async Task UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm, string command) { - if (!string.IsNullOrEmpty(vm.TargetCurrency) && _currencies.GetCurrencyData(vm.TargetCurrency, false) == null) + var app = await GetOwnedApp(appId, AppType.Crowdfund); + if (app == null) + return NotFound(); + vm.TargetCurrency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, vm.TargetCurrency); + if (_currencies.GetCurrencyData(vm.TargetCurrency, false) == null) ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency"); try @@ -116,11 +121,6 @@ namespace BTCPayServer.Controllers return View(vm); } - - var app = await GetOwnedApp(appId, AppType.Crowdfund); - if (app == null) - return NotFound(); - var newSettings = new CrowdfundSettings() { Title = vm.Title, diff --git a/BTCPayServer/Controllers/AppsController.PointOfSale.cs b/BTCPayServer/Controllers/AppsController.PointOfSale.cs index c78fcf927..e70c32bca 100644 --- a/BTCPayServer/Controllers/AppsController.PointOfSale.cs +++ b/BTCPayServer/Controllers/AppsController.PointOfSale.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using BTCPayServer.Data; using System.Text; using System.Text.Encodings.Web; using System.Text.RegularExpressions; @@ -17,7 +18,6 @@ namespace BTCPayServer.Controllers public PointOfSaleSettings() { Title = "Tea shop"; - Currency = "USD"; Template = "green tea:\n" + " price: 1\n" + @@ -96,7 +96,6 @@ namespace BTCPayServer.Controllers var settings = app.GetSettings(); settings.DefaultView = settings.EnableShoppingCart ? PosViewType.Cart : settings.DefaultView; settings.EnableShoppingCart = false; - var vm = new UpdatePointOfSaleViewModel { Id = appId, @@ -123,7 +122,7 @@ namespace BTCPayServer.Controllers }; if (HttpContext?.Request != null) { - var appUrl = HttpContext.Request.GetAbsoluteRoot().WithTrailingSlash() + $"apps/{appId}/pos"; + var appUrl = HttpContext.Request.GetAbsoluteUri($"/apps/{appId}/pos"); var encoder = HtmlEncoder.Default; if (settings.ShowCustomAmount) { @@ -140,6 +139,7 @@ namespace BTCPayServer.Controllers } try { + var items = _AppService.Parse(settings.Template, settings.Currency); var builder = new StringBuilder(); builder.AppendLine($"
"); @@ -162,11 +162,14 @@ namespace BTCPayServer.Controllers [Route("{appId}/settings/pos")] public async Task UpdatePointOfSale(string appId, UpdatePointOfSaleViewModel vm) { + var app = await GetOwnedApp(appId, AppType.PointOfSale); + if (app == null) + return NotFound(); if (!ModelState.IsValid) { return View(vm); } - + vm.Currency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, vm.Currency); if (_currencies.GetCurrencyData(vm.Currency, false) == null) ModelState.AddModelError(nameof(vm.Currency), "Invalid currency"); try @@ -181,9 +184,6 @@ namespace BTCPayServer.Controllers { return View(vm); } - var app = await GetOwnedApp(appId, AppType.PointOfSale); - if (app == null) - return NotFound(); app.SetSettings(new PointOfSaleSettings { Title = vm.Title, @@ -191,7 +191,7 @@ namespace BTCPayServer.Controllers ShowCustomAmount = vm.ShowCustomAmount, ShowDiscount = vm.ShowDiscount, EnableTips = vm.EnableTips, - Currency = vm.Currency.ToUpperInvariant(), + Currency = vm.Currency, Template = vm.Template, ButtonText = vm.ButtonText, CustomButtonText = vm.CustomButtonText, diff --git a/BTCPayServer/Controllers/AppsController.cs b/BTCPayServer/Controllers/AppsController.cs index 564256934..8f92886b5 100644 --- a/BTCPayServer/Controllers/AppsController.cs +++ b/BTCPayServer/Controllers/AppsController.cs @@ -1,16 +1,17 @@ using System; +using BTCPayServer.Data; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Models; -using BTCPayServer.Data; using BTCPayServer.Models; using BTCPayServer.Models.AppViewModels; using BTCPayServer.Security; using BTCPayServer.Services.Apps; using BTCPayServer.Services.Mails; using BTCPayServer.Services.Rates; +using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -29,6 +30,7 @@ namespace BTCPayServer.Controllers BTCPayNetworkProvider networkProvider, CurrencyNameTable currencies, EmailSenderFactory emailSenderFactory, + Services.Stores.StoreRepository storeRepository, AppService AppService) { _UserManager = userManager; @@ -37,6 +39,7 @@ namespace BTCPayServer.Controllers _NetworkProvider = networkProvider; _currencies = currencies; _emailSenderFactory = emailSenderFactory; + _storeRepository = storeRepository; _AppService = AppService; } @@ -46,6 +49,7 @@ namespace BTCPayServer.Controllers private readonly BTCPayNetworkProvider _NetworkProvider; private readonly CurrencyNameTable _currencies; private readonly EmailSenderFactory _emailSenderFactory; + private readonly StoreRepository _storeRepository; private readonly AppService _AppService; public string CreatedAppId { get; set; } @@ -162,6 +166,22 @@ namespace BTCPayServer.Controllers Name = vm.Name, AppType = appType.ToString() }; + + var defaultCurrency = await GetStoreDefaultCurrentIfEmpty(appData.StoreDataId, null); + switch (appType) + { + case AppType.Crowdfund: + var emptyCrowdfund = new CrowdfundSettings(); + emptyCrowdfund.TargetCurrency = defaultCurrency; + appData.SetSettings(emptyCrowdfund); + break; + case AppType.PointOfSale: + var empty = new PointOfSaleSettings(); + empty.Currency = defaultCurrency; + appData.SetSettings(empty); + break; + } + await _AppService.UpdateOrCreateApp(appData); TempData[WellKnownTempData.SuccessMessage] = "App successfully created"; CreatedAppId = appData.Id; @@ -177,6 +197,15 @@ namespace BTCPayServer.Controllers } } + async Task GetStoreDefaultCurrentIfEmpty(string storeId, string currency) + { + if (String.IsNullOrWhiteSpace(currency)) + { + currency = (await _storeRepository.FindStore(storeId)).GetStoreBlob().DefaultCurrency; + } + return currency.Trim().ToUpperInvariant(); + } + [HttpGet("{appId}/delete")] public async Task DeleteApp(string appId) { diff --git a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs index d38037dd0..26b390246 100644 --- a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs @@ -52,10 +52,9 @@ namespace BTCPayServer.Models.AppViewModels [Display(Name = "End date")] public DateTime? EndDate { get; set; } - [Required] [MaxLength(5)] [Display(Name = "Primary currency used for targets and stats. (e.g. BTC, LTC, USD, etc.)")] - public string TargetCurrency { get; set; } = "BTC"; + public string TargetCurrency { get; set; } [Display(Name = "Set a target amount")] [Range(0, double.PositiveInfinity)] diff --git a/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs b/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs index 5f72b9628..e217bc73b 100644 --- a/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs @@ -14,7 +14,6 @@ namespace BTCPayServer.Models.AppViewModels [Required] [MaxLength(30)] public string Title { get; set; } - [Required] [MaxLength(5)] public string Currency { get; set; } public string Template { get; set; } diff --git a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml index 91bdacd8f..d0a6cd402 100644 --- a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml +++ b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml @@ -47,8 +47,8 @@
- - + +
diff --git a/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml b/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml index e038da2bf..790b82c9f 100644 --- a/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml +++ b/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml @@ -1,4 +1,4 @@ -@using BTCPayServer.Services.Apps +@using BTCPayServer.Services.Apps @addTagHelper *, BundlerMinifier.TagHelpers @model UpdatePointOfSaleViewModel @{ @@ -22,8 +22,8 @@
- - + +