Fix Bringin plugin crash (#95)

This commit is contained in:
nicolas.dorier
2025-06-27 09:10:29 +09:00
parent 91908c1e7d
commit 10438f9a9c

View File

@@ -1,14 +1,17 @@
@using System.Threading @using System.Security.Claims
@using System.Threading
@using BTCPayServer.Abstractions.Extensions @using BTCPayServer.Abstractions.Extensions
@using BTCPayServer.Client @using BTCPayServer.Client
@using BTCPayServer.Configuration
@using BTCPayServer.Data @using BTCPayServer.Data
@using BTCPayServer.Payments @using BTCPayServer.Payments
@using BTCPayServer.PayoutProcessors @using BTCPayServer.PayoutProcessors
@using BTCPayServer.Payouts @using BTCPayServer.Payouts
@using BTCPayServer.Services @using BTCPayServer.Services
@using BTCPayServer.Services.Invoices
@using BTCPayServer.Services.Stores @using BTCPayServer.Services.Stores
@using Microsoft.AspNetCore.Http @using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Routing @using Microsoft.AspNetCore.Routing
@using NBitcoin @using NBitcoin
@implements IAsyncDisposable; @implements IAsyncDisposable;
@@ -18,7 +21,10 @@
private bool _isLoaded = false; private bool _isLoaded = false;
private CancellationTokenSource _cts = new CancellationTokenSource(); private CancellationTokenSource _cts = new CancellationTokenSource();
[Inject] private IHttpContextAccessor HttpContextAccessor { get; set; }
[Inject] BTCPayServerOptions BTCPayServerOptions { get; set; }
[Inject] private AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject] UserManager<ApplicationUser> UserManager { get; set; }
[Inject] private DisplayFormatter DisplayFormatter { get; set; } [Inject] private DisplayFormatter DisplayFormatter { get; set; }
[Inject] private BringinService BringinService { get; set; } [Inject] private BringinService BringinService { get; set; }
[Inject] private LinkGenerator LinkGenerator { get; set; } [Inject] private LinkGenerator LinkGenerator { get; set; }
@@ -73,14 +79,19 @@
private bool _editMode; private bool _editMode;
private bool _readOnly; private bool _readOnly;
async Task<ClaimsPrincipal> GetUser()
{
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.Identity?.IsAuthenticated is not true)
return null;
return state.User;
}
protected override async Task OnAfterRenderAsync(bool firstRender) protected override async Task OnAfterRenderAsync(bool firstRender)
{ {
if (firstRender) if (firstRender)
{ {
_readOnly = !(await AuthorizationService.AuthorizeAsync(HttpContextAccessor.HttpContext.User, StoreId, Policies.CanModifyStoreSettings)).Succeeded; _readOnly = !(await AuthorizationService.AuthorizeAsync(await GetUser(), StoreId, Policies.CanModifyStoreSettings)).Succeeded;
OnboardLink = LinkGenerator.GetUriByAction(HttpContextAccessor.HttpContext, "Onboard", "Bringin", new {StoreId}); OnboardLink = LinkGenerator.GetPathByAction("Onboard", "Bringin", new {StoreId}, pathBase: BTCPayServerOptions.RootPath);
PmiLink = $"A payout processor has not been configured for this payment method. Payouts generated by Bringin will not be automatically handled. <a href=\"{LinkGenerator.GetUriByAction(HttpContextAccessor.HttpContext, "ConfigureStorePayoutProcessors", "UIPayoutProcessors", new {StoreId})}\">Configure now</a>";
_callbackLink = LinkGenerator.GetUriByAction(HttpContextAccessor.HttpContext, "Callback", "Bringin", new {StoreId});
_settings = BringinService.IsInEditMode(StoreId) ? await BringinService.Update(StoreId) : await BringinService.Get(StoreId); _settings = BringinService.IsInEditMode(StoreId) ? await BringinService.Update(StoreId) : await BringinService.Get(StoreId);
var store = await StoreRepository.FindStore(StoreId); var store = await StoreRepository.FindStore(StoreId);
_pms = PayoutMethodHandlerDictionary.GetSupportedPayoutMethods(store); _pms = PayoutMethodHandlerDictionary.GetSupportedPayoutMethods(store);
@@ -98,8 +109,6 @@
await base.OnAfterRenderAsync(firstRender); await base.OnAfterRenderAsync(firstRender);
} }
private string _callbackLink;
private void EditModeChanged(object sender, (string storeId, bool editMode) e) private void EditModeChanged(object sender, (string storeId, bool editMode) e)
{ {
if (e.storeId != StoreId) if (e.storeId != StoreId)
@@ -302,8 +311,6 @@
// private bool ManualOrderPayout = true; // private bool ManualOrderPayout = true;
public string PmiLink;
public string OnboardLink; public string OnboardLink;
private async void ResetBalance(PaymentMethodId pmi) private async void ResetBalance(PaymentMethodId pmi)
@@ -521,9 +528,9 @@
<span class="text-secondary fw-semibold">pending payouts</span> <span class="text-secondary fw-semibold">pending payouts</span>
</div> </div>
} }
@if (!_pps.Contains(pmi) && PmiLink is not null) @if (!_pps.Contains(pmi))
{ {
<p class="text-warning">@((MarkupString) PmiLink)</p> <p class="text-warning">A payout processor has not been configured for this payment method. Payouts generated by Bringin will not be automatically handled. <a href="@LinkGenerator.GetPathByAction("ConfigureStorePayoutProcessors", "UIPayoutProcessors", new {StoreId}, pathBase: BTCPayServerOptions.RootPath)">Configure now</a></p>
} }
</div> </div>
} }