2018-08-30 18:53:59 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-03-05 09:09:17 +01:00
|
|
|
|
using System.Threading;
|
2018-08-30 18:53:59 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2018-09-08 07:53:11 +02:00
|
|
|
|
using BTCPayServer.Filters;
|
2019-02-21 10:40:27 +01:00
|
|
|
|
using BTCPayServer.Models;
|
2018-08-30 18:53:59 +02:00
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class PublicController : Controller
|
|
|
|
|
{
|
|
|
|
|
public PublicController(InvoiceController invoiceController,
|
|
|
|
|
StoreRepository storeRepository)
|
|
|
|
|
{
|
|
|
|
|
_InvoiceController = invoiceController;
|
|
|
|
|
_StoreRepository = storeRepository;
|
|
|
|
|
}
|
2018-09-04 06:18:07 +02:00
|
|
|
|
|
2018-08-30 18:53:59 +02:00
|
|
|
|
private InvoiceController _InvoiceController;
|
|
|
|
|
private StoreRepository _StoreRepository;
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-09-08 07:53:11 +02:00
|
|
|
|
[Route("api/v1/invoices")]
|
|
|
|
|
[MediaTypeAcceptConstraintAttribute("text/html")]
|
2018-08-30 18:53:59 +02:00
|
|
|
|
[IgnoreAntiforgeryToken]
|
|
|
|
|
[EnableCors(CorsPolicies.All)]
|
2019-03-05 09:09:17 +01:00
|
|
|
|
public async Task<IActionResult> PayButtonHandle([FromForm]PayButtonViewModel model, CancellationToken cancellationToken)
|
2018-08-30 18:53:59 +02:00
|
|
|
|
{
|
2018-09-08 07:53:11 +02:00
|
|
|
|
var store = await _StoreRepository.FindStore(model.StoreId);
|
2018-08-30 18:53:59 +02:00
|
|
|
|
if (store == null)
|
|
|
|
|
ModelState.AddModelError("Store", "Invalid store");
|
2018-09-04 06:18:07 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var storeBlob = store.GetStoreBlob();
|
2018-09-08 07:32:26 +02:00
|
|
|
|
if (!storeBlob.AnyoneCanInvoice)
|
2018-09-04 06:18:07 +02:00
|
|
|
|
ModelState.AddModelError("Store", "Store has not enabled Pay Button");
|
|
|
|
|
}
|
2018-09-08 07:53:11 +02:00
|
|
|
|
|
2018-09-04 06:18:07 +02:00
|
|
|
|
if (model == null || model.Price <= 0)
|
2018-08-30 18:53:59 +02:00
|
|
|
|
ModelState.AddModelError("Price", "Price must be greater than 0");
|
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
return View();
|
|
|
|
|
|
2019-02-21 10:40:27 +01:00
|
|
|
|
var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
|
2018-08-30 18:53:59 +02:00
|
|
|
|
{
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Currency = model.Currency,
|
|
|
|
|
ItemDesc = model.CheckoutDesc,
|
|
|
|
|
OrderId = model.OrderId,
|
2018-10-12 03:09:13 +02:00
|
|
|
|
NotificationEmail = model.NotifyEmail,
|
2018-08-30 18:53:59 +02:00
|
|
|
|
NotificationURL = model.ServerIpn,
|
|
|
|
|
RedirectURL = model.BrowserRedirect,
|
|
|
|
|
FullNotifications = true
|
2019-03-05 09:09:17 +01:00
|
|
|
|
}, store, HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
|
2018-08-30 18:53:59 +02:00
|
|
|
|
return Redirect(invoice.Data.Url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|