コード例 #1
0
ファイル: WebhookSender.cs プロジェクト: nichlor/btcpayserver
        public static WebhookInvoiceEvent?GetWebhookEvent(InvoiceEvent invoiceEvent)
        {
            var eventCode = invoiceEvent.EventCode;

            switch (eventCode)
            {
            case InvoiceEventCode.Completed:
            case InvoiceEventCode.PaidAfterExpiration:
                return(null);

            case InvoiceEventCode.Confirmed:
            case InvoiceEventCode.MarkedCompleted:
                return(new WebhookInvoiceSettledEvent(WebhookEventType.InvoiceSettled)
                {
                    ManuallyMarked = eventCode == InvoiceEventCode.MarkedCompleted
                });

            case InvoiceEventCode.Created:
                return(new WebhookInvoiceEvent(WebhookEventType.InvoiceCreated));

            case InvoiceEventCode.Expired:
                return(new WebhookInvoiceExpiredEvent(WebhookEventType.InvoiceExpired)
                {
                    PartiallyPaid = invoiceEvent.PaidPartial
                });

            case InvoiceEventCode.FailedToConfirm:
            case InvoiceEventCode.MarkedInvalid:
                return(new WebhookInvoiceInvalidEvent(WebhookEventType.InvoiceInvalid)
                {
                    ManuallyMarked = eventCode == InvoiceEventCode.MarkedInvalid
                });

            case InvoiceEventCode.PaidInFull:
                return(new WebhookInvoiceProcessingEvent(WebhookEventType.InvoiceProcessing)
                {
                    OverPaid = invoiceEvent.Invoice.ExceptionStatus == InvoiceExceptionStatus.PaidOver,
                });

            case InvoiceEventCode.ReceivedPayment:
                return(new WebhookInvoiceReceivedPaymentEvent(WebhookEventType.InvoiceReceivedPayment)
                {
                    AfterExpiration = invoiceEvent.Invoice.Status.ToModernStatus() == InvoiceStatus.Expired || invoiceEvent.Invoice.Status.ToModernStatus() == InvoiceStatus.Invalid,
                    PaymentMethod = invoiceEvent.Payment.GetPaymentMethodId().ToStringNormalized(),
                    Payment = GreenfieldInvoiceController.ToPaymentModel(invoiceEvent.Invoice, invoiceEvent.Payment)
                });

            case InvoiceEventCode.PaymentSettled:
                return(new WebhookInvoiceReceivedPaymentEvent(WebhookEventType.InvoicePaymentSettled)
                {
                    AfterExpiration = invoiceEvent.Invoice.Status.ToModernStatus() == InvoiceStatus.Expired || invoiceEvent.Invoice.Status.ToModernStatus() == InvoiceStatus.Invalid,
                    PaymentMethod = invoiceEvent.Payment.GetPaymentMethodId().ToStringNormalized(),
                    Payment = GreenfieldInvoiceController.ToPaymentModel(invoiceEvent.Invoice, invoiceEvent.Payment),
                    OverPaid = invoiceEvent.Invoice.ExceptionStatus == InvoiceExceptionStatus.PaidOver,
                });

            default:
                return(null);
            }
        }
コード例 #2
0
    protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
    {
        if (evt is InvoiceEvent invoiceEvent)
        {
            var type = WebhookSender.GetWebhookEvent(invoiceEvent);
            if (type is null)
            {
                return;
            }

            var store = await _storeRepository.FindStore(invoiceEvent.Invoice.StoreId);


            var blob = store.GetStoreBlob();
            if (blob.EmailRules?.Any() is true)
            {
                var actionableRules = blob.EmailRules.Where(rule => rule.Trigger == type.Type).ToList();
                if (actionableRules.Any())
                {
                    var sender = await _emailSenderFactory.GetEmailSender(invoiceEvent.Invoice.StoreId);

                    foreach (UIStoresController.StoreEmailRule actionableRule in actionableRules)
                    {
                        var recipients = (actionableRule.To?.Split(",", StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty <string>())
                                         .Select(o =>
                        {
                            MailboxAddressValidator.TryParse(o, out var mb);
                            return(mb);
                        })
                                         .Where(o => o != null)
                                         .ToList();
                        if (actionableRule.CustomerEmail &&
                            MailboxAddressValidator.TryParse(invoiceEvent.Invoice.Metadata.BuyerEmail, out var bmb))
                        {
                            recipients.Add(bmb);
                        }
                        var i = GreenfieldInvoiceController.ToModel(invoiceEvent.Invoice, _linkGenerator, null);
                        sender.SendEmail(recipients.ToArray(), null, null, Interpolator(actionableRule.Subject, i),
                                         Interpolator(actionableRule.Body, i));
                    }
                }
            }
        }
    }