diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index ceb0f3118..79a73276e 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -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()); + 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()); + 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, diff --git a/BTCPayServer/Controllers/GreenField/GreenfieldInvoiceController.cs b/BTCPayServer/Controllers/GreenField/GreenfieldInvoiceController.cs index b8c518f77..282a22dff 100644 --- a/BTCPayServer/Controllers/GreenField/GreenfieldInvoiceController.cs +++ b/BTCPayServer/Controllers/GreenField/GreenfieldInvoiceController.cs @@ -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++) @@ -226,7 +227,7 @@ namespace BTCPayServer.Controllers.Greenfield if (!ModelState.IsValid) return this.CreateValidationError(ModelState); - + try { var invoice = await _invoiceController.CreateInvoiceCoreRaw(request, store,