mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
CurrencyNameTable can use fallback
This commit is contained in:
@@ -65,7 +65,7 @@ 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)
|
||||||
{
|
{
|
||||||
if (_Currencies.GetCurrencyData(vm.Currency) == null)
|
if (_Currencies.GetCurrencyData(vm.Currency, false) == null)
|
||||||
ModelState.AddModelError(nameof(vm.Currency), "Invalid currency");
|
ModelState.AddModelError(nameof(vm.Currency), "Invalid currency");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -102,7 +102,7 @@ namespace BTCPayServer.Controllers
|
|||||||
if (app == null)
|
if (app == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
var settings = app.GetSettings<PointOfSaleSettings>();
|
var settings = app.GetSettings<PointOfSaleSettings>();
|
||||||
var currency = _Currencies.GetCurrencyData(settings.Currency);
|
var currency = _Currencies.GetCurrencyData(settings.Currency, false);
|
||||||
double step = currency == null ? 1 : Math.Pow(10, -(currency.Divisibility));
|
double step = currency == null ? 1 : Math.Pow(10, -(currency.Divisibility));
|
||||||
return View(new ViewPointOfSaleViewModel()
|
return View(new ViewPointOfSaleViewModel()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -305,8 +305,8 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
public static string FormatCurrency(decimal price, string currency, CurrencyNameTable currencies)
|
public static string FormatCurrency(decimal price, string currency, CurrencyNameTable currencies)
|
||||||
{
|
{
|
||||||
var provider = currencies.GetNumberFormatInfo(currency);
|
var provider = currencies.GetNumberFormatInfo(currency, true);
|
||||||
var currencyData = currencies.GetCurrencyData(currency);
|
var currencyData = currencies.GetCurrencyData(currency, true);
|
||||||
var divisibility = currencyData.Divisibility;
|
var divisibility = currencyData.Divisibility;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ namespace BTCPayServer.Controllers
|
|||||||
CryptoCode = r.Pair.Left,
|
CryptoCode = r.Pair.Left,
|
||||||
Code = r.Pair.Right,
|
Code = r.Pair.Right,
|
||||||
CurrencyPair = r.Pair.ToString(),
|
CurrencyPair = r.Pair.ToString(),
|
||||||
Name = _CurrencyNameTable.GetCurrencyData(r.Pair.Right)?.Name,
|
Name = _CurrencyNameTable.GetCurrencyData(r.Pair.Right, true).Name,
|
||||||
Value = r.Value.Value
|
Value = r.Value.Value
|
||||||
}).Where(n => n.Name != null).ToArray());
|
}).Where(n => n.Name != null).ToArray());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace BTCPayServer
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
var currency = match.Groups.Last().Value.ToUpperInvariant();
|
var currency = match.Groups.Last().Value.ToUpperInvariant();
|
||||||
var currencyData = _CurrencyTable.GetCurrencyData(currency);
|
var currencyData = _CurrencyTable.GetCurrencyData(currency, false);
|
||||||
if (currencyData == null)
|
if (currencyData == null)
|
||||||
return false;
|
return false;
|
||||||
v = Math.Round(v, currencyData.Divisibility);
|
v = Math.Round(v, currencyData.Divisibility);
|
||||||
|
|||||||
@@ -42,19 +42,21 @@ namespace BTCPayServer.Services.Rates
|
|||||||
|
|
||||||
static Dictionary<string, IFormatProvider> _CurrencyProviders = new Dictionary<string, IFormatProvider>();
|
static Dictionary<string, IFormatProvider> _CurrencyProviders = new Dictionary<string, IFormatProvider>();
|
||||||
|
|
||||||
public NumberFormatInfo GetNumberFormatInfo(string currency)
|
public NumberFormatInfo GetNumberFormatInfo(string currency, bool useFallback)
|
||||||
{
|
{
|
||||||
var data = GetCurrencyProvider(currency);
|
var data = GetCurrencyProvider(currency);
|
||||||
if (data is NumberFormatInfo nfi)
|
if (data is NumberFormatInfo nfi)
|
||||||
return nfi;
|
return nfi;
|
||||||
if (data is CultureInfo ci)
|
if (data is CultureInfo ci)
|
||||||
return ci.NumberFormat;
|
return ci.NumberFormat;
|
||||||
|
if (!useFallback)
|
||||||
|
return null;
|
||||||
return CreateFallbackCurrencyFormatInfo(currency);
|
return CreateFallbackCurrencyFormatInfo(currency);
|
||||||
}
|
}
|
||||||
|
|
||||||
private NumberFormatInfo CreateFallbackCurrencyFormatInfo(string currency)
|
private NumberFormatInfo CreateFallbackCurrencyFormatInfo(string currency)
|
||||||
{
|
{
|
||||||
var usd = GetNumberFormatInfo("USD");
|
var usd = GetNumberFormatInfo("USD", false);
|
||||||
var currencyInfo = (NumberFormatInfo)usd.Clone();
|
var currencyInfo = (NumberFormatInfo)usd.Clone();
|
||||||
currencyInfo.CurrencySymbol = currency;
|
currencyInfo.CurrencySymbol = currency;
|
||||||
return currencyInfo;
|
return currencyInfo;
|
||||||
@@ -145,10 +147,23 @@ namespace BTCPayServer.Services.Rates
|
|||||||
return dico.Values.ToArray();
|
return dico.Values.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CurrencyData GetCurrencyData(string currency)
|
public CurrencyData GetCurrencyData(string currency, bool useFallback)
|
||||||
{
|
{
|
||||||
CurrencyData result;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user