Server side validation of PayButton POST

This commit is contained in:
rockstardev 2018-08-17 13:21:00 +02:00
parent 74ddcfa01e
commit 0084d4766b
4 changed files with 48 additions and 7 deletions

View file

@ -119,6 +119,9 @@
<Content Update="Views\Apps\PayButton.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButtonHandle.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButtonTest.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>

View file

@ -291,12 +291,7 @@ namespace BTCPayServer.Controllers
var settings = app.GetSettings<PointOfSaleSettings>();
var store = await GetStore(app);
var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
.Select(a=>a.PaymentId.ToString()).ToList();
var currencyDropdown = new List<string>();
currencyDropdown.Add(settings.Currency);
currencyDropdown.AddRange(paymentMethods);
List<string> currencyDropdown = supportedCurrencies(settings, store);
var model = new PayButtonViewModel
{
@ -309,6 +304,15 @@ namespace BTCPayServer.Controllers
return View(model);
}
private List<string> supportedCurrencies(PointOfSaleSettings settings, StoreData store)
{
var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
.Select(a => a.PaymentId.ToString()).ToList();
var currencyDropdown = new List<string>();
currencyDropdown.Add(settings.Currency);
currencyDropdown.AddRange(paymentMethods);
return currencyDropdown;
}
[HttpPost]
[Route("{appId}/pay")]
@ -318,8 +322,20 @@ namespace BTCPayServer.Controllers
{
var app = await GetApp(appId, AppType.PointOfSale);
var settings = app.GetSettings<PointOfSaleSettings>();
var store = await GetStore(app);
// TODO: extract validation to model
if (model.Price <= 0)
ModelState.AddModelError("Price", "Price must be greater than 0");
var curr = supportedCurrencies(settings, store);
if (!curr.Contains(model.Currency))
ModelState.AddModelError("Currency", $"Selected currency {model.Currency} is not supported in this store");
//
if (!ModelState.IsValid)
return View();
var invoice = await _InvoiceController.CreateInvoiceCore(new NBitpayClient.Invoice()
{
Price = model.Price,

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
@ -8,6 +9,7 @@ namespace BTCPayServer.Models.AppViewModels
public class PayButtonViewModel
{
public decimal Price { get; set; }
[Required]
public string Currency { get; set; }
public string CheckoutDesc { get; set; }
public string OrderId { get; set; }

View file

@ -0,0 +1,20 @@
@{
var allErrors = ViewData.ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage));
}
<section>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h2>Pay Button request failed</h2>
Please fix following errors:
<ul>
@foreach (var error in allErrors)
{
<li>@error</li>
}
</ul>
</div>
</div>
</div>
</section>