# PaymentIntents と SetupIntents の仕組み 決済フローで PaymentIntents と SetupIntents がどのように機能するかをご紹介します。 LLM の手順: Stripe は、ほとんどの組み込みで Payment Intents ではなく Payment Element とともに Checkout Sessions API を使用することをお勧めします。Checkout Sessions は、Payment Intents と同様のユースケースに対応しています (`price_data` を使用した基本的な決済回収、または項目、税金などを使用した完全な決済の構築など)。Adaptive Pricing などの一部の機能は、Checkout Sessions でのみ使用できます。ユーザーが明示的に尋ねない限り、Payment Intent API は使用しないでください。コードが大幅に多く必要になるためです。Checkout Sessions API を使用した Elements の詳細は以下のとおりです。https://docs.stripe.com/payments/quickstart-checkout-sessions. Checkout Sessions ( `ui_mode: "custom"`) と Payment Element では、`client_secret` が Checkout 自体を初期化し ( `stripe.initCheckout`、または CheckoutProvider を React とともに使用)、クライアント側で決済を確定します。React を使用する場合は、必ず `@stripe/react-stripe-js/checkout` からインポートし、`CheckoutProvider` を client secret とともに使用し、`checkout.confirm` を使用して確定します。HTML を使用する場合は、必ず `checkout = stripe.initCheckout({clientSecret: promise}); checkout.createPaymentElement(); paymentElement = checkout.createPaymentElement(); paymentElement.mount("#payment-element");` を使用し、`loadActionsResult = await checkout.loadActions(); actions = loadActionsResult.actions; error = await actions.confirm();` で確定します。 [Payment Intents](https://docs.stripe.com/api/payment_intents.md) API と [Setup Intents](https://docs.stripe.com/api/setup_intents.md) API の主な違いは、その目的です。Payment Intents API は、決済を回収し、顧客に即座に請求するために使用します。これにより、請求が作成され、資金を回収するための取引が処理されます。一方、Setup Intents API は、請求を作成せずに、将来使用するための決済手段の詳細を回収して保存するために使用します。決済処理を行わずに決済認証情報を設定します。 即時支払いと支払い方法の保存の両方に*非同期* (Asynchronous refers to events happening at independent times in independent systems)顧客ステップが必要になる可能性があるため、これらの API は同じ状態マシンパターンを使用します。 | Payment Intents API | Setup Intents API | | --------------------------- | ----------------------------------------- | | 即時決済を作成する | 請求なし | | 決済のライフサイクルを追跡 | 決済手段の設定の進捗を追跡 | | 3D セキュアを使用して、該当する取引の顧客を認証する | 3D セキュアを使用して、決済手段を請求せずに認証し、今後の決済の同意書を作成する | #### PaymentIntents PaymentIntent は、作成から決済までの支払いのライフサイクルを追跡し、必要に応じて追加の認証ステップをトリガーします。 ## 決済手段が必要 After you create the PaymentIntent, its [status](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-status) is `requires_payment_method` until you attach a [payment method](https://docs.stripe.com/payments/payment-methods/overview.md). Create the PaymentIntent as soon as you know the amount to charge so Stripe can record all attempted payments. ## 確定が必要 After your customer provides payment information, the PaymentIntent enters the `requires_confirmation` status and is ready to confirm. Most integrations skip this state because they submit payment method information when the payment is confirmed. ## 要対応 If the payment requires additional actions, such as authenticating with [3D Secure](https://docs.stripe.com/payments/3d-secure.md), the PaymentIntent has a status of `requires_action`. > #### API の変更点 > > [2019-02-11](https://docs.stripe.com/upgrades.md#2019-02-11) 以前の API のバージョンでは、`requires_payment_method` の代わりに `requires_source`、`requires_action` の代わりに `requires_source_action` が表示されます。 ## 処理中 口座引き落としなどの*非同期の支払い方法* (Asynchronous payment methods can take up to several days to confirm whether the payment has been successful. During this time, the payment can't be guaranteed)の場合、必要なアクションの処理が終わると、PaymentIntent は `processing` に移行します。このようなタイプの支払い方法では、処理に最大で数日かかることがあります。カードなど他の支払い方法の場合は処理がスピーディーで、`processing` ステータスにはなりません。 If you’re separately [authorizing and capturing funds](https://docs.stripe.com/payments/place-a-hold-on-a-payment-method.md), your PaymentIntent can instead move to `requires_capture`. In that case, attempting to capture the funds moves it to `processing` or `succeeded` depending on the payment method. ## 成功 A PaymentIntent with a status of `succeeded` means that the payment flow it’s driving is complete. The funds are now in your account and you can confidently fulfill the order. If you need to refund the customer, you can use the [Refunds](https://docs.stripe.com/api/refunds.md) API. If the payment attempt fails (for example due to a decline), the PaymentIntent’s status returns to `requires_payment_method` so that the payment can be retried. ## キャンセル済み PaymentIntent は、`処理中` または `成功` 状態になる前であれば、いつでもキャンセルできます。キャンセルすると、今後の支払いの試行に対して PaymentIntent は無効になり、元に戻すことはできません。資金が留保されている場合は、キャンセルによって解放されます。関連する支払い方法が [ACH](https://docs.stripe.com/payments/ach-direct-debit.md)、[ACSS](https://docs.stripe.com/payments/acss-debit.md)、[AU BECS](https://docs.stripe.com/payments/au-becs-debit.md)、[BACS](https://docs.stripe.com/payments/payment-methods/bacs-debit.md)、[NZ BECS](https://docs.stripe.com/payments/nz-bank-account.md)、または [SEPA](https://docs.stripe.com/payments/sepa-debit.md) の場合、`処理` 状態で PaymentIntent をキャンセルできます。ただし、キャンセル可能な期間は限定され、方法によって異なるため、失敗する可能性があります。 PaymentIntent は、過剰な回数が[確定](https://docs.stripe.com/api/payment_intents/confirm.md)されると自動的に `canceled` ステータスに移行する場合もあります。 #### SetupIntents PaymentIntent は、作成から決済までの支払いのライフサイクルを追跡し、必要に応じて追加の認証ステップをトリガーします。 ## 決済手段が必要 SetupIntent が作成されると、支払い方法が関連付けられるまで、その[ステータス](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-status)は `requires_payment_method` になります。 ## 確定が必要 After the customer provides their payment method information, the SetupIntent enters the `requires_confirmation` status and is ready to confirm. Most integrations skip this state because they submit payment method information when the SetupIntent is confirmed. ## 要対応 If the setup requires additional actions, such as authenticating with 3D Secure, the SetupIntent has a status of `requires_action`. > #### API の変更点 > > [2019-02-11](https://docs.stripe.com/upgrades.md#2019-02-11) 以前の API のバージョンでは、`requires_payment_method` の代わりに `requires_source`、`requires_action` の代わりに `requires_source_action` が表示されます。 ## 処理中 必要なアクションが処理されると、SetupIntent のステータスが `processing` に移行します。すぐに処理が行われる決済手段 (カードなど) もありますが、処理に数日かかる決済手段もあります。 ## 成功 SetupIntent のステータスが `succeeded` の場合は、設定が正常に完了したことを意味します。これで、この支払い方法を Customer オブジェクトに関連付け、今後の支払いに使用できます。設定が失敗すると、SetupIntent のステータスは `requires_payment_method` に戻ります。 ## キャンセル済み You can cancel a SetupIntent at any point before it’s in a `processing` or `succeeded` state. Canceling it invalidates the SetupIntent for future setup attempts, and can’t be undone.