# ダッシュボードでデフォルトの支払い方法を管理する

API をアップグレードし、デフォルトとしてダッシュボードで支払い方法を管理します。

2023 年 8 月 16 日に、Stripe は [/v1/payment_intents](https://docs.stripe.com/api/payment_intents.md) および [/v1/setup_intents](https://docs.stripe.com/api/setup_intents.md) API で作成した PaymentIntent と SetupIntent に適用されるデフォルトの支払い方法の[選定プロセスを更新](http://stripe.com/blog/dynamic-payment-methods)しました。

以前のバージョンの Stripe API では、作成リクエスト中に [payment_method_types](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_types) パラメーターを指定しなかった場合、Stripe は PaymentIntent と SetupIntent の両方にカードの支払い方法をデフォルトで使用していました。

これ以降、作成リクエストで `payment_method_types` パラメーターを指定しない場合、Stripe は PaymentIntent と SetupIntent に、[ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)でお客様が管理している対象の支払い方法をデフォルトで適用します。

### 決済手段

デフォルトでは、カードとその他の一般的な決済手段が有効になっています。[Stripe ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)で個々の決済手段をオンまたはオフにできます。Checkout では、Stripe は通貨と制限事項を評価して、対応している決済手段を顧客に動的に提示します。

決済手段が顧客にどのように表示されるかを確認するには、ダッシュボードの[決済手段の確認](https://dashboard.stripe.com/settings/payment_methods/review)ページで取引 ID を入力するか、注文金額と通貨を設定します。

Checkout は、実装を変更することなく Apple Pay と Google Pay をサポートしています。[ウォレットのテスト方法](https://docs.stripe.com/testing/wallets.md?ui=embedded-page)をご覧ください。

## 決済フローを更新する

現在の Stripe の実装に合ったアップグレードパスを選択してください。

#### Elements

お客様のシステムで Card Element または個別の支払い方法の Element を使用している場合は、[Payment Element](https://docs.stripe.com/payments/payment-element/migration.md) に移行することをお勧めします。この統合型の導入により、一度に 25 種類以上の支払い方法を受け付けられるようになります。

### PaymentIntent を作成する

このバージョンの API では、[automatic_payment_methods.enabled](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-automatic_payment_methods-enabled) パラメーターの指定は任意です。このパラメーターを指定しない場合、Stripe では `true` の値を想定し、デフォルトでこの機能が有効になるとみなされます。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd
```

### クライアント側での Stripe.js による確定

お客様のシステムが Stripe.js を使用し、[confirmPayment](https://docs.stripe.com/js/payment_intents/confirm_payment) または [payment method](https://docs.stripe.com/js/payment_intents/payment_method) で支払いを確定している場合、既存のプロセスは変わらず、これ以降の変更も必要ありません。

支払いを確定する場合は、[return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url) パラメーターを指定することをお勧めします。これにより、[リダイレクトを必要とする](https://docs.stripe.com/payments/payment-methods/payment-method-support.md#additional-api-supportability)支払い方法を受け付けることができます。

#### HTML + JS

```javascript
const form = document.getElementById('payment-form');

form.addEventListener('submit', async (event) => {
  event.preventDefault();

  const {error} = await stripe.confirmPayment({
    //`Elements` instance that was used to create the Payment Element
    elements,
    confirmParams: {
      return_url: 'https://example.com/return_url',
    },
  });

  if (error) {
    // This point will only be reached if there is an immediate error when
    // confirming the payment. Show error to your customer (for example, payment
    // details incomplete)
    const messageContainer = document.querySelector('#error-message');
    messageContainer.textContent = error.message;
  } else {
    // Your customer will be redirected to your `return_url`. For some payment
    // methods like iDEAL, your customer will be redirected to an intermediate
    // site first to authorize the payment, then redirected to the `return_url`.
  }
});
```

#### React

```jsx
import React, {useState} from 'react';
import {useStripe, useElements, PaymentElement} from '@stripe/react-stripe-js';

const CheckoutForm = () => {
  const stripe = useStripe();
  const elements = useElements();

  const [errorMessage, setErrorMessage] = useState(null);

  const handleSubmit = async (event) => {
    // We don't want to let default form submission happen here,
    // which would refresh the page.
    event.preventDefault();

    if (!stripe || !elements) {
      // Stripe.js hasn't yet loaded.
      // Make sure to disable form submission until Stripe.js has loaded.
      return;
    }

    const {error} = await stripe.confirmPayment({
      //`Elements` instance that was used to create the Payment Element
      elements,
      confirmParams: {
        return_url: 'https://example.com/return_url',
      },
    });


    if (error) {
      // This point will only be reached if there is an immediate error when
      // confirming the payment. Show error to your customer (for example, payment
      // details incomplete)
      setErrorMessage(error.message);
    } else {
      // Your customer will be redirected to your `return_url`. For some payment
      // methods like iDEAL, your customer will be redirected to an intermediate
      // site first to authorize the payment, then redirected to the `return_url`.
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <PaymentElement />
      <button disabled={!stripe}>Submit</button>
      {/* Show error message to your customers */}
      {errorMessage && <div>{errorMessage}</div>}
    </form>
  )
};

export default CheckoutForm;
```

### サーバー側での確定

サーバー側で確定する場合は、実装で [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url) パラメーターを使用する必要があります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  --data-urlencode "return_url=https://example.com/return_url"
```

または、[automatic_payment_methods.allow_redirects](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-automatic_payment_methods-allow_redirects) パラメーターを`never`に設定して PaymentIntent または SetupIntent を作成することもできます。これにより、確定時の`return_url`要件が無効になります。決済手段は[ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)から管理できますが、[リダイレクトが必要な支払い方法](https://docs.stripe.com/payments/bank-redirects.md#bank-redirects-api-support)は対象外です。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  -d "automatic_payment_methods[enabled]=true" \
  -d "automatic_payment_methods[allow_redirects]=never"
```

最後に、[allowed_payment_method_types](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-allowed_payment_method_types) パラメーターを使用して PaymentIntent または SetupIntent を作成できます。これにより、確定時の `return_url` の要件も無効になります。このオプションを使用すると、[ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)から決済手段を管理できなくなります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  -d "allowed_payment_method_types[]=card"
```

#### Checkout と Payment Links

お客様のシステムで Checkout と Payment Links を使用している場合は、[API バージョンのアップグレードに関するガイド](https://docs.stripe.com/upgrades.md#how-can-i-upgrade-my-api)を参照していただくことで、実装コードを変更せずにアップグレード可能になります。

#### API

お客様のシステムが Stripe.js を使用し、[off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) パラメーターを `true` に設定して支払いを確定している場合、既存のプロセスは変わらず、これ以降の変更も必要ありません。

それ以外の場合は、実装で [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url) パラメーターを指定する必要があります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  --data-urlencode "return_url=https://example.com/return_url"
```

または、[automatic_payment_methods.allow_redirects](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-automatic_payment_methods-allow_redirects) パラメーターを `never` に設定して PaymentIntent または SetupIntent を作成できます。これにより、確定時の `return_url` 要件も無効になります。引き続き[ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)から支払い方法を管理できますが、リダイレクトを必要とする支払い方法は対象外になります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  -d "automatic_payment_methods[enabled]=true" \
  -d "automatic_payment_methods[allow_redirects]=never"
```

最後に、[allowed_payment_method_types](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-allowed_payment_method_types) パラメーターを使用して PaymentIntent または SetupIntent を作成できます。これにより、確定時の `return_url` の要件も無効になります。このオプションを使用すると、[ダッシュボード](https://dashboard.stripe.com/settings/payment_methods)から決済手段を管理できなくなります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=usd \
  -d confirm=true \
  -d "payment_method={{PAYMENTMETHOD_ID}}" \
  -d "allowed_payment_method_types[]=card"
```
