From e49f25af093040573ddf8b1a8f8ce7d6e005b8c1 Mon Sep 17 00:00:00 2001 From: lepipele Date: Fri, 20 Oct 2017 22:24:28 -0500 Subject: [PATCH] Returning whole invoice serialized as JSON on $ajax call --- .../Controllers/InvoiceController.UI.cs | 25 ++++++++++++------- BTCPayServer/wwwroot/js/core.js | 7 +++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index a9275891d..316a9228d 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -125,9 +125,18 @@ namespace BTCPayServer.Controllers id = invoiceId; //// - var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); - if(invoice == null) + var model = await GetInvoiceModel(invoiceId); + if (model == null) return NotFound(); + + return View(nameof(Checkout), model); + } + + private async Task GetInvoiceModel(string invoiceId) + { + var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); + if (invoice == null) + return null; var store = await _StoreRepository.FindStore(invoice.StoreId); var dto = invoice.EntityToDTO(); @@ -154,11 +163,9 @@ namespace BTCPayServer.Controllers status = invoice.Status }; - - - var expiration = TimeSpan.FromSeconds((double)model.expirationSeconds); + var expiration = TimeSpan.FromSeconds(model.expirationSeconds); model.TimeLeft = PrettyPrint(expiration); - return View(nameof(Checkout), model); + return model; } private string PrettyPrint(TimeSpan expiration) @@ -176,10 +183,10 @@ namespace BTCPayServer.Controllers [Route("i/{invoiceId}/status")] public async Task GetStatus(string invoiceId) { - var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); - if(invoice == null) + var model = await GetInvoiceModel(invoiceId); + if(model == null) return NotFound(); - return Content(invoice.Status); + return Json(model); } [HttpPost] diff --git a/BTCPayServer/wwwroot/js/core.js b/BTCPayServer/wwwroot/js/core.js index 20afbac90..d54fdc5c6 100644 --- a/BTCPayServer/wwwroot/js/core.js +++ b/BTCPayServer/wwwroot/js/core.js @@ -158,7 +158,9 @@ $("#copy-tab").click(function () { var oldStat = srvModel.status; onDataCallback(srvModel.status); -function onDataCallback(newStatus) { +function onDataCallback(jsonData) { + var newStatus = jsonData.status; + if (oldStat != newStatus) { oldStat = newStatus; window.parent.postMessage({ "invoiceId": srvModel.invoiceId, "status": newStatus }, "*"); @@ -206,8 +208,7 @@ var watcher = setInterval(function () { url: path, type: "GET" }).done(function (data) { - status = data; - onDataCallback(status); + onDataCallback(data); }).fail(function (jqXHR, textStatus, errorThrown) { });