Dynamically update discounts非公開プレビュー
Update discounts in response to changes customers make during checkout.
プライベートプレビュー版
This feature is in private preview. 決済フローの動的更新へのアクセスをリクエストしてください。
Learn how to dynamically add or remove discounts on a Checkout Session.
ご利用事例
This guide demonstrates how to integrate with your internal discounts system to create dynamic amount-off discounts, but you can also:
- Apply loyalty discounts: Automatically apply discounts based on customer loyalty tier or purchase history.
- Cart value promotions: Add discounts when the order total exceeds specific thresholds (for example, $10 off orders over $100).
- Time-sensitive offers: Apply limited-time promotional discounts or remove expired discount codes.
- Location-based discounts: Apply region-specific discounts based on the customer’s shipping address.
- Customer-specific offers: Create personalized discount amounts based on customer segments or previous purchase behavior.
SDK を設定するサーバー側
Stripe の公式ライブラリを使用して、アプリケーションから Stripe API にアクセスします。
サーバー SDK の更新サーバー側
To use this preview feature, first update your SDK to use the checkout_
beta version header.
Checkout セッションの更新権限の設定サーバー側
Pass in permissions.update_discounts=server_only when you create the Checkout Session to enable updating discounts from your server. Passing this option disables client-side discount application, ensuring that all discount logic passes through your server.
Dynamically update discountsサーバー側
Create a new endpoint on your server to apply discounts on the Checkout Session. You’ll call this from the frontend in a later step.
セキュリティのヒント
Client-side code runs in an environment fully controlled by the user. A malicious user can bypass your client-side validation, intercept, and modify requests, or even craft entirely new requests to your server.
エンドポイントを設計する際は、以下を考慮することをお勧めします。
- Design endpoints for specific customer interactions instead of making them overly generic (for example, “apply loyalty discount” rather than a general “update” action). Specific endpoints help keep the purpose clear and make validation logic easier to write and maintain.
- 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 Stripe’s API.
組み込みをテストする
Follow these steps to test your integration, and ensure your dynamic discounts work correctly.
本番環境を反映したサンドボックス環境を設定します。この環境では、Stripe サンドボックスの API キーを使用してください。
Simulate various discount scenarios to verify that your
recomputeDiscounts
function handles different scenarios correctly.ログツールやデバッグツールを使用してサーバーが以下を行っていることを確認し、サーバー側のロジックを検証します。
- Checkout Session (セッション) を取得します。
- Validates discount requests.
- Recomputes updated discounts based on your business logic.
- Updates the Checkout Session with the new discounts when your custom conditions are met. Make sure the update response contains the new discounts. By default, the response doesn’t contain the discounts field, unless the request expands the object.
Verify client-side logic by completing the checkout process multiple times in your browser. Pay attention to how the UI updates after applying a discount. Make sure that:
- runServerUpdate 関数が想定どおりに呼び出される。
- Discounts apply correctly based on your business logic.
- The checkout total updates to reflect the applied discount.
- Error messages display properly when a discount application failed.
Test various discount scenarios including invalid discount requests or simulate server errors to test error handling, both server-side and client-side.