Remove payment methods not currently configured when creating invoice (#3394)

* Add error message when wallet is not configured

* Adjust payment methods based on available ones

* Disable "Create invoice" page if there is an error

* Add test

* update HasErrorMessage

* Add method for checking if payment methods are available

* small pr fixes

Co-authored-by: Kukks <evilkukka@gmail.com>
This commit is contained in:
Umar Bolatov 2022-02-17 01:22:09 -08:00 committed by GitHub
parent 9a3a7a3444
commit a3b748ffe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 6 deletions

View File

@ -23,7 +23,7 @@
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" /> <PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="Selenium.Support" Version="3.141.0" /> <PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" /> <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="96.0.4664.4500" /> <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="98.0.4758.10200" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -386,6 +386,10 @@ namespace BTCPayServer.Tests
await s.StartAsync(); await s.StartAsync();
s.RegisterNewUser(true); s.RegisterNewUser(true);
s.CreateNewStore(); s.CreateNewStore();
s.GoToInvoices();
s.Driver.FindElement(By.Id("CreateNewInvoice")).Click();
// Should give us an error message if we try to create an invoice before adding a wallet
Assert.Contains("To create an invoice, you need to", s.Driver.PageSource);
s.AddDerivationScheme(); s.AddDerivationScheme();
s.GoToInvoices(); s.GoToInvoices();
s.CreateInvoice(); s.CreateInvoice();

View File

@ -883,13 +883,24 @@ namespace BTCPayServer.Controllers
private SelectList GetPaymentMethodsSelectList() private SelectList GetPaymentMethodsSelectList()
{ {
return new SelectList(_paymentMethodHandlerDictionary.Distinct().SelectMany(handler => var store = GetCurrentStore();
handler.GetSupportedPaymentMethods() var excludeFilter = store.GetStoreBlob().GetExcludedPaymentMethods();
.Select(id => new SelectListItem(id.ToPrettyString(), id.ToString()))),
return new SelectList(store.GetSupportedPaymentMethods(_NetworkProvider)
.Where(s => !excludeFilter.Match(s.PaymentId))
.Select(method => new SelectListItem(method.PaymentId.ToPrettyString(), method.PaymentId.ToString())),
nameof(SelectListItem.Value), nameof(SelectListItem.Value),
nameof(SelectListItem.Text)); nameof(SelectListItem.Text));
} }
private bool AnyPaymentMethodAvailable(StoreData store)
{
var storeBlob = store.GetStoreBlob();
var excludeFilter = storeBlob.GetExcludedPaymentMethods();
return store.GetSupportedPaymentMethods(_NetworkProvider).Where(s => !excludeFilter.Match(s.PaymentId)).Any();
}
[HttpGet("/stores/{storeId}/invoices/create")] [HttpGet("/stores/{storeId}/invoices/create")]
[HttpGet("invoices/create")] [HttpGet("invoices/create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
@ -913,6 +924,17 @@ namespace BTCPayServer.Controllers
var store = await _StoreRepository.FindStore(model.StoreId, GetUserId()); var store = await _StoreRepository.FindStore(model.StoreId, GetUserId());
if (store == null) if (store == null)
return NotFound(); return NotFound();
if (!AnyPaymentMethodAvailable(store))
{
TempData.SetStatusMessageModel(new StatusMessageModel
{
Severity = StatusMessageModel.StatusSeverity.Error,
Html = $"To create an invoice, you need to <a href='{Url.Action(nameof(UIStoresController.SetupWallet), "UIStores", new { cryptoCode = _NetworkProvider.DefaultNetwork.CryptoCode, storeId = store.Id })}' class='alert-link'>set up a wallet</a> first",
AllowDismiss = false
});
}
HttpContext.SetStoreData(store); HttpContext.SetStoreData(store);
} }
@ -941,12 +963,12 @@ namespace BTCPayServer.Controllers
return View(model); return View(model);
} }
if (!store.GetSupportedPaymentMethods(_NetworkProvider).Any()) if (!AnyPaymentMethodAvailable(store))
{ {
TempData.SetStatusMessageModel(new StatusMessageModel TempData.SetStatusMessageModel(new StatusMessageModel
{ {
Severity = StatusMessageModel.StatusSeverity.Error, Severity = StatusMessageModel.StatusSeverity.Error,
Html = $"To create an invoice, you need to <a href='{Url.Action(nameof(UIStoresController.GeneralSettings), "UIStores", new { storeId = store.Id })}' class='alert-link'>set up a wallet</a> first", Html = $"To create an invoice, you need to <a href='{Url.Action(nameof(UIStoresController.SetupWallet), "UIStores", new { cryptoCode = _NetworkProvider.DefaultNetwork.CryptoCode, storeId = store.Id })}' class='alert-link'>set up a wallet</a> first",
AllowDismiss = false AllowDismiss = false
}); });
return View(model); return View(model);

View File

@ -115,6 +115,12 @@ namespace BTCPayServer
tempData.Peek(WellKnownTempData.ErrorMessage) ?? tempData.Peek(WellKnownTempData.ErrorMessage) ??
tempData.Peek("StatusMessageModel")) != null; tempData.Peek("StatusMessageModel")) != null;
} }
public static bool HasErrorMessage(this ITempDataDictionary tempData)
{
return GetStatusMessageModel(tempData)?.Severity == StatusMessageModel.StatusSeverity.Error;
}
public static PaymentMethodId GetpaymentMethodId(this InvoiceCryptoInfo info) public static PaymentMethodId GetpaymentMethodId(this InvoiceCryptoInfo info)
{ {
return new PaymentMethodId(info.CryptoCode, PaymentTypes.Parse(info.PaymentType)); return new PaymentMethodId(info.CryptoCode, PaymentTypes.Parse(info.PaymentType));