.. include:: ../../Includes.rst.txt .. _webhook-notifications: ================================== Notify a Slack channel via webhook ================================== Besides the emails, EXT:cart can notify other systems about orders, e.g. post a message to a Slack channel when an order is finished or paid. EXT:cart has no configuration for this; register your own event listeners for the events which also send the emails: ``\Extcode\Cart\Event\Order\FinishEvent`` Dispatched when an order is finished. With a payment provider which redirects to an external payment page, the payment provider extension dispatches it after a successful payment, see :ref:`developer-events`. ``\Extcode\Cart\Event\Order\AfterStatusChangeEvent`` Dispatched after the payment or the shipping status of an order was changed, e.g. in the backend module or by a payment provider, see :ref:`developer-order-status-events`. The examples use a Slack incoming webhook. Other services (Microsoft Teams, Mattermost, Discord, n8n, Zapier, ...) expect a different body, only the payload of the request has to be changed. Webhook URL =========== Create an incoming webhook for your Slack channel, see `Sending messages using incoming webhooks `__. Everyone who knows the URL can post to the channel, so keep it out of the repository, e.g. in an environment variable: .. code-block:: bash :caption: .env SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T000/B000/XXXX Sending the message =================== A small service sends the message. It is used by both listeners. .. code-block:: php :caption: EXT:my_extension/Classes/Notification/SlackNotifier.php requestFactory->request($webhookUrl, 'POST', [ 'json' => ['text' => $text], // The buyer waits for the response in the checkout. 'timeout' => 3, ]); } catch (\Throwable $exception) { // A failed notification must never break the order. $this->logger->error('Slack notification failed', ['exception' => $exception]); return false; } return true; } } The request is sent synchronously. Keep the timeout short and catch every exception: in the checkout the buyer waits for the response, and an exception would abort the remaining listeners, e.g. the emails. New orders ========== .. code-block:: php :caption: EXT:my_extension/Classes/EventListener/NotifySlackAboutNewOrder.php getOrderItem(); $this->slackNotifier->send(sprintf( 'New order %s: %s %s', $orderItem->getOrderNumber(), number_format($orderItem->getTotalGross(), 2), $orderItem->getCurrencyCode() )); } } Paid orders =========== A listener of the ``AfterStatusChangeEvent`` reports a failure with ``addError()``. The status stays changed, the editor sees the message in the backend. .. code-block:: php :caption: EXT:my_extension/Classes/EventListener/NotifySlackAboutPaidOrder.php getServiceType() !== ServiceType::Payment || $event->getToStatus() !== PaymentStatus::PAID ) { return; } $orderNumber = $event->getOrderItem()->getOrderNumber(); $sent = $this->slackNotifier->send(sprintf( 'Order %s was paid (before: %s)', $orderNumber, $event->getFromStatus() )); if (!$sent) { $event->addError('The Slack notification for order ' . $orderNumber . ' could not be sent.'); } } } Things to consider ================== Personal data The messages are stored by Slack. Send only what the channel needs, e.g. the order number, the total and the status, but not the name, the address or the email address of the buyer. Sending only once The ``AfterStatusChangeEvent`` is dispatched once per change. If you send the messages later or retry them, use the uid of the change (``$event->getOrderChange()->uid``) to send each message only once. Asynchronous sending To keep the checkout independent of the response time of the webhook, dispatch a message to the :ref:`message bus ` of TYPO3 in the listener and send the request in its handler. With an asynchronous transport the request is sent by a worker, which can also retry failed requests. .. note:: Would a configurable webhook in EXT:cart help you? Give feedback with your use case in `issue #908 `__.