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 System;
using BTCPayServer.Data;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Models.AppViewModels; using BTCPayServer.Models.AppViewModels;
@@ -66,7 +67,11 @@ namespace BTCPayServer.Controllers
[Route("{appId}/settings/crowdfund")] [Route("{appId}/settings/crowdfund")]
public async Task<IActionResult> UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm, string command) 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"); ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency");
try try
@@ -116,11 +121,6 @@ namespace BTCPayServer.Controllers
return View(vm); return View(vm);
} }
var app = await GetOwnedApp(appId, AppType.Crowdfund);
if (app == null)
return NotFound();
var newSettings = new CrowdfundSettings() var newSettings = new CrowdfundSettings()
{ {
Title = vm.Title, Title = vm.Title,

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using BTCPayServer.Data;
using System.Text; using System.Text;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -17,7 +18,6 @@ namespace BTCPayServer.Controllers
public PointOfSaleSettings() public PointOfSaleSettings()
{ {
Title = "Tea shop"; Title = "Tea shop";
Currency = "USD";
Template = Template =
"green tea:\n" + "green tea:\n" +
" price: 1\n" + " price: 1\n" +
@@ -96,7 +96,6 @@ namespace BTCPayServer.Controllers
var settings = app.GetSettings<PointOfSaleSettings>(); var settings = app.GetSettings<PointOfSaleSettings>();
settings.DefaultView = settings.EnableShoppingCart ? PosViewType.Cart : settings.DefaultView; settings.DefaultView = settings.EnableShoppingCart ? PosViewType.Cart : settings.DefaultView;
settings.EnableShoppingCart = false; settings.EnableShoppingCart = false;
var vm = new UpdatePointOfSaleViewModel var vm = new UpdatePointOfSaleViewModel
{ {
Id = appId, Id = appId,
@@ -123,7 +122,7 @@ namespace BTCPayServer.Controllers
}; };
if (HttpContext?.Request != null) 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; var encoder = HtmlEncoder.Default;
if (settings.ShowCustomAmount) if (settings.ShowCustomAmount)
{ {
@@ -140,6 +139,7 @@ namespace BTCPayServer.Controllers
} }
try try
{ {
var items = _AppService.Parse(settings.Template, settings.Currency); var items = _AppService.Parse(settings.Template, settings.Currency);
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendLine($"<form method=\"POST\" action=\"{encoder.Encode(appUrl)}\">"); builder.AppendLine($"<form method=\"POST\" action=\"{encoder.Encode(appUrl)}\">");
@@ -162,11 +162,14 @@ namespace BTCPayServer.Controllers
[Route("{appId}/settings/pos")] [Route("{appId}/settings/pos")]
public async Task<IActionResult> UpdatePointOfSale(string appId, UpdatePointOfSaleViewModel vm) public async Task<IActionResult> UpdatePointOfSale(string appId, UpdatePointOfSaleViewModel vm)
{ {
var app = await GetOwnedApp(appId, AppType.PointOfSale);
if (app == null)
return NotFound();
if (!ModelState.IsValid) if (!ModelState.IsValid)
{ {
return View(vm); return View(vm);
} }
vm.Currency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, vm.Currency);
if (_currencies.GetCurrencyData(vm.Currency, false) == null) if (_currencies.GetCurrencyData(vm.Currency, false) == null)
ModelState.AddModelError(nameof(vm.Currency), "Invalid currency"); ModelState.AddModelError(nameof(vm.Currency), "Invalid currency");
try try
@@ -181,9 +184,6 @@ namespace BTCPayServer.Controllers
{ {
return View(vm); return View(vm);
} }
var app = await GetOwnedApp(appId, AppType.PointOfSale);
if (app == null)
return NotFound();
app.SetSettings(new PointOfSaleSettings app.SetSettings(new PointOfSaleSettings
{ {
Title = vm.Title, Title = vm.Title,
@@ -191,7 +191,7 @@ namespace BTCPayServer.Controllers
ShowCustomAmount = vm.ShowCustomAmount, ShowCustomAmount = vm.ShowCustomAmount,
ShowDiscount = vm.ShowDiscount, ShowDiscount = vm.ShowDiscount,
EnableTips = vm.EnableTips, EnableTips = vm.EnableTips,
Currency = vm.Currency.ToUpperInvariant(), Currency = vm.Currency,
Template = vm.Template, Template = vm.Template,
ButtonText = vm.ButtonText, ButtonText = vm.ButtonText,
CustomButtonText = vm.CustomButtonText, CustomButtonText = vm.CustomButtonText,

View File

@@ -1,16 +1,17 @@
using System; using System;
using BTCPayServer.Data;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Abstractions.Models; using BTCPayServer.Abstractions.Models;
using BTCPayServer.Data;
using BTCPayServer.Models; using BTCPayServer.Models;
using BTCPayServer.Models.AppViewModels; using BTCPayServer.Models.AppViewModels;
using BTCPayServer.Security; using BTCPayServer.Security;
using BTCPayServer.Services.Apps; using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Mails; using BTCPayServer.Services.Mails;
using BTCPayServer.Services.Rates; using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -29,6 +30,7 @@ namespace BTCPayServer.Controllers
BTCPayNetworkProvider networkProvider, BTCPayNetworkProvider networkProvider,
CurrencyNameTable currencies, CurrencyNameTable currencies,
EmailSenderFactory emailSenderFactory, EmailSenderFactory emailSenderFactory,
Services.Stores.StoreRepository storeRepository,
AppService AppService) AppService AppService)
{ {
_UserManager = userManager; _UserManager = userManager;
@@ -37,6 +39,7 @@ namespace BTCPayServer.Controllers
_NetworkProvider = networkProvider; _NetworkProvider = networkProvider;
_currencies = currencies; _currencies = currencies;
_emailSenderFactory = emailSenderFactory; _emailSenderFactory = emailSenderFactory;
_storeRepository = storeRepository;
_AppService = AppService; _AppService = AppService;
} }
@@ -46,6 +49,7 @@ namespace BTCPayServer.Controllers
private readonly BTCPayNetworkProvider _NetworkProvider; private readonly BTCPayNetworkProvider _NetworkProvider;
private readonly CurrencyNameTable _currencies; private readonly CurrencyNameTable _currencies;
private readonly EmailSenderFactory _emailSenderFactory; private readonly EmailSenderFactory _emailSenderFactory;
private readonly StoreRepository _storeRepository;
private readonly AppService _AppService; private readonly AppService _AppService;
public string CreatedAppId { get; set; } public string CreatedAppId { get; set; }
@@ -162,6 +166,22 @@ namespace BTCPayServer.Controllers
Name = vm.Name, Name = vm.Name,
AppType = appType.ToString() 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); await _AppService.UpdateOrCreateApp(appData);
TempData[WellKnownTempData.SuccessMessage] = "App successfully created"; TempData[WellKnownTempData.SuccessMessage] = "App successfully created";
CreatedAppId = appData.Id; 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")] [HttpGet("{appId}/delete")]
public async Task<IActionResult> DeleteApp(string appId) public async Task<IActionResult> DeleteApp(string appId)
{ {

View File

@@ -52,10 +52,9 @@ namespace BTCPayServer.Models.AppViewModels
[Display(Name = "End date")] [Display(Name = "End date")]
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }
[Required]
[MaxLength(5)] [MaxLength(5)]
[Display(Name = "Primary currency used for targets and stats. (e.g. BTC, LTC, USD, etc.)")] [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")] [Display(Name = "Set a target amount")]
[Range(0, double.PositiveInfinity)] [Range(0, double.PositiveInfinity)]

View File

@@ -14,7 +14,6 @@ namespace BTCPayServer.Models.AppViewModels
[Required] [Required]
[MaxLength(30)] [MaxLength(30)]
public string Title { get; set; } public string Title { get; set; }
[Required]
[MaxLength(5)] [MaxLength(5)]
public string Currency { get; set; } public string Currency { get; set; }
public string Template { get; set; } public string Template { get; set; }

View File

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

View File

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