Transfering Pay Button from App directly to Store

This commit is contained in:
rockstardev 2018-08-22 13:57:54 +02:00
parent fe31dc8606
commit 5a43ce2719
7 changed files with 95 additions and 96 deletions

View File

@ -122,13 +122,13 @@
<Content Update="Views\Server\SSHService.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\ViewPayButton.cshtml">
<Content Update="Views\Stores\PayButton.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButtonHandle.cshtml">
<Content Update="Views\Stores\PayButtonHandle.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButtonTest.cshtml">
<Content Update="Views\Stores\PayButtonTest.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Server\LNDGRPCServices.cshtml">

View File

@ -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<IActionResult> 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<string> supportedCurrencies(StoreData store)
{
var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
.Select(a => a.PaymentId.ToString()).ToList();
var currencyDropdown = new List<string>();
currencyDropdown.Add(DEFAULT_CURRENCY);
currencyDropdown.AddRange(paymentMethods);
return currencyDropdown;
}
[HttpPost]
[Route("{appId}/pay")]
[IgnoreAntiforgeryToken]
[EnableCors(CorsPolicies.All)]
public async Task<IActionResult> 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();
}
}
}

View File

@ -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<ApplicationUser> _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<IActionResult> 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<string> supportedCurrencies(StoreData store)
{
var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
.Select(a => a.PaymentId.ToString()).ToList();
var currencyDropdown = new List<string>();
currencyDropdown.Add(DEFAULT_CURRENCY);
currencyDropdown.AddRange(paymentMethods);
return currencyDropdown;
}
[HttpPost]
[Route("{storeId}/pay")]
[IgnoreAntiforgeryToken]
[EnableCors(CorsPolicies.All)]
public async Task<IActionResult> 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();
}
}
}

View File

@ -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; }

View File

@ -1,6 +1,7 @@
@model PayButtonViewModel
@{
ViewData["Title"] = "Pay Button";
ViewData.SetActivePageAndTitle(StoreNavPages.PayButton, "Pay Button");
}
<section>