Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new custom event #8

Merged
merged 2 commits into from
Jul 20, 2023
Merged

Conversation

pasqualinibruno
Copy link
Contributor

This PR adds a new a custom event that allows webhooks/integrations to be triggered manually. This is useful when we want to to set up a webhook/integration that shouldn't be triggered automatically by the system when one of the default entity events present in this bundle takes places such as CREATE, UPDATE, DELETE.

The following code shows how to make use of this functionality by creating a custom event that would trigger a webhook when an Oro payment transaction is completed.

1. Create a webhook custom event

services.yml

  MyBundle\Webhook\WebhookCustomEventPaymentTransactionComplete:
    tags:
      - { name: aligent_async_events.custom_events }

WebhookCustomEventPaymentTransactionComplete class

class WebhookCustomEventPaymentTransactionComplete implements WebhookCustomEventInterface
{
    private const PAYMENT_TRANSACTION_COMPLETE = 'payment_complete';
    private const TRANSLATION_EVENT_NAME = 'mybundle.async.transport.form.custom_event.payment_transaction_complete';

    /**
     * @inheritDoc
     */
    public static function getTranslationName(): string
    {
        return self::TRANSLATION_EVENT_NAME;
    }

    /**
     * @inheritDoc
     */
    public static function getKey(): string
    {
        return self::PAYMENT_TRANSACTION_COMPLETE;
    }
}

messages.en.yml

mybundle.async.transport.form.custom_event.payment_transaction_complete: Custom - Payment Transaction Complete

2. Once the above has been implemented, you should see your new custom event as a option in the event dropdown

image

3. Listen to payment transactions

  MyBundle\EventListener\PaymentTransactionListener:
    arguments:
      - '@oro_message_queue.client.message_producer'
      - '@Aligent\AsyncEventsBundle\Provider\WebhookConfigProvider'
    tags:
      - { name: kernel.event_listener, event: oro_payment.event.transaction_complete, method: onTransactionComplete, priority: -256 }

4. Trigger webhook custom event

        $webhookEntityConfig = $this->webhookConfigProvider->getNotificationChannels(
            Order::class,
            WebhookCustomEventPaymentTransactionComplete::getKey()
        );

        foreach ($webhookEntityConfig as $channelId) {
            $this->producer->send(
                Topics::WEBHOOK_ENTITY_CUSTOM,
                [
                    'class' => Order::class,
                    'id' => ['id' => $order->getId()],
                    'channelId' => $channelId
                ]
            );
        }

@pasqualinibruno pasqualinibruno merged commit 1571ca2 into master Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants