---
title: "Notify a Slack channel via webhook"
manual: "Cart"
version: "main"
permalink: "https://docs.typo3.org/permalink/extcode/cart:webhook-notifications@main"
source: "Tutorials/WebhookNotifications/Index.rst"
rendered: "2026-12-04T23:27:06+00:00"
---

# Notify a Slack channel via webhook {#webhook-notifications}

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 [Events](https://docs.typo3.org/permalink/extcode/cart:developer-events@main).

-   **`\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
    [Events](https://docs.typo3.org/permalink/extcode/cart:developer-order-status-events@main).

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 {#webhook-url}

Create an incoming webhook for your Slack channel, see
[Sending messages using incoming webhooks](https://api.slack.com/messaging/webhooks).
Everyone who knows the URL can post to the channel, so keep it out of the
repository, e.g. in an environment variable:

**.env**

```bash
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T000/B000/XXXX
```

## Sending the message {#sending-the-message}

A small service sends the message. It is used by both listeners.

**EXT:my_extension/Classes/Notification/SlackNotifier.php**

```php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Notification;

use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Http\RequestFactory;

final readonly class SlackNotifier
{
    public function __construct(
        private RequestFactory $requestFactory,
        private LoggerInterface $logger,
    ) {
    }

    public function send(string $text): bool
    {
        $webhookUrl = (string)getenv('SLACK_WEBHOOK_URL');
        if ($webhookUrl === '') {
            return false;
        }

        try {
            $this->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 {#new-orders}

**EXT:my_extension/Classes/EventListener/NotifySlackAboutNewOrder.php**

```php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use Extcode\Cart\Event\Order\FinishEvent;
use MyVendor\MyExtension\Notification\SlackNotifier;
use TYPO3\CMS\Core\Attribute\AsEventListener;

#[AsEventListener('my-extension--order--finish--notify-slack')]
final readonly class NotifySlackAboutNewOrder
{
    public function __construct(
        private SlackNotifier $slackNotifier,
    ) {
    }

    public function __invoke(FinishEvent $event): void
    {
        $orderItem = $event->getOrderItem();

        $this->slackNotifier->send(sprintf(
            'New order %s: %s %s',
            $orderItem->getOrderNumber(),
            number_format($orderItem->getTotalGross(), 2),
            $orderItem->getCurrencyCode()
        ));
    }
}
```

## Paid orders {#paid-orders}

A listener of the `AfterStatusChangeEvent` reports a failure with
`addError()`. The status stays changed, the editor sees the message in the
backend.

**EXT:my_extension/Classes/EventListener/NotifySlackAboutPaidOrder.php**

```php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use Extcode\Cart\Domain\Order\Status\PaymentStatus;
use Extcode\Cart\Domain\Order\Status\ServiceType;
use Extcode\Cart\Event\Order\AfterStatusChangeEvent;
use MyVendor\MyExtension\Notification\SlackNotifier;
use TYPO3\CMS\Core\Attribute\AsEventListener;

#[AsEventListener('my-extension--order--status-change--notify-slack')]
final readonly class NotifySlackAboutPaidOrder
{
    public function __construct(
        private SlackNotifier $slackNotifier,
    ) {
    }

    public function __invoke(AfterStatusChangeEvent $event): void
    {
        if ($event->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 {#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
    [message bus](https://docs.typo3.org/m/typo3/reference-coreapi/14.3/en-us/ApiOverview/MessageBus/Index.html#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](https://github.com/extcode/cart/issues/908).
