Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisreimann committed Nov 23, 2023
1 parent 8ee6362 commit 01d286e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 60 deletions.
8 changes: 3 additions & 5 deletions BTCPayServer.Tests/AltcoinTests/AltcoinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,9 @@ public async Task CanUsePoSApp()
Assert.Equal(test.ExpectedSymbol,
vmview.CurrencyInfo.CurrencySymbol
.Replace("", "¥")); // Hack so JPY test pass on linux as well);
Assert.Equal(test.ExpectedDecimalSeparator, vmview.CurrencyInfo.DecimalSeparator);
Assert.Equal(test.ExpectedThousandSeparator, vmview.CurrencyInfo.ThousandSeparator);
Assert.Equal(test.ExpectedPrefixed, vmview.CurrencyInfo.Prefixed);
Assert.Equal(test.ExpectedDivisibility, vmview.CurrencyInfo.Divisibility);
Assert.Equal(test.ExpectedSymbolSpace, vmview.CurrencyInfo.SymbolSpace);
Assert.Equal(test.ExpectedDecimalSeparator, vmview.CurrencyInfo.CurrencyDecimalSeparator);
Assert.Equal(test.ExpectedThousandSeparator, vmview.CurrencyInfo.CurrencyGroupSeparator);
Assert.Equal(test.ExpectedDivisibility, vmview.CurrencyInfo.CurrencyDecimalDigits);
}

//test inventory related features
Expand Down
65 changes: 37 additions & 28 deletions BTCPayServer/Blazor/Keypad.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
@using Newtonsoft.Json.Linq
@using System.Text.RegularExpressions
@using System.Globalization
@using Newtonsoft.Json
@inject IJSRuntime JS

<form class="d-flex flex-column gap-4" @onsubmit="Submit">
<div class="d-flex flex-column gap-4">
<input type="hidden" name="amount" value="@GetTotal()">
<input type="hidden" name="posdata" value="@GetData()">
<div class="d-flex flex-column align-items-center px-4 mb-auto" @ref="_keypadTop">
<div class="fw-semibold text-muted" id="Currency">@CurrencyCode</div>
<div class="fw-bold lh-sm" id="Amount" style="font-size:@($"{FontSize}px")" @ref="_keypadAmount">@FormatCurrency(GetTotal(), false)</div>
Expand Down Expand Up @@ -56,13 +59,14 @@
}
</div>
<div id="ModeTablist" class="nav btcpay-pills align-items-center justify-content-center mb-n2 pb-1" role="tablist">
@{ var amount = GetAmount(); }
@foreach (var mode in GetModes())
{
<input id="ModeTablist-@mode" name="mode" value="@mode" type="radio" role="tab"
aria-controls="Mode-@mode" aria-selected="@(Mode == mode ? "true" : "false")"
data-bs-toggle="pill" data-bs-target="#Mode-@mode"
checked="@(Mode == mode)"
disabled="@(Mode != InputMode.Amount && GetAmount() is 0)"
disabled="@(mode != InputMode.Amount && amount == 0)"
@onclick="() => Mode = mode">
<label for="ModeTablist-@mode">@mode</label>
}
Expand All @@ -74,7 +78,7 @@
<button disabled="@(key == '+' && Mode != InputMode.Amount)" @onclick="@(e => KeyPress(key))" @onclick:preventDefault @ondblclick="@(e => DoublePress(key))" type="button" class="btn btn-secondary btn-lg" data-key="@key">@key</button>
}
</div>
<button class="btn btn-lg btn-primary mx-3" type="submit" disabled="@IsSubmitting" id="pay-button">
<button class="btn btn-lg btn-primary mx-3" type="submit" disabled="@IsSubmitting" id="pay-button" @onclick="@(e => { IsSubmitting = true; })">
@if (IsSubmitting)
{
<div class="spinner-border spinner-border-sm" role="status">
Expand All @@ -86,22 +90,22 @@
<span>Charge</span>
}
</button>
</form>
</div>

@code {
#nullable enable
[Parameter]
public string? CurrencyCode { get; set; }
[Parameter, EditorRequired]
public string CurrencyCode { get; set; }
[Parameter]
public string? CurrencySymbol { get; set; }
[Parameter]
public int CurrencyDivisibility { get; set; }
public NumberFormatInfo? CurrencyInfo { get; set; }
[Parameter]
public bool IsDiscountEnabled { get; set; }
[Parameter]
public bool IsTipEnabled { get; set; }
[Parameter]
public int[]? CustomTipPercentages { get; set; }
[Parameter]
public EventCallback<MouseEventArgs> OnClickCallback { get; set; }

private bool IsSubmitting { get; set; }

Expand All @@ -118,6 +122,8 @@

private ElementReference _keypadTop;
private ElementReference _keypadAmount;
private string? _currencySymbol;
private int _currencyDivisibility;

private InputMode Mode { get; set; } = InputMode.Amount;

Expand All @@ -128,14 +134,17 @@
public int? TipPercent { get; set; }
public decimal? Tip { get; set; }
}

private KeypadModel Model { get; set; } = new ();

private void Submit()

protected override void OnInitialized()
{
IsSubmitting = true;
base.OnInitialized();

_currencySymbol = CurrencyInfo?.CurrencySymbol ?? CurrencyCode;
_currencyDivisibility = CurrencyInfo?.CurrencyDecimalDigits ?? 0;
}

private KeypadModel Model { get; set; } = new ();

private async Task KeyPress(char key)
{
if (Mode == InputMode.Amount) {
Expand All @@ -155,9 +164,8 @@
} else if (key == '+' && lastAmount != 0) {
Model.Amounts.Add(0);
} else { // Is a digit
Model.Amounts[lastIndex] = Math.Min(ApplyKeyToValue(key, lastAmount, CurrencyDivisibility), decimal.MaxValue / 10);
Model.Amounts[lastIndex] = Math.Min(ApplyKeyToValue(key, lastAmount, _currencyDivisibility), decimal.MaxValue / 10);
}
await UpdateFontSize();
} else {
if (key == 'C') {
if (Mode == InputMode.Tip)
Expand All @@ -170,7 +178,7 @@
Model.DiscountPercent = null;
}
} else {
var divisibility = Mode == InputMode.Tip ? CurrencyDivisibility : 0;
var divisibility = Mode == InputMode.Tip ? _currencyDivisibility : 0;
if (Mode == InputMode.Tip)
{
Model.Tip = Math.Min(ApplyKeyToValue(key, Model.Tip ?? 0, divisibility), decimal.MaxValue / 10);
Expand All @@ -183,11 +191,12 @@
}
}
}
await UpdateFontSize();
}

