カスタムイベントを Stripe Webhook に追加する
Adobe Commerce の Stripe モジュールでサポートされている Webhook イベントのリストを拡張します。
Stripe Webhook は、Stripe アカウントで発生したイベントに基づいてアクションをトリガーするツールです。このガイドでは、Stripe モジュールを拡張して、有効なイベントのリストにカスタムイベントを追加する方法について説明します。
新しいモジュールを作成する
まず、次のディレクトリ構造で新しいモジュールを作成します。Vendor
をご希望のベンダー名に置き換えます。
app/code/Vendor/StripeCustomizations/ ├── etc/ │ ├── module.xml │ └── di.xml ├── Plugin/ │ └── Webhooks/ │ └── EnabledEventsPlugin.php ├── registration.php
モジュールを登録する 
registration.
内で、モジュールを Magento に登録します。
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_StripeCustomizations', __DIR__ );
モジュール構成を定義する 
etc/module.
の中で、モジュールを定義し、それが Stripe モジュールに依存することを指定します。
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_StripeCustomizations" setup_version="1.0.0"> <sequence> <module name="StripeIntegration_Payments"/> </sequence> </module> </config>
依存関係の挿入を構成する 
etc/di.
内で、EnabledEvents
クラスのプラグインを定義します。
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="StripeIntegration\Payments\Model\Webhooks\EnabledEvents"> <plugin name="vendor_stripecustomizations_webhooks_enabledevents_plugin" type="Vendor\StripeCustomizations\Plugin\Webhooks\EnabledEventsPlugin" sortOrder="10" disabled="false" /> </type> </config>
プラグインを作成する 
Plugin/Webhooks/EnabledEventsPlugin.
内で、getEvents
メソッドの after
プラグインを定義します。
<?php namespace Vendor\StripeCustomizations\Plugin\Webhooks; use StripeIntegration\Payments\Model\Webhooks\EnabledEvents; class EnabledEventsPlugin { /** * After plugin for getEvents method. * * @param EnabledEvents $subject * @param array $result * @return array */ public function afterGetEvents(EnabledEvents $subject, array $result) { // Add custom events to the list $result[] = 'custom.event.example'; $result[] = 'another.custom.event'; return $result; } }
ここでは、カスタムイベント (custom.
と another.
) を既存のイベントリストに追加します。
サポートされているイベントタイプの完全なリストについては、イベントの種類のドキュメントを参照してください。
モジュールを有効にする 
次のコマンドを実行して、モジュールを有効にしてデプロイします。
php bin/magento module:enable Vendor_StripeCustomizations php bin/magento setup:upgrade php bin/magento cache:clean php bin/magento cache:flush
変更を確認する 
CLI から次のコマンドを実行して、Webhook を再構成します。
bin/magento stripe:webhooks:configure
Stripe ダッシュボードに移動し、新しい Webhook エンドポイントの設定を手動で確認します。Listening for xx events タブにカーソルを合わせると、新しい Webhook イベントがリストに表示されます。