Greenfield: Apply store default payment method on invoice creation

Fixes #4947.
This commit is contained in:
Dennis Reimann 2023-05-04 15:14:15 +02:00 committed by Andrew Camilleri
parent eddd458744
commit b9b11e722c
2 changed files with 30 additions and 7 deletions

View File

@ -2257,7 +2257,7 @@ namespace BTCPayServer.Tests
Assert.Single(paymentMethods);
Assert.True(paymentMethods.First().Activated);
var invoiceWithdefaultPaymentMethodLN = await client.CreateInvoice(user.StoreId,
var invoiceWithDefaultPaymentMethodLN = await client.CreateInvoice(user.StoreId,
new CreateInvoiceRequest()
{
Currency = "USD",
@ -2268,9 +2268,9 @@ namespace BTCPayServer.Tests
DefaultPaymentMethod = "BTC_LightningLike"
}
});
Assert.Equal("BTC_LightningLike", invoiceWithdefaultPaymentMethodLN.Checkout.DefaultPaymentMethod);
Assert.Equal("BTC_LightningLike", invoiceWithDefaultPaymentMethodLN.Checkout.DefaultPaymentMethod);
var invoiceWithdefaultPaymentMethodOnChain = await client.CreateInvoice(user.StoreId,
var invoiceWithDefaultPaymentMethodOnChain = await client.CreateInvoice(user.StoreId,
new CreateInvoiceRequest()
{
Currency = "USD",
@ -2281,13 +2281,35 @@ namespace BTCPayServer.Tests
DefaultPaymentMethod = "BTC"
}
});
Assert.Equal("BTC", invoiceWithdefaultPaymentMethodOnChain.Checkout.DefaultPaymentMethod);
Assert.Equal("BTC", invoiceWithDefaultPaymentMethodOnChain.Checkout.DefaultPaymentMethod);
// reset lazy payment methods
store = await client.GetStore(user.StoreId);
store.LazyPaymentMethods = false;
store = await client.UpdateStore(store.Id,
JObject.FromObject(store).ToObject<UpdateStoreRequest>());
Assert.False(store.LazyPaymentMethods);
// use store default payment method
store = await client.GetStore(user.StoreId);
Assert.Null(store.DefaultPaymentMethod);
var storeDefaultPaymentMethod = "BTC-LightningNetwork";
store.DefaultPaymentMethod = storeDefaultPaymentMethod;
store = await client.UpdateStore(store.Id,
JObject.FromObject(store).ToObject<UpdateStoreRequest>());
Assert.Equal(storeDefaultPaymentMethod, store.DefaultPaymentMethod);
var invoiceWithStoreDefaultPaymentMethod = await client.CreateInvoice(user.StoreId,
new CreateInvoiceRequest()
{
Currency = "USD",
Amount = 100,
Checkout = new CreateInvoiceRequest.CheckoutOptions()
{
PaymentMethods = new[] { "BTC", "BTC-LightningNetwork", "BTC_LightningLike" }
}
});
Assert.Equal(storeDefaultPaymentMethod, invoiceWithStoreDefaultPaymentMethod.Checkout.DefaultPaymentMethod);
//let's see the overdue amount
invoice = await client.CreateInvoice(user.StoreId,

View File

@ -183,7 +183,8 @@ namespace BTCPayServer.Controllers.Greenfield
{
ModelState.AddModelError(nameof(request.Amount), "The amount should be 0 or more.");
}
request.Checkout = request.Checkout ?? new CreateInvoiceRequest.CheckoutOptions();
request.Checkout ??= new CreateInvoiceRequest.CheckoutOptions();
request.Checkout.DefaultPaymentMethod ??= store.GetDefaultPaymentId()?.ToStringNormalized();
if (request.Checkout.PaymentMethods?.Any() is true)
{
for (int i = 0; i < request.Checkout.PaymentMethods.Length; i++)