diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index 26ebe0aed..620653b3a 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -122,13 +122,13 @@ $(IncludeRazorContentInPack) - + $(IncludeRazorContentInPack) - + $(IncludeRazorContentInPack) - + $(IncludeRazorContentInPack) diff --git a/BTCPayServer/Controllers/AppsController.PayButton.cs b/BTCPayServer/Controllers/AppsController.PayButton.cs index 834c30bdf..d9b1e104b 100644 --- a/BTCPayServer/Controllers/AppsController.PayButton.cs +++ b/BTCPayServer/Controllers/AppsController.PayButton.cs @@ -11,84 +11,5 @@ 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, - PayButtonImageUrl = appUrl + "/img/paybutton/pay.png", - AppId = appId - }; - 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 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/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index 488fb6355..ef4f671bb 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -1,8 +1,13 @@ -using BTCPayServer.Authentication; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Authentication; using BTCPayServer.Configuration; using BTCPayServer.Data; -using BTCPayServer.HostedServices; using BTCPayServer.Models; +using BTCPayServer.Models.AppViewModels; using BTCPayServer.Models.StoreViewModels; using BTCPayServer.Rating; using BTCPayServer.Security; @@ -11,21 +16,13 @@ using BTCPayServer.Services.Rates; using BTCPayServer.Services.Stores; using BTCPayServer.Services.Wallets; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; -using Microsoft.Extensions.Options; using NBitcoin; using NBitcoin.DataEncoders; -using NBXplorer.DerivationStrategy; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; namespace BTCPayServer.Controllers { @@ -51,7 +48,8 @@ namespace BTCPayServer.Controllers ExplorerClientProvider explorerProvider, IFeeProviderFactory feeRateProvider, LanguageService langService, - IHostingEnvironment env) + IHostingEnvironment env, + InvoiceController invoiceController) { _RateFactory = rateFactory; _Repo = repo; @@ -67,6 +65,7 @@ namespace BTCPayServer.Controllers _ServiceProvider = serviceProvider; _BtcpayServerOptions = btcpayServerOptions; _BTCPayEnv = btcpayEnv; + _InvoiceController = invoiceController; } BTCPayServerOptions _BtcpayServerOptions; BTCPayServerEnvironment _BTCPayEnv; @@ -81,6 +80,7 @@ namespace BTCPayServer.Controllers UserManager _UserManager; private LanguageService _LangService; IHostingEnvironment _Env; + InvoiceController _InvoiceController; [TempData] public string StatusMessage @@ -767,5 +767,82 @@ namespace BTCPayServer.Controllers return null; return _UserManager.GetUserId(User); } + + + + // 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("{storeId}/paybutton")] + public async Task PayButton() + { + var store = StoreData; + var currencyDropdown = supportedCurrencies(store); + + var appUrl = HttpContext.Request.GetAbsoluteRoot(); + var model = new PayButtonViewModel + { + Price = 10, + Currency = DEFAULT_CURRENCY, + ButtonSize = 2, + UrlRoot = appUrl, + CurrencyDropdown = currencyDropdown, + PayButtonImageUrl = appUrl + "/img/paybutton/pay.png", + StoreId = store.Id + }; + 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("{storeId}/pay")] + [IgnoreAntiforgeryToken] + [EnableCors(CorsPolicies.All)] + public async Task PayButtonHandle(string storeId, [FromForm]PayButtonViewModel model) + { + var store = StoreData; + + // 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("{storeId}/paybuttontest")] + public IActionResult PayButtonTest(string storeId) + { + return View(); + } } } diff --git a/BTCPayServer/Models/AppViewModels/PayButtonViewModel.cs b/BTCPayServer/Models/StoreViewModels/PayButtonViewModel.cs similarity index 90% rename from BTCPayServer/Models/AppViewModels/PayButtonViewModel.cs rename to BTCPayServer/Models/StoreViewModels/PayButtonViewModel.cs index 6fc2e64ca..b8fbd306c 100644 --- a/BTCPayServer/Models/AppViewModels/PayButtonViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/PayButtonViewModel.cs @@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; -namespace BTCPayServer.Models.AppViewModels +namespace BTCPayServer.Models.StoreViewModels { public class PayButtonViewModel { @@ -21,7 +21,7 @@ namespace BTCPayServer.Models.AppViewModels [EmailAddress] public string NotifyEmail { get; set; } - public string AppId { get; set; } + public string StoreId { get; set; } // Data that influences Pay Button UI, but not invoice creation public string UrlRoot { get; set; } diff --git a/BTCPayServer/Views/Apps/ViewPayButton.cshtml b/BTCPayServer/Views/Stores/PayButton.cshtml similarity index 99% rename from BTCPayServer/Views/Apps/ViewPayButton.cshtml rename to BTCPayServer/Views/Stores/PayButton.cshtml index 1df322a28..abc03c0ee 100644 --- a/BTCPayServer/Views/Apps/ViewPayButton.cshtml +++ b/BTCPayServer/Views/Stores/PayButton.cshtml @@ -1,6 +1,7 @@ @model PayButtonViewModel @{ ViewData["Title"] = "Pay Button"; + ViewData.SetActivePageAndTitle(StoreNavPages.PayButton, "Pay Button"); }
diff --git a/BTCPayServer/Views/Apps/PayButtonHandle.cshtml b/BTCPayServer/Views/Stores/PayButtonHandle.cshtml similarity index 100% rename from BTCPayServer/Views/Apps/PayButtonHandle.cshtml rename to BTCPayServer/Views/Stores/PayButtonHandle.cshtml diff --git a/BTCPayServer/Views/Apps/PayButtonTest.cshtml b/BTCPayServer/Views/Stores/PayButtonTest.cshtml similarity index 100% rename from BTCPayServer/Views/Apps/PayButtonTest.cshtml rename to BTCPayServer/Views/Stores/PayButtonTest.cshtml