Add ability to select default payment method for invoice through UI

This commit is contained in:
Umar Bolatov 2021-08-22 21:55:06 -07:00 committed by Andrew Camilleri
parent bb6a188883
commit 809340e629
7 changed files with 33 additions and 5 deletions

View File

@ -476,7 +476,7 @@ namespace BTCPayServer.Controllers
bool isDefaultPaymentId = false;
if (paymentMethodId is null)
{
paymentMethodId = store.GetDefaultPaymentId(_NetworkProvider);
paymentMethodId = _InvoiceRepository.GetDefaultPaymentId(store.GetEnabledPaymentIds(_NetworkProvider), invoice) ?? store.GetDefaultPaymentId(_NetworkProvider);
isDefaultPaymentId = true;
}
BTCPayNetworkBase network = _NetworkProvider.GetNetwork<BTCPayNetworkBase>(paymentMethodId.CryptoCode);
@ -854,7 +854,8 @@ namespace BTCPayServer.Controllers
SupportedTransactionCurrencies = model.SupportedTransactionCurrencies?.ToDictionary(s => s, s => new InvoiceSupportedTransactionCurrency()
{
Enabled = true
})
}),
DefaultPaymentMethod = model.DefaultPaymentMethod,
}, store, HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
TempData[WellKnownTempData.SuccessMessage] = $"Invoice {result.Data.Id} just created!";

View File

@ -151,6 +151,7 @@ namespace BTCPayServer.Controllers
excludeFilter = PaymentFilter.Where(p => !supportedTransactionCurrencies.Contains(p));
}
entity.PaymentTolerance = storeBlob.PaymentTolerance;
entity.DefaultPaymentMethod = invoice.DefaultPaymentMethod;
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken);
}

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using NBitpayClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Models
{
@ -52,6 +51,8 @@ namespace BTCPayServer.Models
public string Currency { get; set; }
[JsonProperty(PropertyName = "price", DefaultValueHandling = DefaultValueHandling.Ignore)]
public decimal? Price { get; set; }
[JsonProperty(PropertyName = "defaultPaymentMethod", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string DefaultPaymentMethod { get; set; }
[JsonProperty(PropertyName = "notificationEmail", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string NotificationEmail { get; set; }
[JsonConverter(typeof(DateTimeJsonConverter))]

View File

@ -44,6 +44,12 @@ namespace BTCPayServer.Models.InvoicingModels
get; set;
}
[Display(Name = "Default payment method on checkout")]
public string DefaultPaymentMethod
{
get; set;
}
[DisplayName("POS Data")]
public string PosData
{

View File

@ -250,6 +250,7 @@ namespace BTCPayServer.Services.Invoices
public decimal Price { get; set; }
public string Currency { get; set; }
public string DefaultPaymentMethod { get; set; }
[JsonExtensionData]
public IDictionary<string, JToken> AdditionalData { get; set; }

View File

@ -10,8 +10,8 @@ namespace BTCPayServer.Services.Invoices
{
public static class InvoiceExtensions
{
public static async Task ActivateInvoicePaymentMethod(this InvoiceRepository invoiceRepository,
public static async Task ActivateInvoicePaymentMethod(this InvoiceRepository invoiceRepository,
EventAggregator eventAggregator, BTCPayNetworkProvider btcPayNetworkProvider, PaymentMethodHandlerDictionary paymentMethodHandlerDictionary,
StoreData store,InvoiceEntity invoice, PaymentMethodId paymentMethodId)
{
@ -49,5 +49,18 @@ namespace BTCPayServer.Services.Invoices
}
}
public static PaymentMethodId GetDefaultPaymentId(
this InvoiceRepository invoiceRepository,
PaymentMethodId[] paymentMethodIds,
InvoiceEntity invoice
)
{
PaymentMethodId.TryParse(invoice.DefaultPaymentMethod, out var defaultPaymentId);
var chosen = paymentMethodIds.FirstOrDefault(f => f == defaultPaymentId) ??
paymentMethodIds.FirstOrDefault(f => f.CryptoCode == defaultPaymentId?.CryptoCode) ??
paymentMethodIds.FirstOrDefault();
return chosen;
}
}
}

View File

@ -79,6 +79,11 @@
<select asp-for="SupportedTransactionCurrencies" asp-items="Model.AvailablePaymentMethods" class="form-select" multiple="multiple"></select>
<span asp-validation-for="SupportedTransactionCurrencies" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DefaultPaymentMethod" class="form-label"></label>
<select asp-for="DefaultPaymentMethod" asp-items="Model.AvailablePaymentMethods" class="form-select"></select>
<span asp-validation-for="DefaultPaymentMethod" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" id="Create" />
<a asp-action="ListInvoices" class="text-muted ms-3">Back to list</a>