2018-08-30 20:16:24 +02:00
|
|
|
|
using System;
|
2018-11-16 04:31:38 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-08-30 20:16:24 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2018-09-08 07:32:26 +02:00
|
|
|
|
using System.Security.Claims;
|
2018-08-30 20:16:24 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using BTCPayServer.Models.AppViewModels;
|
2018-09-08 07:32:26 +02:00
|
|
|
|
using BTCPayServer.Security;
|
2018-08-30 20:16:24 +02:00
|
|
|
|
using BTCPayServer.Services.Apps;
|
|
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using YamlDotNet.RepresentationModel;
|
|
|
|
|
using static BTCPayServer.Controllers.AppsController;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AppsPublicController : Controller
|
|
|
|
|
{
|
|
|
|
|
public AppsPublicController(AppsHelper appsHelper, InvoiceController invoiceController)
|
|
|
|
|
{
|
|
|
|
|
_AppsHelper = appsHelper;
|
|
|
|
|
_InvoiceController = invoiceController;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AppsHelper _AppsHelper;
|
|
|
|
|
private InvoiceController _InvoiceController;
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/apps/{appId}/pos")]
|
|
|
|
|
public async Task<IActionResult> ViewPointOfSale(string appId)
|
|
|
|
|
{
|
|
|
|
|
var app = await _AppsHelper.GetApp(appId, AppType.PointOfSale);
|
|
|
|
|
if (app == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
var settings = app.GetSettings<PointOfSaleSettings>();
|
|
|
|
|
var currency = _AppsHelper.GetCurrencyData(settings.Currency, false);
|
|
|
|
|
double step = currency == null ? 1 : Math.Pow(10, -(currency.Divisibility));
|
|
|
|
|
|
|
|
|
|
return View(new ViewPointOfSaleViewModel()
|
|
|
|
|
{
|
|
|
|
|
Title = settings.Title,
|
|
|
|
|
Step = step.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
ShowCustomAmount = settings.ShowCustomAmount,
|
2018-11-16 04:31:38 +01:00
|
|
|
|
CurrencySymbol = currency.Symbol,
|
2018-11-17 03:39:43 +01:00
|
|
|
|
Items = _AppsHelper.Parse(settings.Template, settings.Currency),
|
|
|
|
|
ButtonText = settings.ButtonText,
|
|
|
|
|
CustomButtonText = settings.CustomButtonText,
|
|
|
|
|
CustomCSSLink = settings.CustomCSSLink
|
2018-08-30 20:16:24 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2018-09-08 07:32:26 +02:00
|
|
|
|
|
2018-08-30 20:16:24 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("/apps/{appId}/pos")]
|
|
|
|
|
[IgnoreAntiforgeryToken]
|
|
|
|
|
[EnableCors(CorsPolicies.All)]
|
|
|
|
|
public async Task<IActionResult> ViewPointOfSale(string appId,
|
|
|
|
|
decimal amount,
|
|
|
|
|
string email,
|
|
|
|
|
string orderId,
|
|
|
|
|
string notificationUrl,
|
|
|
|
|
string redirectUrl,
|
|
|
|
|
string choiceKey)
|
|
|
|
|
{
|
|
|
|
|
var app = await _AppsHelper.GetApp(appId, AppType.PointOfSale);
|
|
|
|
|
if (string.IsNullOrEmpty(choiceKey) && amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction(nameof(ViewPointOfSale), new { appId = appId });
|
|
|
|
|
}
|
|
|
|
|
if (app == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
var settings = app.GetSettings<PointOfSaleSettings>();
|
|
|
|
|
if (string.IsNullOrEmpty(choiceKey) && !settings.ShowCustomAmount)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction(nameof(ViewPointOfSale), new { appId = appId });
|
|
|
|
|
}
|
|
|
|
|
string title = null;
|
|
|
|
|
var price = 0.0m;
|
|
|
|
|
if (!string.IsNullOrEmpty(choiceKey))
|
|
|
|
|
{
|
|
|
|
|
var choices = _AppsHelper.Parse(settings.Template, settings.Currency);
|
|
|
|
|
var choice = choices.FirstOrDefault(c => c.Id == choiceKey);
|
|
|
|
|
if (choice == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
title = choice.Title;
|
|
|
|
|
price = choice.Price.Value;
|
2018-11-17 03:39:43 +01:00
|
|
|
|
if (amount > price)
|
|
|
|
|
price = amount;
|
2018-08-30 20:16:24 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!settings.ShowCustomAmount)
|
|
|
|
|
return NotFound();
|
|
|
|
|
price = amount;
|
|
|
|
|
title = settings.Title;
|
|
|
|
|
}
|
|
|
|
|
var store = await _AppsHelper.GetStore(app);
|
2018-09-08 07:32:26 +02:00
|
|
|
|
store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id));
|
2018-08-30 20:16:24 +02:00
|
|
|
|
var invoice = await _InvoiceController.CreateInvoiceCore(new NBitpayClient.Invoice()
|
|
|
|
|
{
|
2018-11-16 15:16:44 +01:00
|
|
|
|
ItemCode = choiceKey ?? string.Empty,
|
2018-08-30 20:16:24 +02:00
|
|
|
|
ItemDesc = title,
|
|
|
|
|
Currency = settings.Currency,
|
|
|
|
|
Price = price,
|
|
|
|
|
BuyerEmail = email,
|
|
|
|
|
OrderId = orderId,
|
|
|
|
|
NotificationURL = notificationUrl,
|
|
|
|
|
RedirectURL = redirectUrl,
|
|
|
|
|
FullNotifications = true
|
|
|
|
|
}, store, HttpContext.Request.GetAbsoluteRoot());
|
|
|
|
|
return Redirect(invoice.Data.Url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class AppsHelper
|
|
|
|
|
{
|
|
|
|
|
ApplicationDbContextFactory _ContextFactory;
|
|
|
|
|
CurrencyNameTable _Currencies;
|
|
|
|
|
|
|
|
|
|
public AppsHelper(ApplicationDbContextFactory contextFactory, CurrencyNameTable currencies)
|
|
|
|
|
{
|
|
|
|
|
_ContextFactory = contextFactory;
|
|
|
|
|
_Currencies = currencies;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AppData> GetApp(string appId, AppType appType)
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
return await ctx.Apps
|
|
|
|
|
.Where(us => us.Id == appId &&
|
|
|
|
|
us.AppType == appType.ToString())
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<StoreData> GetStore(AppData app)
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
return await ctx.Stores.FirstOrDefaultAsync(s => s.Id == app.StoreDataId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 04:31:38 +01:00
|
|
|
|
|
2018-08-30 20:16:24 +02:00
|
|
|
|
public ViewPointOfSaleViewModel.Item[] Parse(string template, string currency)
|
|
|
|
|
{
|
2018-11-13 08:32:13 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(template))
|
|
|
|
|
return Array.Empty<ViewPointOfSaleViewModel.Item>();
|
2018-08-30 20:16:24 +02:00
|
|
|
|
var input = new StringReader(template);
|
|
|
|
|
YamlStream stream = new YamlStream();
|
|
|
|
|
stream.Load(input);
|
|
|
|
|
var root = (YamlMappingNode)stream.Documents[0].RootNode;
|
|
|
|
|
return root
|
|
|
|
|
.Children
|
2018-11-16 04:31:38 +01:00
|
|
|
|
.Select(kv => new PosHolder { Key = (kv.Key as YamlScalarNode)?.Value, Value = kv.Value as YamlMappingNode })
|
2018-08-30 20:16:24 +02:00
|
|
|
|
.Where(kv => kv.Value != null)
|
|
|
|
|
.Select(c => new ViewPointOfSaleViewModel.Item()
|
|
|
|
|
{
|
2018-11-16 04:31:38 +01:00
|
|
|
|
Description = c.GetDetailString("description"),
|
2018-08-30 20:16:24 +02:00
|
|
|
|
Id = c.Key,
|
2018-11-16 04:31:38 +01:00
|
|
|
|
Image = c.GetDetailString("image"),
|
|
|
|
|
Title = c.GetDetailString("title") ?? c.Key,
|
|
|
|
|
Price = c.GetDetail("price")
|
2018-08-30 20:16:24 +02:00
|
|
|
|
.Select(cc => new ViewPointOfSaleViewModel.Item.ItemPrice()
|
|
|
|
|
{
|
|
|
|
|
Value = decimal.Parse(cc.Value.Value, CultureInfo.InvariantCulture),
|
|
|
|
|
Formatted = FormatCurrency(cc.Value.Value, currency)
|
2018-11-16 04:31:38 +01:00
|
|
|
|
}).Single(),
|
|
|
|
|
Custom = c.GetDetailString("custom") == "true"
|
2018-08-30 20:16:24 +02:00
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 04:31:38 +01:00
|
|
|
|
private class PosHolder
|
|
|
|
|
{
|
|
|
|
|
public string Key { get; set; }
|
|
|
|
|
public YamlMappingNode Value { get; set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PosScalar> GetDetail(string field)
|
|
|
|
|
{
|
|
|
|
|
var res = Value.Children
|
|
|
|
|
.Where(kv => kv.Value != null)
|
|
|
|
|
.Select(kv => new PosScalar { Key = (kv.Key as YamlScalarNode)?.Value, Value = kv.Value as YamlScalarNode })
|
|
|
|
|
.Where(cc => cc.Key == field);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetDetailString(string field)
|
|
|
|
|
{
|
|
|
|
|
return GetDetail(field).FirstOrDefault()?.Value?.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private class PosScalar
|
|
|
|
|
{
|
|
|
|
|
public string Key { get; set; }
|
|
|
|
|
public YamlScalarNode Value { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 20:16:24 +02:00
|
|
|
|
public string FormatCurrency(string price, string currency)
|
|
|
|
|
{
|
|
|
|
|
return decimal.Parse(price, CultureInfo.InvariantCulture).ToString("C", _Currencies.GetCurrencyProvider(currency));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CurrencyData GetCurrencyData(string currency, bool useFallback)
|
|
|
|
|
{
|
|
|
|
|
return _Currencies.GetCurrencyData(currency, useFallback);
|
|
|
|
|
}
|
2018-10-09 16:34:51 +02:00
|
|
|
|
public async Task<AppData> GetAppDataIfOwner(string userId, string appId, AppType? type = null)
|
|
|
|
|
{
|
|
|
|
|
if (userId == null || appId == null)
|
|
|
|
|
return null;
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var app = await ctx.UserStore
|
|
|
|
|
.Where(us => us.ApplicationUserId == userId && us.Role == StoreRoles.Owner)
|
|
|
|
|
.SelectMany(us => us.StoreData.Apps.Where(a => a.Id == appId))
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
if (app == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (type != null && type.Value.ToString() != app.AppType)
|
|
|
|
|
return null;
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-30 20:16:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|