diff --git a/BTCPayServer/Controllers/AppsController.PointOfSale.cs b/BTCPayServer/Controllers/AppsController.PointOfSale.cs index ea89e4dd8..22f4814e4 100644 --- a/BTCPayServer/Controllers/AppsController.PointOfSale.cs +++ b/BTCPayServer/Controllers/AppsController.PointOfSale.cs @@ -1,5 +1,8 @@ -using System.Text; +using System; +using System.Linq; +using System.Text; using System.Text.Encodings.Web; +using System.Text.RegularExpressions; using System.Threading.Tasks; using BTCPayServer.Data; using BTCPayServer.Models.AppViewModels; @@ -65,8 +68,8 @@ namespace BTCPayServer.Controllers public string CustomButtonText { get; set; } = CUSTOM_BUTTON_TEXT_DEF; public const string CUSTOM_TIP_TEXT_DEF = "Do you want to leave a tip?"; public string CustomTipText { get; set; } = CUSTOM_TIP_TEXT_DEF; - public const string CUSTOM_TIP_PERCENTAGES = "15,18,20"; - public string CustomTipPercentages { get; set; } = CUSTOM_TIP_PERCENTAGES; + public static readonly int[] CUSTOM_TIP_PERCENTAGES_DEF = new int[] { 15, 18, 20 }; + public int[] CustomTipPercentages { get; set; } = CUSTOM_TIP_PERCENTAGES_DEF; public string CustomCSSLink { get; set; } @@ -90,7 +93,7 @@ namespace BTCPayServer.Controllers ButtonText = settings.ButtonText ?? PointOfSaleSettings.BUTTON_TEXT_DEF, CustomButtonText = settings.CustomButtonText ?? PointOfSaleSettings.CUSTOM_BUTTON_TEXT_DEF, CustomTipText = settings.CustomTipText ?? PointOfSaleSettings.CUSTOM_TIP_TEXT_DEF, - CustomTipPercentages = settings.CustomTipPercentages, + CustomTipPercentages = settings.CustomTipPercentages != null ? string.Join(",", settings.CustomTipPercentages) : string.Join(",", PointOfSaleSettings.CUSTOM_TIP_PERCENTAGES_DEF), CustomCSSLink = settings.CustomCSSLink }; if (HttpContext?.Request != null) @@ -161,7 +164,7 @@ namespace BTCPayServer.Controllers ButtonText = vm.ButtonText, CustomButtonText = vm.CustomButtonText, CustomTipText = vm.CustomTipText, - CustomTipPercentages = vm.CustomTipPercentages, + CustomTipPercentages = ListSplit(vm.CustomTipPercentages), CustomCSSLink = vm.CustomCSSLink }); await UpdateAppSettings(app); @@ -179,5 +182,21 @@ namespace BTCPayServer.Controllers await ctx.SaveChangesAsync(); } } + + private int[] ListSplit(string list, string separator = ",") + { + if (string.IsNullOrEmpty(list)) + { + return Array.Empty(); + } + else + { + // Remove all characters except numeric and comma + Regex charsToDestroy = new Regex(@"[^\d|\" + separator + "]"); + list = charsToDestroy.Replace(list, ""); + + return list.Split(separator, System.StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); + } + } } } diff --git a/BTCPayServer/Models/AppViewModels/ViewPointOfSaleViewModel.cs b/BTCPayServer/Models/AppViewModels/ViewPointOfSaleViewModel.cs index 7593ce637..802d74a9c 100644 --- a/BTCPayServer/Models/AppViewModels/ViewPointOfSaleViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/ViewPointOfSaleViewModel.cs @@ -45,7 +45,7 @@ namespace BTCPayServer.Models.AppViewModels public string ButtonText { get; set; } public string CustomButtonText { get; set; } public string CustomTipText { get; set; } - public string CustomTipPercentages { get; set; } + public int[] CustomTipPercentages { get; set; } public string CustomCSSLink { get; set; } } diff --git a/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml b/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml index 175e0d936..6dd637d08 100644 --- a/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml +++ b/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml @@ -5,7 +5,7 @@ @{ ViewData["Title"] = Model.Title; Layout = null; - String[] tipPercentages = Model.CustomTipPercentages != null ? Model.CustomTipPercentages.Split(',') : null; + int[] CustomTipPercentages = Model.CustomTipPercentages; } @@ -124,14 +124,12 @@
- @if (tipPercentages != null && tipPercentages.Length > 0) { - @for (int i = 0; i < tipPercentages.Length; i++) { - int percentage; - @if(int.TryParse(tipPercentages[i], out percentage)) { - - } + @if (CustomTipPercentages != null && CustomTipPercentages.Length > 0) { + @for (int i = 0; i < CustomTipPercentages.Length; i++) { + var percentage = CustomTipPercentages[i]; + } }