2020-06-22 09:32:51 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2020-06-22 09:32:51 +02:00
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Controllers;
|
|
|
|
using BTCPayServer.Events;
|
|
|
|
using BTCPayServer.Models.NotificationViewModels;
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services.Notifications.Blobs
|
|
|
|
{
|
2020-10-20 13:09:09 +02:00
|
|
|
internal class InvoiceEventNotification:BaseNotification
|
2020-06-22 09:32:51 +02:00
|
|
|
{
|
2020-10-20 13:09:09 +02:00
|
|
|
private const string TYPE = "invoicestate";
|
2020-06-22 09:32:51 +02:00
|
|
|
internal class Handler : NotificationHandler<InvoiceEventNotification>
|
|
|
|
{
|
|
|
|
private readonly LinkGenerator _linkGenerator;
|
|
|
|
private readonly BTCPayServerOptions _options;
|
|
|
|
|
|
|
|
public Handler(LinkGenerator linkGenerator, BTCPayServerOptions options)
|
|
|
|
{
|
|
|
|
_linkGenerator = linkGenerator;
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:09:09 +02:00
|
|
|
public override string NotificationType => TYPE;
|
|
|
|
|
|
|
|
public override (string identifier, string name)[] Meta
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return new (string identifier, string name)[] {(TYPE, "All invoice updates"),}
|
|
|
|
.Concat(TextMapping.Select(pair => ($"{TYPE}_{pair.Key}", $"Invoice {pair.Value}"))).ToArray();
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 09:32:51 +02:00
|
|
|
|
|
|
|
internal static Dictionary<string, string> TextMapping = new Dictionary<string, string>()
|
|
|
|
{
|
2021-01-07 01:07:42 -08:00
|
|
|
// {InvoiceEvent.PaidInFull, "was fully paid"},
|
|
|
|
{InvoiceEvent.PaidAfterExpiration, "was paid after expiration"},
|
|
|
|
{InvoiceEvent.ExpiredPaidPartial, "expired with partial payments"},
|
|
|
|
{InvoiceEvent.FailedToConfirm, "has payments that failed to confirm on time"},
|
|
|
|
// {InvoiceEvent.ReceivedPayment, "received a payment"},
|
|
|
|
{InvoiceEvent.Confirmed, "was confirmed paid"}
|
2020-06-22 09:32:51 +02:00
|
|
|
};
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2020-06-22 09:32:51 +02:00
|
|
|
protected override void FillViewModel(InvoiceEventNotification notification,
|
|
|
|
NotificationViewModel vm)
|
|
|
|
{
|
|
|
|
var baseStr = $"Invoice {notification.InvoiceId.Substring(0, 5)}..";
|
2020-06-24 10:23:16 +02:00
|
|
|
if (TextMapping.ContainsKey(notification.Event))
|
|
|
|
{
|
|
|
|
vm.Body = $"{baseStr} {TextMapping[notification.Event]}";
|
|
|
|
}
|
2020-06-22 09:32:51 +02:00
|
|
|
vm.ActionLink = _linkGenerator.GetPathByAction(nameof(InvoiceController.Invoice),
|
|
|
|
"Invoice",
|
2020-06-28 17:55:27 +09:00
|
|
|
new { invoiceId = notification.InvoiceId }, _options.RootPath);
|
2020-06-22 09:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public InvoiceEventNotification()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public InvoiceEventNotification(string invoiceId, string invoiceEvent)
|
|
|
|
{
|
|
|
|
InvoiceId = invoiceId;
|
|
|
|
Event = invoiceEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool HandlesEvent(string invoiceEvent)
|
|
|
|
{
|
|
|
|
return Handler.TextMapping.Keys.Any(s => s == invoiceEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string InvoiceId { get; set; }
|
|
|
|
public string Event { get; set; }
|
2020-10-20 13:09:09 +02:00
|
|
|
public override string Identifier => $"{TYPE}_{Event}";
|
|
|
|
public override string NotificationType => TYPE;
|
2020-06-22 09:32:51 +02:00
|
|
|
}
|
|
|
|
}
|