Skip to content

Commit

Permalink
NotificationContext parameter introduced to NotificationEvent
Browse files Browse the repository at this point in the history
This limits attaching invoice to specific notifications - fixes the situation where invoice was added to all notification events containing `payment_id` parameter.

remp/crm#1654
  • Loading branch information
miroc committed Jan 20, 2021
1 parent 36e9a5e commit 9dadbb3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ invoiceGenerator:

The snippet tells to invoice generator to use template provided at given path instead of default template.

## Attaching invoices to email notifications

PDF invoice is attached by default to each email notification containing reference to a payment (as a parameter in `NotificationEvent`). In addition, this notification has to be emitted as a result of processing of a specific trigger (Hermes message) by the application. Typically, this happens when a Hermes message triggers a scenario (see [ScenariosModule] for details) that emits a `NotificationEvent` (this is done by _Email node_).

To limit which Hermes messages shall be accepted when attaching an invoice (see `PreNotificationEventHandler` for implementation details), add the following configuration to your `config.local.neon` file, listing allowed Hermes message types:

```neon
invoicesPreNotificationEventHandler:
setup:
- enableForNotificationHermesTypes('new-subscription', 'payment-status-change', 'some-other-hermes-message')
```

## Components

**DownloadReceiptButton**
Expand Down Expand Up @@ -180,3 +192,5 @@ Widget for downloading invoice from payments success page.
[Source code](https://github.com/remp2020/crm-invoices-module/blob/a9b3730588507276109f1f61e80e60021e27fff2/src/components/PaymentSuccessInvoiceWidget/PaymentSuccessInvoiceWidget.php#L1)

[How to use](https://github.com/remp2020/crm-invoices-module/blob/a9b3730588507276109f1f61e80e60021e27fff2/src/InvoicesModule.php#L51)

[ScenariosModule]: https://github.com/remp2020/crm-scenarios-module/
6 changes: 5 additions & 1 deletion src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ services:
- Crm\InvoicesModule\DataProvider\UserFormDataProvider
- Crm\InvoicesModule\Events\AddressChangedHandler
- Crm\InvoicesModule\Events\AddressRemovedHandler
- Crm\InvoicesModule\Events\PreNotificationEventHandler
- Crm\InvoicesModule\Events\NewAddressHandler
- Crm\InvoicesModule\Events\PaymentStatusChangeHandler
- Crm\InvoicesModule\Forms\ChangeInvoiceDetailsFormFactory
Expand All @@ -41,6 +40,11 @@ services:
- Crm\InvoicesModule\Sandbox\InvoiceZipGenerator
invoiceSandbox: Crm\InvoicesModule\Sandbox\InvoiceSandbox('/tmp')

invoicesPreNotificationEventHandler:
class: Crm\InvoicesModule\Events\PreNotificationEventHandler
setup:
- enableForNotificationHermesTypes('new-subscription', 'payment-status-change', 'recurrent-payment-renewed', 'recurrent-payment-state-changed')

- Crm\InvoicesModule\Seeders\ConfigsSeeder
- Crm\InvoicesModule\Seeders\AddressTypesSeeder
- Crm\InvoicesModule\Seeders\PaymentGatewaysSeeder
Expand Down
28 changes: 27 additions & 1 deletion src/events/PreNotificationEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Crm\InvoicesModule\InvoiceGenerationException;
use Crm\InvoicesModule\InvoiceGenerator;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\UsersModule\Events\NotificationContext;
use Crm\UsersModule\Events\PreNotificationEvent;
use League\Event\AbstractListener;
use League\Event\EventInterface;
Expand All @@ -20,6 +21,8 @@ class PreNotificationEventHandler extends AbstractListener

private $applicationConfig;

private $enabledNotificationHermesTypes = [];

public function __construct(
InvoiceGenerator $invoiceGenerator,
PaymentsRepository $paymentsRepository,
Expand All @@ -30,6 +33,16 @@ public function __construct(
$this->applicationConfig = $applicationConfig;
}

/**
* Invoice will be attached to any NotificationEvent having NotificationContext with given hermes types
*
* @param string ...$notificationHermesTypes
*/
public function enableForNotificationHermesTypes(string ...$notificationHermesTypes): void
{
$this->enabledNotificationHermesTypes = $notificationHermesTypes;
}

public function handle(EventInterface $event)
{
if (!($event instanceof PreNotificationEvent)) {
Expand All @@ -38,7 +51,20 @@ public function handle(EventInterface $event)

$flag = filter_var($this->applicationConfig->get('attach_invoice_to_payment_notification'), FILTER_VALIDATE_BOOLEAN);
if (!$flag) {
return false;
return;
}

// Invoice will be attached only in case that correct hermes type is found in NotificationContext
$notificationContext = $event->getNotificationContext();
if (!$notificationContext) {
return;
}
$hermesMessageType = $notificationContext->getContextValue(NotificationContext::HERMES_MESSAGE_TYPE);
if (!$hermesMessageType) {
return;
}
if (!in_array($hermesMessageType, $this->enabledNotificationHermesTypes, false)) {
return;
}

$notificationEvent = $event->getNotificationEvent();
Expand Down

0 comments on commit 9dadbb3

Please sign in to comment.