Skip to content

Commit

Permalink
invoice form through receipt page
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Nov 15, 2022
1 parent 063efd5 commit da77d21
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 56 deletions.
58 changes: 50 additions & 8 deletions BTCPayServer/Controllers/UIInvoiceController.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public async Task<IActionResult> InvoiceReceipt(string invoiceId)
}
JToken? receiptData = null;
i.Metadata?.AdditionalData?.TryGetValue("receiptData", out receiptData);
string? formResponse = null;
if (i.Metadata?.AdditionalData?.TryGetValue("formResponse", out var formResponseRaw)is true)
{
formResponse = formResponseRaw.Value<string>();
}

return View(new InvoiceReceiptViewModel
{
Expand All @@ -190,6 +195,8 @@ public async Task<IActionResult> InvoiceReceipt(string invoiceId)
InvoiceId = i.Id,
OrderId = i.Metadata?.OrderId,
OrderUrl = i.Metadata?.OrderUrl,
FormId = i.CheckoutFormId,
FormSubmitted = formResponse == i.CheckoutFormId,
Payments = receipt.ShowPayments is false ? null : i.GetPayments(true).Select(paymentEntity =>
{
var paymentData = paymentEntity.GetCryptoPaymentData();
Expand Down Expand Up @@ -228,6 +235,48 @@ public async Task<IActionResult> InvoiceReceipt(string invoiceId)
});

}


[HttpGet("i/{invoiceId}/form")]
[HttpPost("i/{invoiceId}/form")]
[AllowAnonymous]
public async Task<IActionResult> ViewInvoiceForm(string invoiceId)
{
TempData.TryGetValue("formResponse", out var formResponseRaw);
TempData.Remove("formResponse");

var i = await _InvoiceRepository.GetInvoice(invoiceId);
if (i is null)
return NotFound();
var store = await _StoreRepository.GetStoreByInvoiceId(i.Id);
if (store is null)
return NotFound();

var formId = i.CheckoutFormId;

JObject formResponse = null;
switch (formId)
{
case { } frid when string.IsNullOrEmpty(frid) || frid == GenericFormOption.None.ToString():
break;
default:
if (formResponseRaw is string raw && !string.IsNullOrEmpty(raw) )
{

var newMeta = i.Metadata.ToJObject();
newMeta.Merge(JObject.Parse(raw));
await _InvoiceRepository.UpdateInvoiceMetadata(invoiceId, store.Id, newMeta);
return RedirectToAction("InvoiceReceipt", new {invoiceId});
}
else
{
var redirect = Request.GetCurrentUrl();
return RedirectToAction("ViewForm", "UIForms", new {id = formId, redirectUrl = redirect});
}
}

return RedirectToAction("InvoiceReceipt", new {invoiceId});
}
private string? GetTransactionLink(PaymentMethodId paymentMethodId, string txId)
{
var network = _NetworkProvider.GetNetwork(paymentMethodId.CryptoCode);
Expand Down Expand Up @@ -924,14 +973,7 @@ public async Task<IActionResult> UpdateCustomer(string invoiceId, [FromBody] Upd
await _InvoiceRepository.UpdateInvoice(invoiceId, data).ConfigureAwait(false);
return Ok("{}");
}

[HttpPost("i/{invoiceId}/Form")]
[HttpPost("invoice/Form")]
public IActionResult UpdateForm(string invoiceId)
{
// TODO: Forms integration
return Ok();
}


[HttpGet("/stores/{storeId}/invoices")]
[HttpGet("invoices")]
Expand Down
2 changes: 1 addition & 1 deletion BTCPayServer/Forms/UIFormsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private async Task<FormData> GetFormData(string id)
dbForm.ApplyValuesFromForm(Request.Form, "internal");

Dictionary<string, object> data = dbForm.GetValues();

data.TryAdd("formResponse", orig.Id);

// var redirect = dbForm.GetFieldByName("integration_redirectUrl")?.Value;
if (TempData.TryGetValue("redirectUrl", out var r) && r is string redirect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class InvoiceReceiptViewModel
public ReceiptOptions ReceiptOptions { get; set; }
public List<ViewPaymentRequestViewModel.PaymentRequestInvoicePayment> Payments { get; set; }
public string OrderUrl { get; set; }
public bool FormSubmitted { get; set; }
public string FormId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public async Task<IActionResult> ViewPointOfSale(string appId, PosViewType? view
PosData = string.IsNullOrEmpty(posData) ? null : posData,
RedirectAutomatically = settings.RedirectAutomatically,
SupportedTransactionCurrencies = paymentMethods,
CheckoutFormId = blob.CheckoutFormId,
CheckoutFormId = null,
RequiresRefundEmail = requiresRefundEmail == RequiresRefundEmail.InheritFromStore
? blob.RequiresRefundEmail
: requiresRefundEmail == RequiresRefundEmail.On,
Expand Down
54 changes: 27 additions & 27 deletions BTCPayServer/Views/UIInvoice/CheckoutV2.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,33 @@
</div>
</section>
<section id="form" v-else-if="step === 'form'">
<form method="post" asp-action="UpdateForm" asp-route-invoiceId="@Model.InvoiceId" v-on:submit.prevent="onFormSubmit">
<div class="top">
<h6>{{$t("Please fill out the following")}}</h6>
<div class="timer" v-if="expiringSoon">
{{$t("Invoice will expire in")}} {{timerText}}
<span class="spinner-border spinner-border-sm ms-2" role="status">
<span class="visually-hidden"></span>
</span>
</div>
<template v-if="srvModel.requiresRefundEmail">
<p>{{$t("Contact_Body")}}</p>
<div class="form-group">
<label class="form-label" for="Email">{{$t("Contact and Refund Email")}}</label>
<input class="form-control" id="Email" name="Email">
<span class="text-danger" hidden>{{$t("Please enter a valid email address")}}</span>
</div>
</template>
</div>
<div class="buttons">
<button type="submit" class="btn btn-primary" :disabled="formSubmitPending" :class="{ 'loading': formSubmitPending }">
{{$t("Continue")}}
<span class="spinner-border spinner-border-sm ms-1" role="status" v-if="formSubmitPending">
<span class="visually-hidden"></span>
</span>
</button>
</div>
</form>
@* <form method="post" asp-action="UpdateForm" asp-route-invoiceId="@Model.InvoiceId" v-on:submit.prevent="onFormSubmit"> *@
@* <div class="top"> *@
@* <h6>{{$t("Please fill out the following")}}</h6> *@
@* <div class="timer" v-if="expiringSoon"> *@
@* {{$t("Invoice will expire in")}} {{timerText}} *@
@* <span class="spinner-border spinner-border-sm ms-2" role="status"> *@
@* <span class="visually-hidden"></span> *@
@* </span> *@
@* </div> *@
@* <template v-if="srvModel.requiresRefundEmail"> *@
@* <p>{{$t("Contact_Body")}}</p> *@
@* <div class="form-group"> *@
@* <label class="form-label" for="Email">{{$t("Contact and Refund Email")}}</label> *@
@* <input class="form-control" id="Email" name="Email"> *@
@* <span class="text-danger" hidden>{{$t("Please enter a valid email address")}}</span> *@
@* </div> *@
@* </template> *@
@* </div> *@
@* <div class="buttons"> *@
@* <button type="submit" class="btn btn-primary" :disabled="formSubmitPending" :class="{ 'loading': formSubmitPending }"> *@
@* {{$t("Continue")}} *@
@* <span class="spinner-border spinner-border-sm ms-1" role="status" v-if="formSubmitPending"> *@
@* <span class="visually-hidden"></span> *@
@* </span> *@
@* </button> *@
@* </div> *@
@* </form> *@
</section>
<section id="payment" v-else>
<h6 class="text-center mb-3 fw-semibold" v-if="srvModel.itemDesc" v-text="srvModel.itemDesc">@Model.ItemDesc</h6>
Expand Down
45 changes: 26 additions & 19 deletions BTCPayServer/Views/UIInvoice/InvoiceReceipt.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ViewData["Title"] = $"Receipt from {Model.StoreName}";
var isProcessing = Model.Status == InvoiceStatus.Processing;
var isSettled = Model.Status == InvoiceStatus.Settled;
var requiresForm = !string.IsNullOrEmpty(Model.FormId) && !Model.FormSubmitted;
}

<!DOCTYPE html>
Expand Down Expand Up @@ -39,6 +40,12 @@
<div class="d-flex flex-column justify-content-center gap-4">
<h1 class="h3 text-center">@ViewData["Title"]</h1>
<div id="InvoiceSummary" class="bg-tile p-3 p-sm-4 rounded d-flex flex-wrap align-items-center">
@if (requiresForm)
{
<div class="w-100 align-content-center">
<a asp-action="ViewInvoiceForm" asp-route-invoiceId="@Model.InvoiceId" class="btn btn-lg btn-primary ">Fill out form</a>
</div>
}
@if (isProcessing)
{
<div class="lead text-center text-muted py-5 px-4 fw-semibold" id="invoice-processing">
Expand All @@ -47,7 +54,7 @@
}
else if (!isSettled)
{
<div class="lead text-center text-muted py-5 px-4 fw-semibold" id="invoice-unsettled">
<div class="lead text-center text-muted py-5 px-4 fw-semibold" id="invoice-unsettled">
The invoice is not settled.
</div>
}
Expand Down Expand Up @@ -78,24 +85,24 @@
<div class="d-flex flex-column">
<dd class="text-muted mb-0 fw-semibold">Order ID</dd>
<dt class="fs-5 mb-0 text-break fw-semibold">
@if (!string.IsNullOrEmpty(Model.OrderUrl))
{
<a href="@Model.OrderUrl" rel="noreferrer noopener" target="_blank">
@if (string.IsNullOrEmpty(Model.OrderId))
{
<span>View Order</span>
}
else
{
@Model.OrderId
}
</a>
}
else
{
<span>@Model.OrderId</span>
}
</dt>
@if (!string.IsNullOrEmpty(Model.OrderUrl))
{
<a href="@Model.OrderUrl" rel="noreferrer noopener" target="_blank">
@if (string.IsNullOrEmpty(Model.OrderId))
{
<span>View Order</span>
}
else
{
@Model.OrderId
}
</a>
}
else
{
<span>@Model.OrderId</span>
}
</dt>
</div>
}
</dl>
Expand Down

0 comments on commit da77d21

Please sign in to comment.