Merge pull request #428 from mariodian/fix-pos-cart-currency

Fix currency format for total amount
This commit is contained in:
Nicolas Dorier 2018-12-01 14:09:01 +09:00 committed by GitHub
commit a653421514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 206 additions and 14 deletions

View File

@ -47,6 +47,7 @@ namespace BTCPayServer.Controllers
Step = step.ToString(CultureInfo.InvariantCulture),
EnableShoppingCart = settings.EnableShoppingCart,
ShowCustomAmount = settings.ShowCustomAmount,
CurrencyCode = currency.Code,
CurrencySymbol = currency.Symbol,
Items = _AppsHelper.Parse(settings.Template, settings.Currency),
ButtonText = settings.ButtonText,

View File

@ -27,6 +27,7 @@ namespace BTCPayServer.Models.AppViewModels
public string Step { get; set; }
public string Title { get; set; }
public Item[] Items { get; set; }
public string CurrencyCode { get; set; }
public string CurrencySymbol { get; set; }
public string ButtonText { get; set; }

View File

@ -115,7 +115,7 @@ Cart.prototype.getTotal = function(plain) {
}
Cart.prototype.updateTotal = function() {
$('#js-cart-total').text(this.formatCurrency(this.getTotal()));
$('#js-cart-total').text(this.formatCurrency(this.getTotal(), srvModel.currencyCode, srvModel.currencySymbol));
}
Cart.prototype.updateAmount = function() {
@ -135,7 +135,7 @@ Cart.prototype.escape = function(input) {
Cart.prototype.listItems = function() {
var $table = $('#js-cart-list').find('tbody'),
self = this,
list = []
list = [],
tableTemplate = '';
if (this.content.length > 0) {
@ -147,8 +147,9 @@ Cart.prototype.listItems = function() {
image = this.escape(item.image),
count = this.escape(item.count),
price = this.escape(item.price.formatted),
currencySymbol = this.escape(srvModel.currencySymbol),
total = this.escape(this.formatCurrency(this.getTotal(), srvModel.currencyCode, srvModel.currencySymbol)),
step = this.escape(srvModel.step),
tip = this.escape(this.tip || ''),
customTipText = this.escape(srvModel.customTipText);
tableTemplate = '<tr data-id="' + id + '">' +
@ -163,17 +164,17 @@ Cart.prototype.listItems = function() {
list.push($(tableTemplate));
}
tableTemplate = '<tr><td colspan="4"><div class="row"><div class="col-sm-8 py-2">' + customTipText + '</div><div class="col-sm-4">' +
tableTemplate = '<tr><td colspan="4"><div class="row"><div class="col-sm-7 py-2">' + customTipText + '</div><div class="col-sm-5">' +
'<div class="input-group">' +
'<div class="input-group-prepend">' +
'<span class="input-group-text">' + currencySymbol + '</span>' +
'<span class="input-group-text"><i class="fa fa-money"></i></span>' +
'</div>' +
'<input class="js-cart-tip form-control" type="number" min="0" step="' + step + '" name="tip" placeholder="Amount">' +
'<input class="js-cart-tip form-control" type="number" min="0" step="' + step + '" value="' + tip + '" name="tip" placeholder="Amount">' +
'</div>' +
'</div></div></td></tr>';
list.push($(tableTemplate));
tableTemplate = '<tr class="bg-light h4"><td colspan="2">Total</td><td colspan="2" align="right"><span id="js-cart-total">' + this.formatCurrency(this.getTotal()) + '</span></td></tr>';
tableTemplate = '<tr class="bg-light h4"><td colspan="1">Total</td><td colspan="3" align="right"><span id="js-cart-total">' + total + '</span></td></tr>';
list.push($(tableTemplate));
// Add the list to DOM
@ -239,16 +240,204 @@ Cart.prototype.emptyList = function() {
$table.html('<tr><td colspan="4">The cart is empty.</td></tr>');
}
/* Get the currency symbol from an existing amount and use it with the new amount*/
Cart.prototype.formatCurrency = function(amount, example) {
var regex = /([0-9.]+)/gm;
Cart.prototype.formatCurrency = function(amount, currency, symbol) {
var amt = '',
thousandsSep = '',
decimalSep = ''
prefix = '',
postfix = '',
currencyName = currency.toLowerCase();
// Get the first item's formated price
if (typeof example == 'undefined' && typeof srvModel != 'undefined') {
example = srvModel.items[0].price.formatted;
// Currency symbols
switch (currencyName) {
case 'usd':
case 'aud':
case 'cad':
case 'clp':
case 'mxn':
case 'nzd':
case 'sgd':
prefix = '$';
break;
case 'eur':
postfix = ' €';
break;
case 'chf':
prefix = 'CHF ';
break;
case 'gbp':
prefix = '£';
break;
case 'jpy':
case 'cny':
prefix = '¥';
break;
case 'hkd':
prefix = 'HK$';
break;
case 'ars':
case 'cop':
prefix = '$ ';
break;
case 'brl':
prefix = 'R$ ';
break;
case 'bdt':
postfix = '৳';
break;
case 'czk':
postfix = ' Kč';
break;
case 'dkk':
postfix = ' kr.';
break;
case 'huf':
postfix = ' Ft';
break;
case 'hrk':
postfix = ' HRK';
break;
case 'ils':
postfix = ' ₪';
break;
case 'inr':
prefix = '₹ ';
break;
case 'isk':
postfix = ' ISK';
break;
case 'kzt':
postfix = ' ₸';
break;
case 'myr':
prefix = 'RM';
break;
case 'ngn':
prefix = '₦';
break;
case 'nok':
prefix = 'kr ';
break;
case 'php':
prefix = '₱';
break;
case 'pen':
prefix = 'S/';
break;
case 'pln':
postfix = ' zł';
break;
case 'ron':
postfix = ' RON';
break;
case 'rub':
postifx = ' ₽';
break;
case 'sek':
postfix = ' kr';
break;
case 'aed':
prefix = 'د.إ. ';
break;
case 'egp':
prefix = 'ج.م.';
break;
case 'irr':
prefix = 'ریال';
break;
case 'pkr':
prefix = ' ر';
break;
case 'sar':
prefix = 'ر.س.';
break;
case 'try':
prefix = '₺';
break;
case 'uah':
postfix = ' ₴';
break;
case 'vnd':
postfix = ' ₫';
break;
case 'zar':
prefix = 'R';
break;
default:
prefix = symbol || currency;
}
// Currency separators
switch (currencyName) {
case 'eur':
case 'czk':
case 'huf':
case 'kzt':
case 'nok':
case 'pln':
case 'rub':
case 'sek':
case 'uah':
case 'zar':
thousandsSep = ' ';
decimalSep = ',';
break;
case 'chf':
thousandsSep = '';
decimalSep = '.';
break;
case 'cop':
case 'clp':
case 'idr':
case 'isk':
case 'vnd':
thousandsSep = '.';
break;
case 'jpy':
thousandsSep = ',';
break;
case 'ars':
case 'brl':
case 'dkk':
case 'hrk':
case 'ron':
case 'try':
thousandsSep = '.';
decimalSep = ',';
break;
case 'irr':
case 'pkr':
thousandsSep = '٬';
break;
case 'egp':
case 'sar':
thousandsSep = '٬';
decimalSep = '٫';
break;
default:
thousandsSep = ',';
decimalSep = '.';
}
return example.replace(regex, amount.toFixed(2));
if (decimalSep !== '') {
// Replace decimal separator
amt = amount.toFixed(2).replace(/.(\d{2})$/g, decimalSep + '\$1');
} else {
// No decimal separator
amt = amount.toString();
}
// Add currency sign and thousands separator
amt = prefix + amt.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSep) + postfix;
return amt;
}
Cart.prototype.toCents = function(num) {
@ -279,4 +468,5 @@ Cart.prototype.destroy = function() {
this.content = [];
this.items = 0;
this.totalAmount = 0;
this.tip = 0;
}