From f5d366cf7fdcf3e351f9b001853629901bfeb085 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 22 Feb 2019 11:37:45 +0100 Subject: [PATCH] Fix final bugs --- .../Controllers/PaymentRequestController.cs | 15 ++++---- BTCPayServer/Hosting/Startup.cs | 2 ++ .../PaymentRequest/PaymentRequestHub.cs | 35 +++++++++++-------- .../PaymentRequest/PaymentRequestService.cs | 9 +++-- .../PaymentRequestRepository.cs | 1 + .../PaymentRequest/EditPaymentRequest.cshtml | 17 +++++---- .../MinimalPaymentRequest.cshtml | 11 ++++-- .../PaymentRequest/ViewPaymentRequest.cshtml | 7 ++-- 8 files changed, 59 insertions(+), 38 deletions(-) diff --git a/BTCPayServer/Controllers/PaymentRequestController.cs b/BTCPayServer/Controllers/PaymentRequestController.cs index c1de44909..6e34a66ce 100644 --- a/BTCPayServer/Controllers/PaymentRequestController.cs +++ b/BTCPayServer/Controllers/PaymentRequestController.cs @@ -34,8 +34,6 @@ namespace BTCPayServer.Controllers private readonly InvoiceController _InvoiceController; private readonly UserManager _UserManager; private readonly StoreRepository _StoreRepository; - private readonly RateFetcher _RateFetcher; - private readonly BTCPayNetworkProvider _BtcPayNetworkProvider; private readonly PaymentRequestRepository _PaymentRequestRepository; private readonly PaymentRequestService _PaymentRequestService; private readonly EventAggregator _EventAggregator; @@ -46,8 +44,6 @@ namespace BTCPayServer.Controllers InvoiceController invoiceController, UserManager userManager, StoreRepository storeRepository, - RateFetcher rateFetcher, - BTCPayNetworkProvider btcPayNetworkProvider, PaymentRequestRepository paymentRequestRepository, PaymentRequestService paymentRequestService, EventAggregator eventAggregator, @@ -57,8 +53,6 @@ namespace BTCPayServer.Controllers _InvoiceController = invoiceController; _UserManager = userManager; _StoreRepository = storeRepository; - _RateFetcher = rateFetcher; - _BtcPayNetworkProvider = btcPayNetworkProvider; _PaymentRequestRepository = paymentRequestRepository; _PaymentRequestService = paymentRequestService; _EventAggregator = eventAggregator; @@ -135,6 +129,8 @@ namespace BTCPayServer.Controllers return NotFound(); } + var oldStatus = data?.Status ?? PaymentRequestData.PaymentRequestStatus.Creating; + if (data != null && data.Status != PaymentRequestData.PaymentRequestStatus.Creating) { return RedirectToAction("ViewPaymentRequest", new @@ -176,8 +172,11 @@ namespace BTCPayServer.Controllers _EventAggregator.Publish(new PaymentRequestUpdated() { Data = data, - PaymentRequestId = data.Id + PaymentRequestId = data.Id, + Published = oldStatus == PaymentRequestData.PaymentRequestStatus.Creating && + data.Status != PaymentRequestData.PaymentRequestStatus.Creating }); + return RedirectToAction("EditPaymentRequest", new {id = data.Id, StatusMessage = "Saved"}); } @@ -307,7 +306,7 @@ namespace BTCPayServer.Controllers store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id)); try { - var newInvoiceId = (await _InvoiceController.CreateInvoiceCore(new Invoice() + var newInvoiceId = (await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest() { OrderId = $"{PaymentRequestRepository.GetOrderIdForPaymentRequest(id)}", Currency = blob.Currency, diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index b214c446b..c463509cb 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -34,6 +34,7 @@ using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Mvc.Cors.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core; using System.Net; +using BTCPayServer.PaymentRequest; using Meziantou.AspNetCore.BundleTagHelpers; using BTCPayServer.Security; using BTCPayServer.Services.Apps; @@ -165,6 +166,7 @@ namespace BTCPayServer.Hosting app.UseSignalR(route => { route.MapHub("/apps/hub"); + route.MapHub("/payment-requests/hub"); }); app.UseWebSockets(); app.UseStatusCodePages(); diff --git a/BTCPayServer/PaymentRequest/PaymentRequestHub.cs b/BTCPayServer/PaymentRequest/PaymentRequestHub.cs index 4fd504f0a..26d30feef 100644 --- a/BTCPayServer/PaymentRequest/PaymentRequestHub.cs +++ b/BTCPayServer/PaymentRequest/PaymentRequestHub.cs @@ -44,7 +44,8 @@ namespace BTCPayServer.PaymentRequest public async Task Pay(decimal? amount = null) { _PaymentRequestController.ControllerContext.HttpContext = Context.GetHttpContext(); - var result = await _PaymentRequestController.PayPaymentRequest(Context.Items["pr-id"].ToString(), false, amount); + var result = + await _PaymentRequestController.PayPaymentRequest(Context.Items["pr-id"].ToString(), false, amount); switch (result) { case OkObjectResult okObjectResult: @@ -60,31 +61,28 @@ namespace BTCPayServer.PaymentRequest } } - public class PaymentRequestStreamer : EventHostedServiceBase + public class PaymentRequestStreamer : EventHostedServiceBase { private readonly IHubContext _HubContext; private readonly PaymentRequestRepository _PaymentRequestRepository; - private readonly AppService _AppService; private readonly PaymentRequestService _PaymentRequestService; - + public PaymentRequestStreamer(EventAggregator eventAggregator, IHubContext hubContext, PaymentRequestRepository paymentRequestRepository, - AppService appService, PaymentRequestService paymentRequestService) : base(eventAggregator) { _HubContext = hubContext; _PaymentRequestRepository = paymentRequestRepository; - _AppService = appService; _PaymentRequestService = paymentRequestService; } public override async Task StartAsync(CancellationToken cancellationToken) { await base.StartAsync(cancellationToken); - _CheckingPendingPayments = CheckingPendingPayments(cancellationToken).ContinueWith(_ => _CheckingPendingPayments = null, TaskScheduler.Default); - + _CheckingPendingPayments = CheckingPendingPayments(cancellationToken) + .ContinueWith(_ => _CheckingPendingPayments = null, TaskScheduler.Default); } private async Task CheckingPendingPayments(CancellationToken cancellationToken) @@ -92,19 +90,22 @@ namespace BTCPayServer.PaymentRequest Logs.PayServer.LogInformation("Starting payment request expiration watcher"); var (total, items) = await _PaymentRequestRepository.FindPaymentRequests(new PaymentRequestQuery() { - Status = new[] { PaymentRequestData.PaymentRequestStatus.Pending } + Status = new[] {PaymentRequestData.PaymentRequestStatus.Pending} }, cancellationToken); Logs.PayServer.LogInformation($"{total} pending payment requests being checked since last run"); - await Task.WhenAll(items.Select(i => _PaymentRequestService.UpdatePaymentRequestStateIfNeeded(i)).ToArray()); + await Task.WhenAll(items.Select(i => _PaymentRequestService.UpdatePaymentRequestStateIfNeeded(i)) + .ToArray()); } Task _CheckingPendingPayments; + public override async Task StopAsync(CancellationToken cancellationToken) { await base.StopAsync(cancellationToken); await (_CheckingPendingPayments ?? Task.CompletedTask); } + protected override void SubscibeToEvents() { Subscribe(); @@ -116,7 +117,7 @@ namespace BTCPayServer.PaymentRequest if (evt is InvoiceEvent invoiceEvent) { var paymentRequestId = - PaymentRequestRepository.GetPaymentRequestIdFromOrderId(invoiceEvent.Invoice.OrderId); + PaymentRequestRepository.GetPaymentRequestIdFromOrderId(invoiceEvent.Invoice.OrderId); if (invoiceEvent.Name == InvoiceEvent.ReceivedPayment) { @@ -131,14 +132,18 @@ namespace BTCPayServer.PaymentRequest invoiceEvent.Payment.GetPaymentMethodId().PaymentType) }); } + await InfoUpdated(paymentRequestId); } else if (evt is PaymentRequestUpdated updated) { - if (updated.Data.Status != PaymentRequestData.PaymentRequestStatus.Creating) + if (updated.Published) { - await InfoUpdated(updated.PaymentRequestId); + await _PaymentRequestService.UpdatePaymentRequestStateIfNeeded(updated.Data); } + + await InfoUpdated(updated.PaymentRequestId); + var expiry = updated.Data.GetBlob().ExpiryDate; if (updated.Data.Status == PaymentRequestData.PaymentRequestStatus.Pending && expiry.HasValue) @@ -165,10 +170,10 @@ namespace BTCPayServer.PaymentRequest private async Task InfoUpdated(string paymentRequestId) { var req = await _PaymentRequestService.GetPaymentRequest(paymentRequestId); - if(req != null) + if (req != null) { await _HubContext.Clients.Group(paymentRequestId) - .SendCoreAsync(PaymentRequestHub.InfoUpdated, new object[] { req }); + .SendCoreAsync(PaymentRequestHub.InfoUpdated, new object[] {req}); } } } diff --git a/BTCPayServer/PaymentRequest/PaymentRequestService.cs b/BTCPayServer/PaymentRequest/PaymentRequestService.cs index 154f264c7..859a28bdf 100644 --- a/BTCPayServer/PaymentRequest/PaymentRequestService.cs +++ b/BTCPayServer/PaymentRequest/PaymentRequestService.cs @@ -15,21 +15,18 @@ namespace BTCPayServer.PaymentRequest { public class PaymentRequestService { - - private readonly IHubContext _HubContext; private readonly PaymentRequestRepository _PaymentRequestRepository; private readonly BTCPayNetworkProvider _BtcPayNetworkProvider; private readonly AppService _AppService; private readonly CurrencyNameTable _currencies; - public PaymentRequestService(EventAggregator eventAggregator, + public PaymentRequestService( IHubContext hubContext, PaymentRequestRepository paymentRequestRepository, BTCPayNetworkProvider btcPayNetworkProvider, AppService appService, CurrencyNameTable currencies) { - _HubContext = hubContext; _PaymentRequestRepository = paymentRequestRepository; _BtcPayNetworkProvider = btcPayNetworkProvider; _AppService = appService; @@ -43,6 +40,7 @@ namespace BTCPayServer.PaymentRequest { return; } + await UpdatePaymentRequestStateIfNeeded(pr); } @@ -55,7 +53,7 @@ namespace BTCPayServer.PaymentRequest if (blob.ExpiryDate.Value <= DateTimeOffset.UtcNow) currentStatus = PaymentRequestData.PaymentRequestStatus.Expired; } - else if(pr.Status == PaymentRequestData.PaymentRequestStatus.Pending) + else if (pr.Status == PaymentRequestData.PaymentRequestStatus.Pending) { var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider); var invoices = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(pr.Id); @@ -67,6 +65,7 @@ namespace BTCPayServer.PaymentRequest currentStatus = PaymentRequestData.PaymentRequestStatus.Completed; } } + if (pr.Status != PaymentRequestData.PaymentRequestStatus.Creating && currentStatus != pr.Status) { pr.Status = currentStatus; diff --git a/BTCPayServer/Services/PaymentRequests/PaymentRequestRepository.cs b/BTCPayServer/Services/PaymentRequests/PaymentRequestRepository.cs index 6e84a6ae5..e584303e4 100644 --- a/BTCPayServer/Services/PaymentRequests/PaymentRequestRepository.cs +++ b/BTCPayServer/Services/PaymentRequests/PaymentRequestRepository.cs @@ -179,6 +179,7 @@ namespace BTCPayServer.Services.PaymentRequests { public string PaymentRequestId { get; set; } public PaymentRequestData Data { get; set; } + public bool Published { get; set; } } public class PaymentRequestQuery diff --git a/BTCPayServer/Views/PaymentRequest/EditPaymentRequest.cshtml b/BTCPayServer/Views/PaymentRequest/EditPaymentRequest.cshtml index e496178eb..d2864266b 100644 --- a/BTCPayServer/Views/PaymentRequest/EditPaymentRequest.cshtml +++ b/BTCPayServer/Views/PaymentRequest/EditPaymentRequest.cshtml @@ -40,7 +40,7 @@ - +
@@ -82,20 +82,25 @@
- - + + + + -
+
-
+
- + @if (!string.IsNullOrEmpty(Model.Id)) + { + View + } Back to list
diff --git a/BTCPayServer/Views/PaymentRequest/MinimalPaymentRequest.cshtml b/BTCPayServer/Views/PaymentRequest/MinimalPaymentRequest.cshtml index 03e0de8ef..0ff7bee3e 100644 --- a/BTCPayServer/Views/PaymentRequest/MinimalPaymentRequest.cshtml +++ b/BTCPayServer/Views/PaymentRequest/MinimalPaymentRequest.cshtml @@ -3,6 +3,13 @@
+ @if (Model.Status == "Creating") + { +
+ This payment request is still in creation mode and can only be viewed by you. Please publish from the settings in order to be able to share it. +
+ } +

@Model.Title @@ -117,7 +124,7 @@ class="form-control" type="number" name="amount" - + value="@Model.AmountDue" max="@Model.AmountDue" step="any" @@ -127,7 +134,7 @@ @Model.Currency.ToUpper()

diff --git a/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml b/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml index 9df095ac5..c506b0c90 100644 --- a/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml +++ b/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml @@ -51,6 +51,9 @@ else
+
+ This payment request is still in creation mode and can only be viewed by you. Please publish from the settings in order to be able to share it. +

{{srvModel.title}} @@ -117,7 +120,7 @@ else
- + @@ -149,7 +152,7 @@ else
Tx IdTx Id Payment Method Amount Link