Move code into CurrencyNameTable

This commit is contained in:
nicolas.dorier 2017-10-27 18:58:43 +09:00
parent b5196aa541
commit ca85095273
3 changed files with 42 additions and 22 deletions

View file

@ -92,27 +92,6 @@ namespace BTCPayServer.Controllers
return View(model);
}
static Dictionary<string, CultureInfo> _CurrencyProviders = new Dictionary<string, CultureInfo>();
private IFormatProvider GetCurrencyProvider(string currency)
{
lock (_CurrencyProviders)
{
if (_CurrencyProviders.Count == 0)
{
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => !c.IsNeutralCulture))
{
try
{
_CurrencyProviders.TryAdd(new RegionInfo(culture.LCID).ISOCurrencySymbol, culture);
}
catch { }
}
}
return _CurrencyProviders.TryGet(currency);
}
}
[HttpGet]
[Route("i/{invoiceId}")]
[Route("invoice")]
@ -140,6 +119,7 @@ namespace BTCPayServer.Controllers
var store = await _StoreRepository.FindStore(invoice.StoreId);
var dto = invoice.EntityToDTO();
var cryptoFormat = _CurrencyNameTable.GetCurrencyProvider("BTC");
var model = new PaymentModel()
{
ServerUrl = HttpContext.Request.GetAbsoluteRoot(),
@ -153,7 +133,7 @@ namespace BTCPayServer.Controllers
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
ItemDesc = invoice.ProductInformation.ItemDesc,
Rate = invoice.Rate.ToString("C", GetCurrencyProvider(invoice.ProductInformation.Currency)),
Rate = invoice.Rate.ToString("C", _CurrencyNameTable.GetCurrencyProvider(invoice.ProductInformation.Currency)),
MerchantRefLink = invoice.RedirectURL ?? "/",
StoreName = store.StoreName,
TxFees = invoice.TxFee.ToString(),

View file

@ -51,11 +51,13 @@ namespace BTCPayServer.Controllers
Network _Network;
UserManager<ApplicationUser> _UserManager;
IFeeProvider _FeeProvider;
private CurrencyNameTable _CurrencyNameTable;
ExplorerClient _Explorer;
public InvoiceController(
Network network,
InvoiceRepository invoiceRepository,
CurrencyNameTable currencyNameTable,
UserManager<ApplicationUser> userManager,
BTCPayWallet wallet,
IRateProvider rateProvider,
@ -64,6 +66,7 @@ namespace BTCPayServer.Controllers
ExplorerClient explorerClient,
IFeeProvider feeProvider)
{
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
_Explorer = explorerClient ?? throw new ArgumentNullException(nameof(explorerClient));
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
_Network = network ?? throw new ArgumentNullException(nameof(network));

View file

@ -1,9 +1,11 @@
using System;
using NBitcoin;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Globalization;
namespace BTCPayServer.Services.Rates
{
@ -37,6 +39,41 @@ namespace BTCPayServer.Services.Rates
_Currencies = LoadCurrency().ToDictionary(k => k.Code);
}
static Dictionary<string, IFormatProvider> _CurrencyProviders = new Dictionary<string, IFormatProvider>();
public IFormatProvider GetCurrencyProvider(string currency)
{
lock (_CurrencyProviders)
{
if (_CurrencyProviders.Count == 0)
{
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => !c.IsNeutralCulture))
{
try
{
_CurrencyProviders.TryAdd(new RegionInfo(culture.LCID).ISOCurrencySymbol, culture);
}
catch { }
}
AddCurrency(_CurrencyProviders, "BTC", 8, "BTC");
}
return _CurrencyProviders.TryGet(currency);
}
}
private void AddCurrency(Dictionary<string, IFormatProvider> currencyProviders, string code, int divisibility, string symbol)
{
var culture = new CultureInfo("en-US");
var number = new NumberFormatInfo();
number.CurrencyDecimalDigits = divisibility;
number.CurrencySymbol = symbol;
number.CurrencyDecimalSeparator = culture.NumberFormat.CurrencyDecimalSeparator;
number.CurrencyGroupSeparator = culture.NumberFormat.CurrencyGroupSeparator;
number.CurrencyGroupSizes = culture.NumberFormat.CurrencyGroupSizes;
number.CurrencyNegativePattern = 8;
number.CurrencyPositivePattern = 3;
number.NegativeSign = culture.NumberFormat.NegativeSign;
currencyProviders.TryAdd(code, number);
}
Dictionary<string, CurrencyData> _Currencies;