Add cart items to receipt (#4851)

This commit is contained in:
Andrew Camilleri 2023-04-05 15:42:23 +02:00 committed by GitHub
parent 929d63ecf8
commit 5ab55e71e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,6 +20,7 @@ using BTCPayServer.Forms.Models;
using BTCPayServer.ModelBinders;
using BTCPayServer.Models;
using BTCPayServer.Plugins.PointOfSale.Models;
using BTCPayServer.Services;
using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
@ -46,12 +47,14 @@ namespace BTCPayServer.Plugins.PointOfSale.Controllers
CurrencyNameTable currencies,
StoreRepository storeRepository,
UIInvoiceController invoiceController,
FormDataService formDataService)
FormDataService formDataService,
DisplayFormatter displayFormatter)
{
_currencies = currencies;
_appService = appService;
_storeRepository = storeRepository;
_invoiceController = invoiceController;
_displayFormatter = displayFormatter;
FormDataService = formDataService;
}
@ -59,6 +62,7 @@ namespace BTCPayServer.Plugins.PointOfSale.Controllers
private readonly StoreRepository _storeRepository;
private readonly AppService _appService;
private readonly UIInvoiceController _invoiceController;
private readonly DisplayFormatter _displayFormatter;
public FormDataService FormDataService { get; }
[HttpGet("/")]
@ -157,9 +161,11 @@ namespace BTCPayServer.Plugins.PointOfSale.Controllers
decimal? price;
Dictionary<string, InvoiceSupportedTransactionCurrency> paymentMethods = null;
ViewPointOfSaleViewModel.Item choice = null;
Dictionary<string, int> cartItems = null;
ViewPointOfSaleViewModel.Item[] choices = null;
if (!string.IsNullOrEmpty(choiceKey))
{
var choices = _appService.GetPOSItems(settings.Template, settings.Currency);
choices = _appService.GetPOSItems(settings.Template, settings.Currency);
choice = choices.FirstOrDefault(c => c.Id == choiceKey);
if (choice == null)
return NotFound();
@ -194,10 +200,11 @@ namespace BTCPayServer.Plugins.PointOfSale.Controllers
price = amount;
title = settings.Title;
//if cart IS enabled and we detect posdata that matches the cart system's, check inventory for the items
if (currentView == PosViewType.Cart &&
AppService.TryParsePosCartItems(jposData, out var cartItems))
AppService.TryParsePosCartItems(jposData, out cartItems))
{
var choices = _appService.GetPOSItems(settings.Template, settings.Currency);
choices = _appService.GetPOSItems(settings.Template, settings.Currency);
var expectedMinimumAmount = 0m;
foreach (var cartItem in cartItems)
{
@ -298,6 +305,50 @@ namespace BTCPayServer.Plugins.PointOfSale.Controllers
{
entity.Metadata.OrderUrl = Request.GetDisplayUrl();
entity.Metadata.PosData = jposData;
var receiptData = new JObject();
if (choice is not null)
{
receiptData = JObject.FromObject(new Dictionary<string, string>()
{
{"Title", choice.Title}, {"Description", choice.Description},
});
}
else if (jposData is not null)
{
var appPosData = jposData.ToObject<PosAppData>();
receiptData = new JObject();
if (cartItems is not null && choices is not null)
{
var selectedChoices = choices.Where(item => cartItems.Keys.Contains(item.Id))
.ToDictionary(item => item.Id);
var cartData = new JObject();
foreach (KeyValuePair<string, int> cartItem in cartItems)
{
if (selectedChoices.TryGetValue(cartItem.Key, out var selectedChoice))
{
cartData.Add(selectedChoice.Title ?? selectedChoice.Id,
$"{(selectedChoice.Price.Value is null ? "Any price" : $"{_displayFormatter.Currency((decimal)selectedChoice.Price.Value, settings.Currency, DisplayFormatter.CurrencyFormat.Symbol)}")} x {cartItem.Value} = {(selectedChoice.Price.Value is null ? "Any price" : $"{_displayFormatter.Currency(((decimal)selectedChoice.Price.Value) * cartItem.Value, settings.Currency, DisplayFormatter.CurrencyFormat.Symbol)}")}");
}
}
receiptData.Add("Cart", cartData);
}
if (appPosData.DiscountAmount > 0)
{
receiptData.Add("Discount",
$"{_displayFormatter.Currency(appPosData.DiscountAmount, settings.Currency, DisplayFormatter.CurrencyFormat.Symbol) } {(appPosData.DiscountPercentage > 0 ? $"({appPosData.DiscountPercentage}%)" : string.Empty)}");
}
if (appPosData.Tip > 0)
{
receiptData.Add("Tip",
$"{_displayFormatter.Currency(appPosData.Tip, settings.Currency, DisplayFormatter.CurrencyFormat.Symbol) }");
}
}
entity.Metadata.SetAdditionalData("receiptData", receiptData);
if (formResponseJObject is null) return;
var meta = entity.Metadata.ToJObject();
meta.Merge(formResponseJObject);