From 1cf17872abb8f219037bb994304741454c6ac9d4 Mon Sep 17 00:00:00 2001 From: Kukks Date: Thu, 11 Apr 2019 11:08:42 +0200 Subject: [PATCH] Allow POS to redirect invoices automatically after paid closes #730 --- BTCPayServer/Controllers/AppsController.PointOfSale.cs | 7 +++++-- BTCPayServer/Controllers/AppsPublicController.cs | 3 ++- BTCPayServer/Controllers/InvoiceController.UI.cs | 1 + BTCPayServer/Controllers/InvoiceController.cs | 2 ++ .../Models/AppViewModels/UpdatePointOfSaleViewModel.cs | 3 +++ BTCPayServer/Models/CreateInvoiceRequest.cs | 3 +++ BTCPayServer/Models/InvoicingModels/PaymentModel.cs | 1 + BTCPayServer/Services/Invoices/InvoiceEntity.cs | 6 ++++++ BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml | 10 ++++++++++ BTCPayServer/wwwroot/checkout/js/core.js | 6 ++++++ 10 files changed, 39 insertions(+), 3 deletions(-) diff --git a/BTCPayServer/Controllers/AppsController.PointOfSale.cs b/BTCPayServer/Controllers/AppsController.PointOfSale.cs index 103e0d96e..a2dec2477 100644 --- a/BTCPayServer/Controllers/AppsController.PointOfSale.cs +++ b/BTCPayServer/Controllers/AppsController.PointOfSale.cs @@ -79,6 +79,7 @@ namespace BTCPayServer.Controllers public string CustomCSSLink { get; set; } public string NotificationEmail { get; set; } public string NotificationUrl { get; set; } + public bool RedirectAutomatically { get; set; } } [HttpGet] @@ -105,7 +106,8 @@ namespace BTCPayServer.Controllers CustomTipPercentages = settings.CustomTipPercentages != null ? string.Join(",", settings.CustomTipPercentages) : string.Join(",", PointOfSaleSettings.CUSTOM_TIP_PERCENTAGES_DEF), CustomCSSLink = settings.CustomCSSLink, NotificationEmail = settings.NotificationEmail, - NotificationUrl = settings.NotificationUrl + NotificationUrl = settings.NotificationUrl, + RedirectAutomatically = settings.RedirectAutomatically }; if (HttpContext?.Request != null) { @@ -180,7 +182,8 @@ namespace BTCPayServer.Controllers CustomTipPercentages = ListSplit(vm.CustomTipPercentages), CustomCSSLink = vm.CustomCSSLink, NotificationUrl = vm.NotificationUrl, - NotificationEmail = vm.NotificationEmail + NotificationEmail = vm.NotificationEmail, + RedirectAutomatically = vm.RedirectAutomatically }); await UpdateAppSettings(app); diff --git a/BTCPayServer/Controllers/AppsPublicController.cs b/BTCPayServer/Controllers/AppsPublicController.cs index 4512c82bb..819c7ed1f 100644 --- a/BTCPayServer/Controllers/AppsPublicController.cs +++ b/BTCPayServer/Controllers/AppsPublicController.cs @@ -270,7 +270,8 @@ namespace BTCPayServer.Controllers RedirectURL = redirectUrl ?? Request.GetDisplayUrl(), FullNotifications = true, ExtendedNotifications = true, - PosData = string.IsNullOrEmpty(posData) ? null : posData + PosData = string.IsNullOrEmpty(posData) ? null : posData, + RedirectAutomatically = settings.RedirectAutomatically }, store, HttpContext.Request.GetAbsoluteRoot(), new List() {AppService.GetAppInternalTag(appId)}, cancellationToken); diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 9305fcab8..1343d5f9b 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -314,6 +314,7 @@ namespace BTCPayServer.Controllers ItemDesc = invoice.ProductInformation.ItemDesc, Rate = ExchangeRate(paymentMethod), MerchantRefLink = invoice.RedirectURL ?? "/", + RedirectAutomatically = invoice.RedirectAutomatically, StoreName = store.StoreName, InvoiceBitcoinUrl = paymentMethodId.PaymentType == PaymentTypes.BTCLike ? cryptoInfo.PaymentUrls.BIP21 : paymentMethodId.PaymentType == PaymentTypes.LightningLike ? cryptoInfo.PaymentUrls.BOLT11 : diff --git a/BTCPayServer/Controllers/InvoiceController.cs b/BTCPayServer/Controllers/InvoiceController.cs index 2e97e5101..32befb8db 100644 --- a/BTCPayServer/Controllers/InvoiceController.cs +++ b/BTCPayServer/Controllers/InvoiceController.cs @@ -125,6 +125,8 @@ namespace BTCPayServer.Controllers if (!Uri.IsWellFormedUriString(entity.RedirectURL, UriKind.Absolute)) entity.RedirectURL = null; + entity.RedirectAutomatically = invoice.RedirectAutomatically; + entity.Status = InvoiceStatus.New; entity.SpeedPolicy = ParseSpeedPolicy(invoice.TransactionSpeed, store.SpeedPolicy); diff --git a/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs b/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs index 2085d93e0..ecf5b4af4 100644 --- a/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/UpdatePointOfSaleViewModel.cs @@ -54,5 +54,8 @@ namespace BTCPayServer.Models.AppViewModels public string CustomCSSLink { get; set; } public string Id { get; set; } + + [Display(Name = "Redirect invoice to redirect url automatically after paid")] + public bool RedirectAutomatically { get; set; } } } diff --git a/BTCPayServer/Models/CreateInvoiceRequest.cs b/BTCPayServer/Models/CreateInvoiceRequest.cs index 9e818c7d8..268196b32 100644 --- a/BTCPayServer/Models/CreateInvoiceRequest.cs +++ b/BTCPayServer/Models/CreateInvoiceRequest.cs @@ -79,5 +79,8 @@ namespace BTCPayServer.Models public string Guid { get; set; } [JsonProperty(PropertyName = "token", DefaultValueHandling = DefaultValueHandling.Ignore)] public string Token { get; set; } + + [JsonProperty(PropertyName = "redirectAutomatically", DefaultValueHandling = DefaultValueHandling.Ignore)] + public bool RedirectAutomatically { get; set; } } } diff --git a/BTCPayServer/Models/InvoicingModels/PaymentModel.cs b/BTCPayServer/Models/InvoicingModels/PaymentModel.cs index e324da2a2..67c1783a2 100644 --- a/BTCPayServer/Models/InvoicingModels/PaymentModel.cs +++ b/BTCPayServer/Models/InvoicingModels/PaymentModel.cs @@ -67,5 +67,6 @@ namespace BTCPayServer.Models.InvoicingModels public string CoinSwitchMerchantId { get; set; } public string RootPath { get; set; } public decimal CoinSwitchAmountMarkupPercentage { get; set; } + public bool RedirectAutomatically { get; set; } } } diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index b6de4d301..49ee3bdf7 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -298,6 +298,12 @@ namespace BTCPayServer.Services.Invoices get; set; } + + public bool RedirectAutomatically + { + get; + set; + } [Obsolete("Use GetPaymentMethod(network).GetTxFee() instead")] public Money TxFee diff --git a/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml b/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml index a2bd1cf62..05e80f8bb 100644 --- a/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml +++ b/BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml @@ -52,18 +52,22 @@
+
+
+
+
* @@ -114,6 +118,12 @@
+ +
+ + + +
diff --git a/BTCPayServer/wwwroot/checkout/js/core.js b/BTCPayServer/wwwroot/checkout/js/core.js index 14fc43aad..23cb6fcd0 100644 --- a/BTCPayServer/wwwroot/checkout/js/core.js +++ b/BTCPayServer/wwwroot/checkout/js/core.js @@ -46,6 +46,12 @@ function onDataCallback(jsonData) { resetTabsSlider(); $("#paid").addClass("active"); + if (!jsonData.isModal && jsonData.redirectAutomatically && jsonData.merchantRefLink) { + $(".payment__spinner").show(); + setTimeout(function () { + window.location = jsonData.merchantRefLink; + }, 2000); + } } if (newStatus === "expired" || newStatus === "invalid") { //TODO: different state if the invoice is invalid (failed to confirm after timeout)