# 決済フォームに外部の決済手段を追加する

PaymentElement 内に Stripe 外の決済手段を表示します。

Stripe PaymentElement では、多数の[外部の決済手段](https://docs.stripe.com/payments/external-payment-methods.md?platform=web#available-external-payment-methods)が表示されます。外部の決済手段とは、Stripe ダッシュボードに実装されていない決済手段と定義します。この代わりに、外部 URL にリダイレクトして支払いを行います。取引はすべて Stripe 外で行われ、金額、通貨、ステータスなどの情報は一切表示されません。また、これらの支払いは Stripe ダッシュボードのどこにも表示されません。このような取引が発生しても、Stripe が決済手数料を徴収することはありません。

支払いが外部で回収される場合、Magento の注文と外部支払いの間で消し込みは行われません。注文のステータスは保留中の支払いのままで、支払いステータスの詳細も表示されません。これは、消し込みを実行する必要があることを意味しています。

Stripe は、外部支払いを Magento の注文と照合する方法のガイドラインを用意していません。消し込みを実行するには、外部の決済手段のサポートチームに実装ガイドをお問い合わせください。

このガイドでは、外部の決済手段を有効にして購入ページに表示する方法についてのみ説明します。顧客が「注文」ボタンをクリックすると、外部決済手段に指定した外部 URL にリダイレクトされます。

## 新しいモジュールを作成する

次のディレクトリー構造で新しいモジュールを作成します。`Vendor` を任意のベンダー名に置き換えます。

```
app/code/Vendor/StripeCustomizations/
├── etc/
│   ├── module.xml
│   └── di.xml
├── Plugin/
│   └── Helper/
│       └── PaymentMethodPlugin.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` 内で、以下のプラグインを定義します。

```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\Helper\PaymentMethod">
        <plugin
            name="vendor_stripecustomizations_helper_paymentmethod_plugin"
            type="Vendor\StripeCustomizations\Plugin\Helper\PaymentMethodPlugin"
            sortOrder="10"
            disabled="false" />
    </type>
</config>
```

`Plugin/Helper/PaymentMethodPlugin.php` 内で、afterMethod インターセプターを追加します。

```php
<?php

namespace Vendor\Module\Plugin;

class PaymentMethodPlugin
{
    public function afterGetExternalPaymentMethods(
        \StripeIntegration\Payments\Helper\PaymentMethod $subject,
        array $result,
        $quote
    ): array {
        // Add custom payment method to the array
        $result[] = [
            'code' => 'external_payment_method_code',
            'redirect_url' => "https://example.com/checkout?merchant=stripeintegration&amount=" . $quote->getGrandTotal() * 100
        ];

        return $result;
    }
}
```

モジュールを有効化します。

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