btcpayserver/BTCPayServer/Plugins/BoltcardBalance/Controllers/UIBoltcardBalanceController.cs

128 lines
5.4 KiB
C#
Raw Normal View History

2024-02-14 17:00:03 +09:00
using System;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using AngleSharp.Dom;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
2024-03-05 10:39:11 +09:00
using BTCPayServer.Controllers.Greenfield;
2024-02-14 17:00:03 +09:00
using BTCPayServer.Data;
using BTCPayServer.HostedServices;
2024-03-05 10:39:11 +09:00
using BTCPayServer.Models;
2024-02-14 17:00:03 +09:00
using BTCPayServer.Plugins.BoltcardBalance.ViewModels;
2024-02-14 16:45:03 +09:00
using BTCPayServer.Plugins.BoltcardFactory;
2024-02-14 17:00:03 +09:00
using BTCPayServer.Services;
2024-02-14 16:45:03 +09:00
using Microsoft.AspNetCore.Mvc;
2024-02-14 17:00:03 +09:00
using Microsoft.EntityFrameworkCore;
2024-02-14 16:45:03 +09:00
namespace BTCPayServer.Plugins.BoltcardBalance.Controllers
{
[AutoValidateAntiforgeryToken]
public class UIBoltcardBalanceController : Controller
{
2024-02-14 17:00:03 +09:00
private readonly ApplicationDbContextFactory _dbContextFactory;
private readonly SettingsRepository _settingsRepository;
private readonly BTCPayServerEnvironment _env;
private readonly BTCPayNetworkJsonSerializerSettings _serializerSettings;
public UIBoltcardBalanceController(
ApplicationDbContextFactory dbContextFactory,
SettingsRepository settingsRepository,
BTCPayServerEnvironment env,
BTCPayNetworkJsonSerializerSettings serializerSettings)
{
_dbContextFactory = dbContextFactory;
_settingsRepository = settingsRepository;
_env = env;
_serializerSettings = serializerSettings;
}
2024-02-14 16:45:03 +09:00
[HttpGet("boltcards/balance")]
2024-02-14 17:00:03 +09:00
public async Task<IActionResult> ScanCard([FromQuery] string p = null, [FromQuery] string c = null)
2024-02-14 16:45:03 +09:00
{
2024-02-14 17:00:03 +09:00
if (p is null || c is null)
{
return View($"{BoltcardBalancePlugin.ViewsDirectory}/ScanCard.cshtml");
}
//return View($"{BoltcardBalancePlugin.ViewsDirectory}/BalanceView.cshtml", new BalanceViewModel()
//{
// AmountDue = 10000m,
// Currency = "SATS",
// Transactions = [new() { Date = DateTimeOffset.UtcNow, Balance = -3.0m }, new() { Date = DateTimeOffset.UtcNow, Balance = -5.0m }]
//});
var issuerKey = await _settingsRepository.GetIssuerKey(_env);
var boltData = issuerKey.TryDecrypt(p);
if (boltData?.Uid is null)
return NotFound();
var id = issuerKey.GetId(boltData.Uid);
var registration = await _dbContextFactory.GetBoltcardRegistration(issuerKey, boltData, true);
if (registration is null)
return NotFound();
2024-02-21 17:45:15 +09:00
return await GetBalanceView(registration.PullPaymentId, p);
2024-02-19 12:23:46 +09:00
}
[NonAction]
2024-02-21 17:45:15 +09:00
public async Task<IActionResult> GetBalanceView(string ppId, string p)
2024-02-19 12:23:46 +09:00
{
2024-02-14 17:00:03 +09:00
using var ctx = _dbContextFactory.CreateContext();
2024-02-19 12:23:46 +09:00
var pp = await ctx.PullPayments.FindAsync(ppId);
2024-02-14 17:00:03 +09:00
if (pp is null)
return NotFound();
var blob = pp.GetBlob();
var payouts = (await ctx.Payouts.GetPayoutInPeriod(pp)
.OrderByDescending(o => o.Date)
.ToListAsync())
.Select(o => new
{
Entity = o,
Blob = o.GetBlob(_serializerSettings)
});
var totalPaid = payouts.Where(p => p.Entity.State != PayoutState.Cancelled).Select(p => p.Blob.Amount).Sum();
2024-02-21 18:58:38 +09:00
var bech32LNUrl = new Uri(Url.Action(nameof(UIBoltcardController.GetPayRequest), "UIBoltcard", new { p }, Request.Scheme), UriKind.Absolute);
bech32LNUrl = LNURL.LNURL.EncodeUri(bech32LNUrl, "payRequest", true);
2024-02-14 17:00:03 +09:00
var vm = new BalanceViewModel()
{
Currency = blob.Currency,
2024-02-21 17:45:15 +09:00
AmountDue = blob.Limit - totalPaid,
2024-02-21 18:58:38 +09:00
LNUrlBech32 = bech32LNUrl.AbsoluteUri,
2024-03-05 10:39:11 +09:00
LNUrlPay = Url.Action(nameof(UIBoltcardController.GetPayRequest), "UIBoltcard", new { p }, "lnurlp"),
2024-03-08 09:40:42 +01:00
BoltcardKeysResetLink = $"boltcard://reset?url={GetBoltcardDeeplinkUrl(pp.Id, OnExistingBehavior.KeepVersion)}",
PullPaymentLink = Url.Action(nameof(UIPullPaymentController.ViewPullPayment), "UIPullPayment", new { pullPaymentId = pp.Id }, Request.Scheme, Request.Host.ToString())
2024-02-14 17:00:03 +09:00
};
foreach (var payout in payouts)
{
vm.Transactions.Add(new BalanceViewModel.Transaction()
{
Date = payout.Entity.Date,
Balance = -payout.Blob.Amount,
Status = payout.Entity.State
});
}
2024-02-19 12:23:46 +09:00
vm.Transactions.Add(new BalanceViewModel.Transaction()
{
Date = pp.StartDate,
Balance = blob.Limit,
Status = PayoutState.Completed
});
2024-02-14 17:00:03 +09:00
return View($"{BoltcardBalancePlugin.ViewsDirectory}/BalanceView.cshtml", vm);
2024-02-14 16:45:03 +09:00
}
2024-03-05 10:39:11 +09:00
private string GetBoltcardDeeplinkUrl(string ppId, OnExistingBehavior onExisting)
{
var registerUrl = Url.Action(nameof(GreenfieldPullPaymentController.RegisterBoltcard), "GreenfieldPullPayment",
new
{
pullPaymentId = ppId,
onExisting = onExisting.ToString()
}, Request.Scheme, Request.Host.ToString());
registerUrl = Uri.EscapeDataString(registerUrl);
return registerUrl;
}
2024-02-14 16:45:03 +09:00
}
}