Change button path to api/v1/invoices

This commit is contained in:
nicolas.dorier
2018-09-08 14:53:11 +09:00
parent fed53661b3
commit 0d3364b3da
4 changed files with 31 additions and 6 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Filters;
using BTCPayServer.Models.StoreViewModels; using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
@@ -22,12 +23,13 @@ namespace BTCPayServer.Controllers
private StoreRepository _StoreRepository; private StoreRepository _StoreRepository;
[HttpPost] [HttpPost]
[Route("/pay/{storeId}")] [Route("api/v1/invoices")]
[MediaTypeAcceptConstraintAttribute("text/html")]
[IgnoreAntiforgeryToken] [IgnoreAntiforgeryToken]
[EnableCors(CorsPolicies.All)] [EnableCors(CorsPolicies.All)]
public async Task<IActionResult> PayButtonHandle(string storeId, [FromForm]PayButtonViewModel model) public async Task<IActionResult> PayButtonHandle([FromForm]PayButtonViewModel model)
{ {
var store = await _StoreRepository.FindStore(storeId); var store = await _StoreRepository.FindStore(model.StoreId);
if (store == null) if (store == null)
ModelState.AddModelError("Store", "Invalid store"); ModelState.AddModelError("Store", "Invalid store");
else else
@@ -37,7 +39,6 @@ namespace BTCPayServer.Controllers
ModelState.AddModelError("Store", "Store has not enabled Pay Button"); ModelState.AddModelError("Store", "Store has not enabled Pay Button");
} }
// TODO: extract validation to model
if (model == null || model.Price <= 0) if (model == null || model.Price <= 0)
ModelState.AddModelError("Price", "Price must be greater than 0"); ModelState.AddModelError("Price", "Price must be greater than 0");

View File

@@ -28,6 +28,28 @@ namespace BTCPayServer.Filters
} }
} }
public class MediaTypeAcceptConstraintAttribute : Attribute, IActionConstraint
{
public MediaTypeAcceptConstraintAttribute(string mediaType)
{
MediaType = mediaType ?? throw new ArgumentNullException(nameof(mediaType));
}
public string MediaType
{
get; set;
}
public int Order => 100;
public bool Accept(ActionConstraintContext context)
{
if (!context.RouteContext.HttpContext.Request.Headers.ContainsKey("Accept"))
return false;
return context.RouteContext.HttpContext.Request.Headers["Accept"].ToString().StartsWith(MediaType, StringComparison.Ordinal);
}
}
public class BitpayAPIConstraintAttribute : Attribute, IActionConstraint public class BitpayAPIConstraintAttribute : Attribute, IActionConstraint
{ {
public BitpayAPIConstraintAttribute(bool isBitpayAPI = true) public BitpayAPIConstraintAttribute(bool isBitpayAPI = true)

View File

@@ -9,6 +9,7 @@ namespace BTCPayServer.Models.StoreViewModels
public class PayButtonViewModel public class PayButtonViewModel
{ {
public decimal Price { get; set; } public decimal Price { get; set; }
public string InvoiceId { get; set; }
[Required] [Required]
public string Currency { get; set; } public string Currency { get; set; }
public string CheckoutDesc { get; set; } public string CheckoutDesc { get; set; }

View File

@@ -42,7 +42,8 @@ function inputChanges(event, buttonSize) {
srvModel.buttonSize = buttonSize; srvModel.buttonSize = buttonSize;
} }
var html = '<form method="POST" action="' + esc(srvModel.urlRoot + 'pay/'+ srvModel.storeId) + '">'; var html = '<form method="POST" action="' + esc(srvModel.urlRoot) + 'api/v1/invoices">';
html += addinput("storeId", srvModel.storeId);
html += addinput("price", srvModel.price); html += addinput("price", srvModel.price);
if (srvModel.currency) { if (srvModel.currency) {
html += addinput("currency", srvModel.currency); html += addinput("currency", srvModel.currency);