Dynamically update discounts
Learn how to apply and modify discount codes during checkout.
Private preview
Dynamic discounts is in private preview. Request access to dynamic updates to checkout.
Checkout Session で割引を動的に追加または削除する方法をご紹介します。
ご利用事例
このガイドでは、社内の割引システムを導入して動的な金額割引を作成する方法を説明します。合わせて、次についても理解できます。
- ロイヤルティ割引の適用: 顧客ロイヤルティ段階または購入履歴に基づき、割引を自動的に適用します。
- カート金額のプロモーション: 注文合計が特定の基準値を超えた場合に割引を追加します (たとえば、100 ドルを超える注文から 10 ドル割引など)。
- 時間的制約のあるオファー: 期間限定のプロモーション割引を適用したり、期限切れの割引コードを削除したりできます。
- ロケーションベースの割引: 顧客の配送先住所に基づいて、地域別に割引を適用します。
- 顧客固有のオファー: 顧客セグメントまたは以前の購買行動に基づき、個人に合わせた割引を作成します。
Payment Intents API
If you use the Payment Intents API, you can apply discounts by manually calculating and modifying the payment amount, or by creating a new PaymentIntent with adjusted amounts.
Set up the SDKServer-side
Stripe の公式ライブラリを使用して、アプリケーションから Stripe API にアクセスします。
Update the server SDKServer-side
To use this preview feature, first update your SDK to use the checkout_
beta version header.
Configure update permissions for the Checkout SessionServer-side
When you create the Checkout Session, pass the permissions.update_discounts=server_only option to disable applying client-side discounts and to enable updating discounts from your server.
Dynamically update discountsServer-side
Create an endpoint on your server to apply discounts on the Checkout Session. You’ll call this from the front end in a later step.
セキュリティのヒント
Client-side code runs in an environment that’s controlled by the user. A malicious user can bypass your client-side validation, intercept and modify requests, or create new requests to your server.
When creating an endpoint, we recommend the following:
- Create endpoints for specific customer interactions instead of making them generic. For example, “apply loyalty discount” instead of a general “update” action. Specific endpoints can help with writing and maintaining validation logic.
- Don’t pass session data directly from the client to your endpoint. Malicious clients can modify request data, making it an unreliable source for determining the Checkout Session state. Instead, pass the session ID to your server and use it to securely retrieve the data from the Stripe API.
Test the integration
導入内容をテストして、動的な割引が正しく機能することを確認するには、以下のステップに従います。
本番環境を反映したサンドボックス環境を設定します。この環境では、Stripe サンドボックスの API キーを使用してください。
さまざまな割引シナリオをシミュレーションして、
recomputeDiscounts
関数がさまざまなシナリオを正しく処理することを確認できます。ログツールやデバッグツールを使用してサーバーが以下を行っていることを確認し、サーバー側のロジックを検証します。
- Checkout Session (セッション) を取得します。
- 割引リクエストを検証します。
- ご自身の事業のロジックに基づき、更新された割引を再計算します。
- カスタム条件が満たされた場合に Checkout Sessionを新しい割引で更新します。更新のレスポンスに新しい割引が示されていることを確認します。デフォルトでは、リクエストがオブジェクトを拡張しない限り、レスポンスには割引のフィールドは示されません。
ブラウザーで決済プロセスを複数回実行し、クライアント側のロジックを検証します。割引の適用後に UI がどのように更新されるかに注意してください。以下を確認してください。
- runServerUpdate 関数が想定どおりに呼び出される。
- 割引は事業のロジックに基づき正しく適用されます。
- Checkout の合計が更新され、適用される割引が反映されます。
- 割引の申し込みが失敗した場合は、エラーメッセージが正しく表示されます。
無効な割引リクエストなど、さまざまな割引シナリオをテストするか、サーバーエラーをシミュレーションして、サーバー側とクライアント側の両方でエラー処理をテストします。