Merge pull request #1875 from dennisreimann/invoice-notification-email

Invoice notification email improvements
This commit is contained in:
Nicolas Dorier 2020-09-19 11:16:49 +09:00 committed by GitHub
commit abd0ba49ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ using BTCPayServer.Payments;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Mails; using BTCPayServer.Services.Mails;
using BTCPayServer.Services.Stores;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NBitpayClient; using NBitpayClient;
using NBXplorer; using NBXplorer;
@ -42,13 +43,14 @@ namespace BTCPayServer.HostedServices
readonly EventAggregator _EventAggregator; readonly EventAggregator _EventAggregator;
readonly InvoiceRepository _InvoiceRepository; readonly InvoiceRepository _InvoiceRepository;
private readonly EmailSenderFactory _EmailSenderFactory; private readonly EmailSenderFactory _EmailSenderFactory;
private readonly StoreRepository _StoreRepository;
public InvoiceNotificationManager( public InvoiceNotificationManager(
IHttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
IBackgroundJobClient jobClient, IBackgroundJobClient jobClient,
EventAggregator eventAggregator, EventAggregator eventAggregator,
InvoiceRepository invoiceRepository, InvoiceRepository invoiceRepository,
BTCPayNetworkProvider networkProvider, StoreRepository storeRepository,
EmailSenderFactory emailSenderFactory) EmailSenderFactory emailSenderFactory)
{ {
_Client = httpClientFactory.CreateClient(); _Client = httpClientFactory.CreateClient();
@ -56,9 +58,10 @@ namespace BTCPayServer.HostedServices
_EventAggregator = eventAggregator; _EventAggregator = eventAggregator;
_InvoiceRepository = invoiceRepository; _InvoiceRepository = invoiceRepository;
_EmailSenderFactory = emailSenderFactory; _EmailSenderFactory = emailSenderFactory;
_StoreRepository = storeRepository;
} }
void Notify(InvoiceEntity invoice, InvoiceEvent invoiceEvent, bool extendedNotification) async Task Notify(InvoiceEntity invoice, InvoiceEvent invoiceEvent, bool extendedNotification)
{ {
var dto = invoice.EntityToDTO(); var dto = invoice.EntityToDTO();
var notification = new InvoicePaymentNotificationEventWrapper() var notification = new InvoicePaymentNotificationEventWrapper()
@ -118,15 +121,21 @@ namespace BTCPayServer.HostedServices
#pragma warning restore CS0618 #pragma warning restore CS0618
} }
if (!String.IsNullOrEmpty(invoice.NotificationEmail)) if (invoiceEvent.Name != InvoiceEvent.Expired && !String.IsNullOrEmpty(invoice.NotificationEmail))
{ {
var emailBody = NBitcoin.JsonConverters.Serializer.ToString(notification); var json = NBitcoin.JsonConverters.Serializer.ToString(notification);
var store = await _StoreRepository.FindStore(invoice.StoreId);
var storeName = store.StoreName ?? "BTCPay Server";
var emailBody = $"Store: {storeName}<br>" +
$"Invoice ID: {notification.Data.Id}<br>" +
$"Status: {notification.Data.Status}<br>" +
$"Amount: {notification.Data.Price} {notification.Data.Currency}<br>" +
$"<br><details><summary>Details</summary><pre>{json}</pre></details>";
_EmailSenderFactory.GetEmailSender(invoice.StoreId).SendEmail( _EmailSenderFactory.GetEmailSender(invoice.StoreId).SendEmail(
invoice.NotificationEmail, invoice.NotificationEmail,
$"BtcPayServer Invoice Notification - ${invoice.StoreId}", $"{storeName} Invoice Notification - ${invoice.StoreId}",
emailBody); emailBody);
} }
if (invoice.NotificationURL != null) if (invoice.NotificationURL != null)