# カスタムイベントを 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.php` 内で、モジュールを Magento に登録します。

```php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_StripeCustomizations',
    __DIR__
);
```

## モジュール設定を定義する

`etc/module.xml` の中で、モジュールを定義し、それが Stripe モジュールに依存することを指定します。

```xml
<?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.xml` 内で、`EnabledEvents` クラスのプラグインを定義します。

```xml
<?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.php` 内で、`getEvents` メソッドの `after` プラグインを定義します。

```php
<?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.event.example` と `another.custom.event`) を既存のイベントリストに追加します。

サポートされているイベントタイプの完全な一覧については、[イベントタイプ](https://docs.stripe.com/api/events/types.md)に関するドキュメントを参照してください。

## モジュールを有効にする

次のコマンドを実行して、モジュールを有効にしてデプロイします。

```sh
php bin/magento module:enable Vendor_StripeCustomizations
php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush
```

## 変更を確認する

CLI から次のコマンドを実行して、Webhook を再構成します。

```sh
bin/magento stripe:webhooks:configure
```

[Stripe ダッシュボード](https://dashboard.stripe.com/webhooks)に移動し、新しい Webhook エンドポイントの設定を手動で確認します。**Listening for xx events** タブにカーソルを合わせると、新しい Webhook イベントがリストに表示されます。
