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\ Finish Event - 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.
\Extcode\Cart\ Event\ Order\ After Status Change Event - 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.
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:
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.
<?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
<?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
A listener of the After reports a failure with
add. The status stays changed, the editor sees the message in the
backend.
<?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
- 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
Afteris dispatched once per change. If you send the messages later or retry them, use the uid of the change (Status Change Event $event->get) to send each message only once.Order Change ()->uid - Asynchronous sending
- To keep the checkout independent of the response time of the webhook, dispatch a message to the 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.