mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Move code into CurrencyNameTable
This commit is contained in:
@@ -92,27 +92,6 @@ namespace BTCPayServer.Controllers
|
|||||||
return View(model);
|
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]
|
[HttpGet]
|
||||||
[Route("i/{invoiceId}")]
|
[Route("i/{invoiceId}")]
|
||||||
[Route("invoice")]
|
[Route("invoice")]
|
||||||
@@ -140,6 +119,7 @@ namespace BTCPayServer.Controllers
|
|||||||
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
||||||
var dto = invoice.EntityToDTO();
|
var dto = invoice.EntityToDTO();
|
||||||
|
|
||||||
|
var cryptoFormat = _CurrencyNameTable.GetCurrencyProvider("BTC");
|
||||||
var model = new PaymentModel()
|
var model = new PaymentModel()
|
||||||
{
|
{
|
||||||
ServerUrl = HttpContext.Request.GetAbsoluteRoot(),
|
ServerUrl = HttpContext.Request.GetAbsoluteRoot(),
|
||||||
@@ -153,7 +133,7 @@ namespace BTCPayServer.Controllers
|
|||||||
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
|
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
|
||||||
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
|
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
|
||||||
ItemDesc = invoice.ProductInformation.ItemDesc,
|
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 ?? "/",
|
MerchantRefLink = invoice.RedirectURL ?? "/",
|
||||||
StoreName = store.StoreName,
|
StoreName = store.StoreName,
|
||||||
TxFees = invoice.TxFee.ToString(),
|
TxFees = invoice.TxFee.ToString(),
|
||||||
|
|||||||
@@ -51,11 +51,13 @@ namespace BTCPayServer.Controllers
|
|||||||
Network _Network;
|
Network _Network;
|
||||||
UserManager<ApplicationUser> _UserManager;
|
UserManager<ApplicationUser> _UserManager;
|
||||||
IFeeProvider _FeeProvider;
|
IFeeProvider _FeeProvider;
|
||||||
|
private CurrencyNameTable _CurrencyNameTable;
|
||||||
ExplorerClient _Explorer;
|
ExplorerClient _Explorer;
|
||||||
|
|
||||||
public InvoiceController(
|
public InvoiceController(
|
||||||
Network network,
|
Network network,
|
||||||
InvoiceRepository invoiceRepository,
|
InvoiceRepository invoiceRepository,
|
||||||
|
CurrencyNameTable currencyNameTable,
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
BTCPayWallet wallet,
|
BTCPayWallet wallet,
|
||||||
IRateProvider rateProvider,
|
IRateProvider rateProvider,
|
||||||
@@ -64,6 +66,7 @@ namespace BTCPayServer.Controllers
|
|||||||
ExplorerClient explorerClient,
|
ExplorerClient explorerClient,
|
||||||
IFeeProvider feeProvider)
|
IFeeProvider feeProvider)
|
||||||
{
|
{
|
||||||
|
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
|
||||||
_Explorer = explorerClient ?? throw new ArgumentNullException(nameof(explorerClient));
|
_Explorer = explorerClient ?? throw new ArgumentNullException(nameof(explorerClient));
|
||||||
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
|
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
|
||||||
_Network = network ?? throw new ArgumentNullException(nameof(network));
|
_Network = network ?? throw new ArgumentNullException(nameof(network));
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using NBitcoin;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace BTCPayServer.Services.Rates
|
namespace BTCPayServer.Services.Rates
|
||||||
{
|
{
|
||||||
@@ -37,6 +39,41 @@ namespace BTCPayServer.Services.Rates
|
|||||||
_Currencies = LoadCurrency().ToDictionary(k => k.Code);
|
_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;
|
Dictionary<string, CurrencyData> _Currencies;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user