When creating a new apps, the default currency of the store should be used

This commit is contained in:
nicolas.dorier
2021-10-25 16:54:36 +09:00
parent 951bfeefb1
commit a73383cd87
7 changed files with 50 additions and 23 deletions

View File

@@ -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<IActionResult> 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,

View File

@@ -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<PointOfSaleSettings>();
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($"<form method=\"POST\" action=\"{encoder.Encode(appUrl)}\">");
@@ -162,11 +162,14 @@ namespace BTCPayServer.Controllers
[Route("{appId}/settings/pos")]
public async Task<IActionResult> 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,

View File

@@ -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<string> 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<IActionResult> DeleteApp(string appId)
{

View File

@@ -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)]

View File

@@ -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; }

View File

@@ -47,8 +47,8 @@
</div>
<div class="col-lg-6">
<div class="form-group">
<label asp-for="TargetCurrency" class="form-label" data-required></label>
<input asp-for="TargetCurrency" class="form-control" required />
<label asp-for="TargetCurrency" class="form-label"></label>
<input asp-for="TargetCurrency" class="form-control" placeholder="Use store's default settings" />
<span asp-validation-for="TargetCurrency" class="text-danger"></span>
</div>
<div class="form-group">

View File

@@ -1,4 +1,4 @@
@using BTCPayServer.Services.Apps
@using BTCPayServer.Services.Apps
@addTagHelper *, BundlerMinifier.TagHelpers
@model UpdatePointOfSaleViewModel
@{
@@ -22,8 +22,8 @@
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Currency" class="form-label" data-required></label>
<input asp-for="Currency" class="form-control" required />
<label asp-for="Currency" class="form-label"></label>
<input asp-for="Currency" class="form-control" placeholder="Use store's default settings" />
<span asp-validation-for="Currency" class="text-danger"></span>
</div>
</div>