diff --git a/BTCPayServer/Controllers/AppsController.PointOfSale.cs b/BTCPayServer/Controllers/AppsController.PointOfSale.cs index 27290739b..301b62354 100644 --- a/BTCPayServer/Controllers/AppsController.PointOfSale.cs +++ b/BTCPayServer/Controllers/AppsController.PointOfSale.cs @@ -65,7 +65,7 @@ namespace BTCPayServer.Controllers [Route("{appId}/settings/pos")] public async Task UpdatePointOfSale(string appId, UpdatePointOfSaleViewModel vm) { - if (_Currencies.GetCurrencyData(vm.Currency) == null) + if (_Currencies.GetCurrencyData(vm.Currency, false) == null) ModelState.AddModelError(nameof(vm.Currency), "Invalid currency"); try { @@ -102,7 +102,7 @@ namespace BTCPayServer.Controllers if (app == null) return NotFound(); var settings = app.GetSettings(); - var currency = _Currencies.GetCurrencyData(settings.Currency); + var currency = _Currencies.GetCurrencyData(settings.Currency, false); double step = currency == null ? 1 : Math.Pow(10, -(currency.Divisibility)); return View(new ViewPointOfSaleViewModel() { diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 699fcd564..4b32fc9f5 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -305,8 +305,8 @@ namespace BTCPayServer.Controllers public static string FormatCurrency(decimal price, string currency, CurrencyNameTable currencies) { - var provider = currencies.GetNumberFormatInfo(currency); - var currencyData = currencies.GetCurrencyData(currency); + var provider = currencies.GetNumberFormatInfo(currency, true); + var currencyData = currencies.GetCurrencyData(currency, true); var divisibility = currencyData.Divisibility; while (true) { diff --git a/BTCPayServer/Controllers/RateController.cs b/BTCPayServer/Controllers/RateController.cs index 9f5f9a4a2..8c35924f5 100644 --- a/BTCPayServer/Controllers/RateController.cs +++ b/BTCPayServer/Controllers/RateController.cs @@ -89,7 +89,7 @@ namespace BTCPayServer.Controllers CryptoCode = r.Pair.Left, Code = r.Pair.Right, CurrencyPair = r.Pair.ToString(), - Name = _CurrencyNameTable.GetCurrencyData(r.Pair.Right)?.Name, + Name = _CurrencyNameTable.GetCurrencyData(r.Pair.Right, true).Name, Value = r.Value.Value }).Where(n => n.Name != null).ToArray()); } diff --git a/BTCPayServer/CurrencyValue.cs b/BTCPayServer/CurrencyValue.cs index 26d98c3b5..ef3f2d812 100644 --- a/BTCPayServer/CurrencyValue.cs +++ b/BTCPayServer/CurrencyValue.cs @@ -21,7 +21,7 @@ namespace BTCPayServer return false; var currency = match.Groups.Last().Value.ToUpperInvariant(); - var currencyData = _CurrencyTable.GetCurrencyData(currency); + var currencyData = _CurrencyTable.GetCurrencyData(currency, false); if (currencyData == null) return false; v = Math.Round(v, currencyData.Divisibility); diff --git a/BTCPayServer/Services/Rates/CurrencyNameTable.cs b/BTCPayServer/Services/Rates/CurrencyNameTable.cs index 4478d9c61..a709890e0 100644 --- a/BTCPayServer/Services/Rates/CurrencyNameTable.cs +++ b/BTCPayServer/Services/Rates/CurrencyNameTable.cs @@ -42,19 +42,21 @@ namespace BTCPayServer.Services.Rates static Dictionary _CurrencyProviders = new Dictionary(); - public NumberFormatInfo GetNumberFormatInfo(string currency) + public NumberFormatInfo GetNumberFormatInfo(string currency, bool useFallback) { var data = GetCurrencyProvider(currency); if (data is NumberFormatInfo nfi) return nfi; if (data is CultureInfo ci) return ci.NumberFormat; + if (!useFallback) + return null; return CreateFallbackCurrencyFormatInfo(currency); } private NumberFormatInfo CreateFallbackCurrencyFormatInfo(string currency) { - var usd = GetNumberFormatInfo("USD"); + var usd = GetNumberFormatInfo("USD", false); var currencyInfo = (NumberFormatInfo)usd.Clone(); currencyInfo.CurrencySymbol = currency; return currencyInfo; @@ -145,10 +147,23 @@ namespace BTCPayServer.Services.Rates return dico.Values.ToArray(); } - public CurrencyData GetCurrencyData(string currency) + public CurrencyData GetCurrencyData(string currency, bool useFallback) { CurrencyData result; - _Currencies.TryGetValue(currency.ToUpperInvariant(), out result); + if(!_Currencies.TryGetValue(currency.ToUpperInvariant(), out result)) + { + if(useFallback) + { + var usd = GetCurrencyData("USD", false); + result = new CurrencyData() + { + Code = currency, + Crypto = true, + Name = currency, + Divisibility = usd.Divisibility + }; + } + } return result; }