Separate app for PayButton

This commit is contained in:
rockstardev 2018-08-22 10:26:49 +02:00
parent 09beb57eaf
commit b1fcf4524a
7 changed files with 112 additions and 95 deletions

View file

@ -119,7 +119,7 @@
<Content Update="Views\Server\SSHService.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButton.cshtml">
<Content Update="Views\Apps\ViewPayButton.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Views\Apps\PayButtonHandle.cshtml">

View file

@ -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<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
};
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 settings = app.GetSettings<PointOfSaleSettings>();
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

@ -263,14 +263,6 @@ namespace BTCPayServer.Controllers
return Redirect(invoice.Data.Url);
}
private async Task<StoreData> 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<IActionResult> PayButton(string appId)
{
var app = await GetApp(appId, AppType.PointOfSale);
if (app == null)
return NotFound();
var settings = app.GetSettings<PointOfSaleSettings>();
var store = await GetStore(app);
List<string> 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<string> supportedCurrencies(PointOfSaleSettings settings, StoreData store)
{
var paymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
.Select(a => a.PaymentId.ToString()).ToList();
var currencyDropdown = new List<string>();
currencyDropdown.Add(settings.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.PointOfSale);
var settings = app.GetSettings<PointOfSaleSettings>();
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();
}
}
}

View file

@ -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<StoreData> GetStore(AppData app)
{
using (var ctx = _ContextFactory.CreateContext())
{
return await ctx.Stores.FirstOrDefaultAsync(s => s.Id == app.StoreDataId);
}
}
}
}

View file

@ -7,6 +7,7 @@ namespace BTCPayServer.Services.Apps
{
public enum AppType
{
PointOfSale
PointOfSale,
PayButton
}
}

View file

@ -1,4 +1,5 @@
@model ListAppsViewModel
@using BTCPayServer.Services.Apps
@model ListAppsViewModel
@{
ViewData["Title"] = "Stores";
}
@ -48,10 +49,9 @@
<td>@app.AppName</td>
<td>@app.AppType</td>
<td style="text-align:right">
@if (app.IsOwner)
@if (app.IsOwner && app.AppType != AppType.PayButton.ToString())
{
<a asp-action="@app.UpdateAction" asp-controller="Apps" asp-route-appId="@app.Id">Settings</a><span> - </span>
<a asp-action="paybutton" asp-controller="Apps" asp-route-appId="@app.Id">Pay Button</a><span> - </span>
}
<a asp-action="@app.ViewAction" asp-controller="Apps" asp-route-appId="@app.Id">View</a><span> - </span>
<a asp-action="DeleteApp" asp-route-appId="@app.Id">Remove</a>