# Confirmation Tokensに移行する PaymentMethod の代わりに ConfirmationToken を使用して、サーバーで支払いを確定します。 このガイドを使用して、[PaymentMethod](https://docs.stripe.com/api/payment_methods.md) ではなく、[ConfirmationToken](https://docs.stripe.com/api/confirmation_tokens/object.md) を使用してサーバーで支払いを確定し、クライアントから収集したデータを自社のサーバーに送信する方法をご覧ください。 `ConfirmationToken` は、配送先情報など、`PaymentMethod` で見つかったデータのスーパーセットを保持し、Stripe が構築した新しい機能を有効化します。 ## 確認トークンを作成する [クライアント側] [stripe.createPaymentMethod](https://docs.stripe.com/js/payment_methods/create_payment_method_elements) を呼び出すのではなく、[stripe.createConfirmationToken](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token) を呼び出して、`ConfirmationToken` オブジェクトを作成します。この ConfirmationToken をサーバーに渡して PaymentIntent を確定します。 `stripe.createConfirmationToken` メソッドは、([params.payment_method_data](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token#create_confirmation_token-options-params-payment_method_data) を介して) `stripe.createPaymentMethod` と同じパラメーターを受け付けます。このほか、[shipping](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token#create_confirmation_token-options-params-shipping) と [return_url](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token#create_confirmation_token-options-params-return_url) パラメーターも受け付けます。 ### Before ```javascript if (error) { // This point is only reached if there's an immediate error when creating the PaymentMethod. // Show the error to your customer (for example, payment details incomplete) handleError(error); return; } // Create and confirm the PaymentIntent const res = await fetch("/create-confirm-intent", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({ }), }); ``` ### After ```javascript // Create the ConfirmationToken using the details collected by the Payment Element and additional shipping information. Provide shipping and return_url if you don't want to provide it when confirming the intent on the server const {error, confirmationToken} = await stripe.createConfirmationToken({ elements, params: { payment_method_data: { billing_details: { name: 'Jenny Rosen', } }, // Remove shipping if you're collecting it using Address Element or don't require it shipping: { name: 'Jenny Rosen', address: { line1: '1234 Main Street', city: 'San Francisco', state: 'CA', country: 'US', postal_code: '94111', }, }, return_url: 'https://example.com/order/123/complete', } }); if (error) { // This point is only reached if there's an immediate error when creating the ConfirmationToken. // Show the error to your customer (for example, payment details incomplete) handleError(error); return; } // Create and confirm the PaymentIntent const res = await fetch("/create-confirm-intent", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({confirmationTokenId: confirmationToken.id, }), }); ``` ## 決済を作成して Stripe に送信する [サーバー側] 以前のように [PaymentMethod](https://docs.stripe.com/api/payment_methods.md) ではなく、ConfirmationToken をサーバーに渡して[PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を確定します。`ConfirmationToken` に保存されたプロパティは、確定時に ID が [confirmation_token](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-confirmation_token) パラメーターに指定されたときに、Intent に適用されます。 > ConfirmationToken にすでに [shipping](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token#create_confirmation_token-options-params-shipping) と [return_url](https://docs.stripe.com/js/confirmation_tokens/create_confirmation_token#create_confirmation_token-options-params-return_url) を指定している場合、 PaymentIntent を確定する際に、これらのフィールドを再度指定する必要はありません。 ### Before ```javascript app.post('/create-confirm-intent', async (req, res) => { try { const intent = await stripe.paymentIntents.create({ confirm: true, amount: 1099, currency: 'usd', // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. automatic_payment_methods: {enabled: true}, use_stripe_sdk: true, }); res.json({ client_secret: intent.client_secret, status: intent.status }); } catch (err) { res.json({ error: err }) } }); ``` ### After ```javascript app.post('/create-confirm-intent', async (req, res) => { try { const intent = await stripe.paymentIntents.create({ confirm: true, amount: 1099, currency: 'usd', // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. automatic_payment_methods: {enabled: true}, use_stripe_sdk: true,// the ConfirmationToken ID sent by your client that already has the shipping, mandate_data, and return_url data confirmation_token: req.body.confirmationTokenId, }); res.json({ client_secret: intent.client_secret, status: intent.status }); } catch (err) { res.json({ error: err }) } }); ``` 確定時に PaymentIntent または SetupIntent に直接指定されるパラメーター (`shipping` など) は、ConfirmationToken の対応するプロパティーを上書きします。 ## Optional: 支払い方法に基づいた条件パラメーター setup_future_usage または capture_method を設定する [ConfirmationToken](https://docs.stripe.com/api/confirmation_tokens/object.md) を使用して、クライアント設定がサーバー設定と一致していることを確認する検証を強化しました。ただし、買い手の選択した支払い方法に基づいて、`PaymentIntent` または `SetupIntent` に `setup_future_usage` または `capture_method` を条件付きで設定している場合、構築済みの Stripe システムと競合する可能性があります。この問題が発生した場合、適切な対応方法は以下のとおりです。 1. Elements をインスタンス化する際に、`setup_future_usage` や `capture_method` を設定「しない」。 1. Intent の上位レベルのパラメーターには、`setup_future_usage` や `capture_method` を設定「しない」 (例 : `paymentIntent.create({ setup_future_usage = ‘off_session’})`)。 1. Intent の `payment_method_options` パラメーター内の各支払い方法に、`setup_future_usage` パラメーターまたは `capture_method` パラメーターを設定する。例: ```javascript stripe.paymentIntents.create({ amount: 100, currency: 'USD', payment_method_options: { card: { setup_future_usage: 'off_session', capture_method: 'manual' }, ideal: { setup_future_usage: 'off_session' } } }); ``` ## 参照情報 - [導入方法の設計](https://docs.stripe.com/payments/payment-element/design-an-integration.md)