private decimal ApplyKeyToValue(char key, decimal value, int divisibility)
{
var str = value is 0 ? "" : Formatted(value, divisibility);
var str = value is 0 ? "" : FormattedInvariant(value, divisibility);
str = (str + key).Replace(".", "");
if (divisibility > 0)
{
Expand Down Expand Up @@ -215,7 +224,7 @@
Mode = InputMode.Amount;
}

private JObject GetData()
private string GetData()
{
var data = new JObject
{
Expand All @@ -242,7 +251,7 @@
{
data["tipPercentage"] = Model.TipPercent;
}
return data;
return JsonConvert.SerializeObject(data);
}

private List<InputMode> GetModes()
Expand All @@ -262,17 +271,17 @@
{
var amount = GetAmount();
return amount > 0 && Model.DiscountPercent is > 0
? Math.Round(amount * (Model.DiscountPercent.Value / 100.0m), CurrencyDivisibility)
? Math.Round(amount * (Model.DiscountPercent.Value / 100.0m), _currencyDivisibility)
: 0;
}

private decimal GetTip()
{
if (Model.TipPercent is > 0) {
var amount = GetAmount() - GetDiscount();
return Math.Round(amount * (Model.TipPercent.Value / 100.0m), CurrencyDivisibility);
return Math.Round(amount * (Model.TipPercent.Value / 100.0m), _currencyDivisibility);
}
return Model.Tip is > 0 ? Math.Round(Model.Tip.Value, CurrencyDivisibility) : 0.0m;
return Model.Tip is > 0 ? Math.Round(Model.Tip.Value, _currencyDivisibility) : 0.0m;
}

private decimal GetTotal()
Expand All @@ -296,8 +305,8 @@
{
if (CurrencyCode is "BTC" or "SATS") return FormatCrypto(value, withSymbol);
try {
var symbol = withSymbol ? $" {CurrencySymbol ?? CurrencyCode}" : "";
return $"{Formatted(value, CurrencyDivisibility)}{symbol}";
var formatted = value.ToString("C", CurrencyInfo);
return withSymbol ? formatted : formatted.Replace(_currencySymbol, "").Trim();
}
catch (Exception)
{
Expand All @@ -306,11 +315,11 @@
}

private string FormatCrypto(decimal value, bool withSymbol) {
var symbol = withSymbol ? $" {CurrencySymbol ?? CurrencyCode}" : "";
return $"{Formatted(value, CurrencyDivisibility)}{symbol}";
var symbol = withSymbol ? $" {_currencySymbol}" : "";
return $"{FormattedInvariant(value, _currencyDivisibility)}{symbol}";
}

private string Formatted(decimal value, int divisibility) {
private string FormattedInvariant(decimal value, int divisibility) {
return string.Format(CultureInfo.InvariantCulture, $"{{0:0.{new string('0', divisibility)}}}", value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ public async Task<IActionResult> ViewPointOfSale(string appId, PosViewType? view
EnableTips = settings.EnableTips,
CurrencyCode = settings.Currency,
CurrencySymbol = numberFormatInfo.CurrencySymbol,
CurrencyInfo = new ViewPointOfSaleViewModel.CurrencyInfoData
{
CurrencySymbol = string.IsNullOrEmpty(numberFormatInfo.CurrencySymbol) ? settings.Currency : numberFormatInfo.CurrencySymbol,
Divisibility = numberFormatInfo.CurrencyDecimalDigits,
DecimalSeparator = numberFormatInfo.CurrencyDecimalSeparator,
ThousandSeparator = numberFormatInfo.NumberGroupSeparator,
Prefixed = new[] { 0, 2 }.Contains(numberFormatInfo.CurrencyPositivePattern),
SymbolSpace = new[] { 2, 3 }.Contains(numberFormatInfo.CurrencyPositivePattern)
},
CurrencyInfo = numberFormatInfo,
Items = AppService.Parse(settings.Template, false),
ButtonText = settings.ButtonText,
CustomButtonText = settings.CustomButtonText,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using BTCPayServer.JsonConverters;
using BTCPayServer.Services.Apps;
Expand Down Expand Up @@ -47,21 +48,11 @@ public class Item
[JsonExtensionData] public Dictionary<string, JToken> AdditionalData { get; set; }
}

public class CurrencyInfoData
{
public bool Prefixed { get; set; }
public string CurrencySymbol { get; set; }
public string ThousandSeparator { get; set; }
public string DecimalSeparator { get; set; }
public int Divisibility { get; set; }
public bool SymbolSpace { get; set; }
}

public string LogoFileId { get; set; }
public string CssFileId { get; set; }
public string BrandColor { get; set; }
public string StoreName { get; set; }
public CurrencyInfoData CurrencyInfo { get; set; }
public NumberFormatInfo CurrencyInfo { get; set; }
public PosViewType ViewType { get; set; }
public bool ShowCustomAmount { get; set; }
public bool ShowDiscount { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion BTCPayServer/Views/Shared/PointOfSale/Public/Cart.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
type="number"
min="0"
step="@Model.Step" />
<span>@(Model.CurrencyInfo.CurrencySymbol ?? Model.CurrencyCode)</span>
<span>@(Model.CurrencySymbol ?? Model.CurrencyCode)</span>
</div>
<button
v-for="percentage in customTipPercentages"
Expand Down
10 changes: 4 additions & 6 deletions BTCPayServer/Views/Shared/PointOfSale/Public/Light.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
}
@section PageFootContent {
<script src="~/js/blazor-interop.js" autostart="false" asp-append-version="true"></script>
<script src="~/_blazorfiles/_framework/blazor.server.js" autostart="false" asp-append-version="true"></script>
}
<div id="PosKeypad" class="public-page-wrap">
<partial name="_StatusMessage" />
Expand All @@ -23,15 +22,14 @@
<noscript>
<partial name="PointOfSale/Public/MinimalLight" model="Model" />
</noscript>
<div class="only-for-js my-auto">
<form method="post" asp-action="ViewPointOfSale" asp-route-appId="@Model.AppId" asp-antiforgery="false" class="only-for-js my-auto">
<component type="typeof(BTCPayServer.Blazor.Keypad)" render-mode="ServerPrerendered"
param-CurrencyDivisibility="Model.CurrencyInfo.Divisibility"
param-CurrencySymbol="Model.CurrencyInfo.CurrencySymbol"
param-CurrencyCode="Model.CurrencyInfo.CurrencyCode"
param-CurrencyCode="Model.CurrencyCode"
param-CurrencyInfo="Model.CurrencyInfo"
param-IsDiscountEnabled="Model.ShowDiscount"
param-IsTipEnabled="Model.EnableTips"
param-CustomTipPercentages="Model.CustomTipPercentages" />
</div>
</form>
}
<footer class="store-footer">
<a class="store-powered-by" href="https://btcpayserver.org" target="_blank" rel="noreferrer noopener">
Expand Down

0 comments on commit 01d286e

Please sign in to comment.