2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2021-12-27 13:46:31 +09:00
|
|
|
using System.Globalization;
|
2018-12-19 14:07:05 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2018-08-30 13:16:24 -05:00
|
|
|
using System.Text.Encodings.Web;
|
2018-12-19 14:07:05 +08:00
|
|
|
using System.Text.RegularExpressions;
|
2018-04-03 11:50:41 +09:00
|
|
|
using System.Threading.Tasks;
|
2022-02-21 15:46:43 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2022-03-02 18:28:12 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2018-04-03 11:50:41 +09:00
|
|
|
using BTCPayServer.Models.AppViewModels;
|
|
|
|
using BTCPayServer.Services.Apps;
|
2018-08-30 13:16:24 -05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-04-03 11:50:41 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
2022-01-07 12:32:00 +09:00
|
|
|
public partial class UIAppsController
|
2018-04-03 11:50:41 +09:00
|
|
|
{
|
2021-12-11 04:32:23 +01:00
|
|
|
[HttpGet("{appId}/settings/pos")]
|
2021-12-16 17:37:19 +01:00
|
|
|
public IActionResult UpdatePointOfSale(string appId)
|
2018-04-03 11:50:41 +09:00
|
|
|
{
|
2021-12-20 15:15:32 +01:00
|
|
|
var app = GetCurrentApp();
|
|
|
|
if (app == null)
|
2018-04-03 11:50:41 +09:00
|
|
|
return NotFound();
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
var settings = app.GetSettings<PointOfSaleSettings>();
|
2020-05-27 14:20:21 +02:00
|
|
|
settings.DefaultView = settings.EnableShoppingCart ? PosViewType.Cart : settings.DefaultView;
|
|
|
|
settings.EnableShoppingCart = false;
|
2021-04-08 15:32:42 +02:00
|
|
|
var vm = new UpdatePointOfSaleViewModel
|
2018-05-24 23:54:48 +09:00
|
|
|
{
|
2019-01-31 08:56:21 +01:00
|
|
|
Id = appId,
|
2021-12-20 15:15:32 +01:00
|
|
|
StoreId = app.StoreDataId,
|
|
|
|
StoreName = app.StoreData?.StoreName,
|
|
|
|
AppName = app.Name,
|
2018-05-24 23:54:48 +09:00
|
|
|
Title = settings.Title,
|
2020-05-26 16:48:47 +02:00
|
|
|
DefaultView = settings.DefaultView,
|
2018-05-24 23:54:48 +09:00
|
|
|
ShowCustomAmount = settings.ShowCustomAmount,
|
2019-02-25 14:11:03 +08:00
|
|
|
ShowDiscount = settings.ShowDiscount,
|
|
|
|
EnableTips = settings.EnableTips,
|
2018-05-24 23:54:48 +09:00
|
|
|
Currency = settings.Currency,
|
2018-11-16 20:39:43 -06:00
|
|
|
Template = settings.Template,
|
|
|
|
ButtonText = settings.ButtonText ?? PointOfSaleSettings.BUTTON_TEXT_DEF,
|
|
|
|
CustomButtonText = settings.CustomButtonText ?? PointOfSaleSettings.CUSTOM_BUTTON_TEXT_DEF,
|
2018-11-27 14:14:32 +08:00
|
|
|
CustomTipText = settings.CustomTipText ?? PointOfSaleSettings.CUSTOM_TIP_TEXT_DEF,
|
2018-12-19 14:07:05 +08:00
|
|
|
CustomTipPercentages = settings.CustomTipPercentages != null ? string.Join(",", settings.CustomTipPercentages) : string.Join(",", PointOfSaleSettings.CUSTOM_TIP_PERCENTAGES_DEF),
|
2019-03-29 07:51:00 +01:00
|
|
|
CustomCSSLink = settings.CustomCSSLink,
|
2019-08-19 07:13:42 +02:00
|
|
|
EmbeddedCSS = settings.EmbeddedCSS,
|
|
|
|
Description = settings.Description,
|
2019-04-11 11:08:42 +02:00
|
|
|
NotificationUrl = settings.NotificationUrl,
|
2020-10-13 15:51:28 +08:00
|
|
|
RedirectUrl = settings.RedirectUrl,
|
2021-12-20 15:15:32 +01:00
|
|
|
SearchTerm = $"storeid:{app.StoreDataId}",
|
2021-10-27 07:32:56 -07:00
|
|
|
RedirectAutomatically = settings.RedirectAutomatically.HasValue ? settings.RedirectAutomatically.Value ? "true" : "false" : "",
|
|
|
|
RequiresRefundEmail = settings.RequiresRefundEmail
|
2018-05-24 23:54:48 +09:00
|
|
|
};
|
|
|
|
if (HttpContext?.Request != null)
|
|
|
|
{
|
2021-10-25 16:54:36 +09:00
|
|
|
var appUrl = HttpContext.Request.GetAbsoluteUri($"/apps/{appId}/pos");
|
2018-05-24 23:54:48 +09:00
|
|
|
var encoder = HtmlEncoder.Default;
|
|
|
|
if (settings.ShowCustomAmount)
|
|
|
|
{
|
|
|
|
StringBuilder builder = new StringBuilder();
|
2021-12-27 13:46:31 +09:00
|
|
|
builder.AppendLine(CultureInfo.InvariantCulture, $"<form method=\"POST\" action=\"{encoder.Encode(appUrl)}\">");
|
2018-05-24 23:54:48 +09:00
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"amount\" value=\"100\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"email\" value=\"customer@example.com\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"orderId\" value=\"CustomOrderId\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"notificationUrl\" value=\"https://example.com/callbacks\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"redirectUrl\" value=\"https://example.com/thanksyou\" />");
|
|
|
|
builder.AppendLine($" <button type=\"submit\">Buy now</button>");
|
|
|
|
builder.AppendLine($"</form>");
|
|
|
|
vm.Example1 = builder.ToString();
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2021-12-11 04:32:23 +01:00
|
|
|
var items = _appService.Parse(settings.Template, settings.Currency);
|
2018-05-24 23:54:48 +09:00
|
|
|
var builder = new StringBuilder();
|
2021-12-27 13:46:31 +09:00
|
|
|
builder.AppendLine(CultureInfo.InvariantCulture, $"<form method=\"POST\" action=\"{encoder.Encode(appUrl)}\">");
|
2018-05-24 23:54:48 +09:00
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"email\" value=\"customer@example.com\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"orderId\" value=\"CustomOrderId\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"notificationUrl\" value=\"https://example.com/callbacks\" />");
|
|
|
|
builder.AppendLine($" <input type=\"hidden\" name=\"redirectUrl\" value=\"https://example.com/thanksyou\" />");
|
2021-12-27 13:46:31 +09:00
|
|
|
builder.AppendLine(CultureInfo.InvariantCulture, $" <button type=\"submit\" name=\"choiceKey\" value=\"{items[0].Id}\">Buy now</button>");
|
2018-05-24 23:54:48 +09:00
|
|
|
builder.AppendLine($"</form>");
|
|
|
|
vm.Example2 = builder.ToString();
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
vm.InvoiceUrl = appUrl + "invoices/SkdsDghkdP3D3qkj7bLq3";
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:35:01 +09:00
|
|
|
vm.ExampleCallback = "{\n \"id\":\"SkdsDghkdP3D3qkj7bLq3\",\n \"url\":\"https://btcpay.example.com/invoice?id=SkdsDghkdP3D3qkj7bLq3\",\n \"status\":\"paid\",\n \"price\":10,\n \"currency\":\"EUR\",\n \"invoiceTime\":1520373130312,\n \"expirationTime\":1520374030312,\n \"currentTime\":1520373179327,\n \"exceptionStatus\":false,\n \"buyerFields\":{\n \"buyerEmail\":\"customer@example.com\",\n \"buyerNotify\":false\n },\n \"paymentSubtotals\": {\n \"BTC\":114700\n },\n \"paymentTotals\": {\n \"BTC\":118400\n },\n \"transactionCurrency\": \"BTC\",\n \"amountPaid\": \"1025900\",\n \"exchangeRates\": {\n \"BTC\": {\n \"EUR\": 8721.690715789999,\n \"USD\": 10817.99\n }\n }\n}";
|
2018-05-24 23:54:48 +09:00
|
|
|
return View(vm);
|
2018-04-03 11:50:41 +09:00
|
|
|
}
|
2021-12-11 04:32:23 +01:00
|
|
|
|
|
|
|
[HttpPost("{appId}/settings/pos")]
|
2018-04-03 11:50:41 +09:00
|
|
|
public async Task<IActionResult> UpdatePointOfSale(string appId, UpdatePointOfSaleViewModel vm)
|
2021-12-20 15:15:32 +01:00
|
|
|
{
|
|
|
|
var app = GetCurrentApp();
|
|
|
|
if (app == null)
|
2021-10-25 16:54:36 +09:00
|
|
|
return NotFound();
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-03-29 22:26:33 -07:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
return View(vm);
|
2021-12-16 17:37:19 +01:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
vm.Currency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, vm.Currency);
|
2019-02-17 16:53:41 +09:00
|
|
|
if (_currencies.GetCurrencyData(vm.Currency, false) == null)
|
2018-04-03 11:50:41 +09:00
|
|
|
ModelState.AddModelError(nameof(vm.Currency), "Invalid currency");
|
|
|
|
try
|
|
|
|
{
|
2021-12-11 04:32:23 +01:00
|
|
|
vm.Template = _appService.SerializeTemplate(_appService.Parse(vm.Template, vm.Currency));
|
2018-04-03 11:50:41 +09:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(vm.Template), "Invalid template");
|
|
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(vm);
|
|
|
|
}
|
2021-10-29 06:29:02 -04:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
app.Name = vm.AppName;
|
|
|
|
app.SetSettings(new PointOfSaleSettings
|
2018-04-03 11:50:41 +09:00
|
|
|
{
|
|
|
|
Title = vm.Title,
|
2020-05-26 16:48:47 +02:00
|
|
|
DefaultView = vm.DefaultView,
|
2018-04-26 22:09:18 +09:00
|
|
|
ShowCustomAmount = vm.ShowCustomAmount,
|
2019-02-25 14:11:03 +08:00
|
|
|
ShowDiscount = vm.ShowDiscount,
|
|
|
|
EnableTips = vm.EnableTips,
|
2021-10-25 16:54:36 +09:00
|
|
|
Currency = vm.Currency,
|
2018-11-16 20:39:43 -06:00
|
|
|
Template = vm.Template,
|
|
|
|
ButtonText = vm.ButtonText,
|
|
|
|
CustomButtonText = vm.CustomButtonText,
|
2018-11-27 14:14:32 +08:00
|
|
|
CustomTipText = vm.CustomTipText,
|
2018-12-19 14:07:05 +08:00
|
|
|
CustomTipPercentages = ListSplit(vm.CustomTipPercentages),
|
2019-04-11 09:14:39 +02:00
|
|
|
CustomCSSLink = vm.CustomCSSLink,
|
|
|
|
NotificationUrl = vm.NotificationUrl,
|
2020-10-13 15:51:28 +08:00
|
|
|
RedirectUrl = vm.RedirectUrl,
|
2019-08-19 07:13:42 +02:00
|
|
|
Description = vm.Description,
|
|
|
|
EmbeddedCSS = vm.EmbeddedCSS,
|
2021-10-27 07:32:56 -07:00
|
|
|
RedirectAutomatically = string.IsNullOrEmpty(vm.RedirectAutomatically) ? (bool?)null : bool.Parse(vm.RedirectAutomatically),
|
|
|
|
RequiresRefundEmail = vm.RequiresRefundEmail,
|
2018-04-03 11:50:41 +09:00
|
|
|
});
|
2021-12-20 15:15:32 +01:00
|
|
|
await _appService.UpdateOrCreateApp(app);
|
2019-10-31 12:29:59 +09:00
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "App updated";
|
2019-08-01 02:55:41 -04:00
|
|
|
return RedirectToAction(nameof(UpdatePointOfSale), new { appId });
|
2018-04-03 11:50:41 +09:00
|
|
|
}
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2018-12-19 14:07:05 +08:00
|
|
|
private int[] ListSplit(string list, string separator = ",")
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(list))
|
|
|
|
{
|
|
|
|
return Array.Empty<int>();
|
2020-01-23 20:19:24 -06:00
|
|
|
}
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-12-16 17:37:19 +01:00
|
|
|
// Remove all characters except numeric and comma
|
|
|
|
Regex charsToDestroy = new Regex(@"[^\d|\" + separator + "]");
|
|
|
|
list = charsToDestroy.Replace(list, "");
|
2018-12-19 14:07:05 +08:00
|
|
|
|
2021-12-16 17:37:19 +01:00
|
|
|
return list.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
|
2018-12-19 14:07:05 +08:00
|
|
|
}
|
2018-04-03 11:50:41 +09:00
|
|
|
}
|
|
|
|
}
|