diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index ea5d20289..c5e30e234 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -119,7 +119,7 @@ $(IncludeRazorContentInPack) - + $(IncludeRazorContentInPack) diff --git a/BTCPayServer/Controllers/AppsController.PayButton.cs b/BTCPayServer/Controllers/AppsController.PayButton.cs new file mode 100644 index 000000000..004512c58 --- /dev/null +++ b/BTCPayServer/Controllers/AppsController.PayButton.cs @@ -0,0 +1,93 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Data; +using BTCPayServer.Models.AppViewModels; +using BTCPayServer.Services.Apps; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace BTCPayServer.Controllers +{ + public partial class AppsController + { + // TODO: Need to have talk about how architect default currency implementation + // For now we have also hardcoded USD for Store creation and then Invoice creation + const string DEFAULT_CURRENCY = "USD"; + + [Route("{appId}/paybutton")] + public async Task ViewPayButton(string appId) + { + var app = await GetApp(appId, AppType.PayButton); + if (app == null) + return NotFound(); + + var store = await GetStore(app); + var currencyDropdown = supportedCurrencies(store); + + var appUrl = HttpContext.Request.GetAbsoluteRoot(); + var model = new PayButtonViewModel + { + Price = 10, + Currency = DEFAULT_CURRENCY, + ButtonSize = 2, + UrlRoot = appUrl, + CurrencyDropdown = currencyDropdown + }; + return View(model); + } + + private List supportedCurrencies(StoreData store) + { + var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider) + .Select(a => a.PaymentId.ToString()).ToList(); + var currencyDropdown = new List(); + currencyDropdown.Add(DEFAULT_CURRENCY); + currencyDropdown.AddRange(paymentMethods); + return currencyDropdown; + } + + [HttpPost] + [Route("{appId}/pay")] + [IgnoreAntiforgeryToken] + [EnableCors(CorsPolicies.All)] + public async Task PayButtonHandle(string appId, [FromForm]PayButtonViewModel model) + { + var app = await GetApp(appId, AppType.PayButton); + var settings = app.GetSettings(); + 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(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, + Currency = model.Currency, + ItemDesc = model.CheckoutDesc, + OrderId = model.OrderId, + BuyerEmail = model.NotifyEmail, + NotificationURL = model.ServerIpn, + RedirectURL = model.BrowserRedirect, + FullNotifications = true + }, store, HttpContext.Request.GetAbsoluteRoot()); + return Redirect(invoice.Data.Url); + } + + [HttpGet] + [Route("{appId}/paybuttontest")] + public IActionResult PayButtonTest(string appId) + { + return View(); + } + } +} diff --git a/BTCPayServer/Controllers/AppsController.PointOfSale.cs b/BTCPayServer/Controllers/AppsController.PointOfSale.cs index 614729ab7..bd6dadad7 100644 --- a/BTCPayServer/Controllers/AppsController.PointOfSale.cs +++ b/BTCPayServer/Controllers/AppsController.PointOfSale.cs @@ -263,14 +263,6 @@ namespace BTCPayServer.Controllers return Redirect(invoice.Data.Url); } - private async Task GetStore(AppData app) - { - using (var ctx = _ContextFactory.CreateContext()) - { - return await ctx.Stores.FirstOrDefaultAsync(s => s.Id == app.StoreDataId); - } - } - private async Task UpdateAppSettings(AppData app) { using (var ctx = _ContextFactory.CreateContext()) @@ -281,81 +273,5 @@ namespace BTCPayServer.Controllers await ctx.SaveChangesAsync(); } } - - [Route("{appId}/paybutton")] - public async Task PayButton(string appId) - { - var app = await GetApp(appId, AppType.PointOfSale); - if (app == null) - return NotFound(); - var settings = app.GetSettings(); - - var store = await GetStore(app); - List currencyDropdown = supportedCurrencies(settings, store); - - var appUrl = HttpContext.Request.GetAbsoluteRoot(); - var model = new PayButtonViewModel - { - Price = 10, - Currency = settings.Currency, - ButtonSize = 2, - UrlRoot = appUrl, - CurrencyDropdown = currencyDropdown - }; - return View(model); - } - - private List supportedCurrencies(PointOfSaleSettings settings, StoreData store) - { - var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider) - .Select(a => a.PaymentId.ToString()).ToList(); - var currencyDropdown = new List(); - currencyDropdown.Add(settings.Currency); - currencyDropdown.AddRange(paymentMethods); - return currencyDropdown; - } - - [HttpPost] - [Route("{appId}/pay")] - [IgnoreAntiforgeryToken] - [EnableCors(CorsPolicies.All)] - public async Task PayButtonHandle(string appId, [FromForm]PayButtonViewModel model) - { - var app = await GetApp(appId, AppType.PointOfSale); - var settings = app.GetSettings(); - 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, - Currency = model.Currency, - ItemDesc = model.CheckoutDesc, - OrderId = model.OrderId, - BuyerEmail = model.NotifyEmail, - NotificationURL = model.ServerIpn, - RedirectURL = model.BrowserRedirect, - FullNotifications = true - }, store, HttpContext.Request.GetAbsoluteRoot()); - return Redirect(invoice.Data.Url); - } - - [HttpGet] - [Route("{appId}/paybuttontest")] - public IActionResult PayButtonTest(string appId) - { - return View(); - } } } diff --git a/BTCPayServer/Controllers/AppsController.cs b/BTCPayServer/Controllers/AppsController.cs index eba124b81..4677e2480 100644 --- a/BTCPayServer/Controllers/AppsController.cs +++ b/BTCPayServer/Controllers/AppsController.cs @@ -1,18 +1,17 @@ using System; -using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Data; using BTCPayServer.Models; using BTCPayServer.Models.AppViewModels; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; -using NBitcoin.DataEncoders; -using NBitcoin; using BTCPayServer.Services.Apps; using BTCPayServer.Services.Rates; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using NBitcoin; +using NBitcoin.DataEncoders; namespace BTCPayServer.Controllers { @@ -202,5 +201,13 @@ namespace BTCPayServer.Controllers { return _UserManager.GetUserId(User); } + + private async Task GetStore(AppData app) + { + using (var ctx = _ContextFactory.CreateContext()) + { + return await ctx.Stores.FirstOrDefaultAsync(s => s.Id == app.StoreDataId); + } + } } } diff --git a/BTCPayServer/Services/Apps/AppType.cs b/BTCPayServer/Services/Apps/AppType.cs index da9563bd0..bd9d3ff06 100644 --- a/BTCPayServer/Services/Apps/AppType.cs +++ b/BTCPayServer/Services/Apps/AppType.cs @@ -7,6 +7,7 @@ namespace BTCPayServer.Services.Apps { public enum AppType { - PointOfSale + PointOfSale, + PayButton } } diff --git a/BTCPayServer/Views/Apps/ListApps.cshtml b/BTCPayServer/Views/Apps/ListApps.cshtml index 773372650..f9340ea23 100644 --- a/BTCPayServer/Views/Apps/ListApps.cshtml +++ b/BTCPayServer/Views/Apps/ListApps.cshtml @@ -1,4 +1,5 @@ -@model ListAppsViewModel +@using BTCPayServer.Services.Apps +@model ListAppsViewModel @{ ViewData["Title"] = "Stores"; } @@ -48,10 +49,9 @@ @app.AppName @app.AppType - @if (app.IsOwner) + @if (app.IsOwner && app.AppType != AppType.PayButton.ToString()) { Settings - - Pay Button - } View - Remove diff --git a/BTCPayServer/Views/Apps/PayButton.cshtml b/BTCPayServer/Views/Apps/ViewPayButton.cshtml similarity index 100% rename from BTCPayServer/Views/Apps/PayButton.cshtml rename to BTCPayServer/Views/Apps/ViewPayButton.cshtml