Dynamically update line items非公開プレビュー
Update line items in response to changes made during checkout
ベータ
This feature is in private beta. Request access to dynamic updates to checkout.
Learn how to dynamically add, remove, or update products included in a Checkout Session and confirm line item changes server-side, such as quantity updates or the addition or removal of line items.
Possible use cases include:
- Inventory checks: Run inventory checks and holds when customers attempt to change item quantities.
- Add new products: Add a complimentary product if the order total exceeds a specific amount.
- Update shipping rates: If the order total changes, update shipping rates by combining the method described in this guide with what’s out on Customize shipping options during checkout.
- Update tax rates: If you’re not using Stripe Tax, you can dynamically update tax rates on line items based on the shipping address entered.
制限事項
- Only supported in payment mode.
- Doesn’t support price_data as a line_items parameter on Update yet.
- Doesn’t support updates in response to changes from outside of the checkout page.
- Doesn’t work with Adaptive Pricing yet. If you enable Adaptive Pricing and use this feature, sessions won’t localize to the buyer’s currency.
Stripe を設定するサーバー側
まず、Stripe アカウントが必要です。今すぐご登録ください。
アプリケーションから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。
checkout_
ベータバージョンヘッダーを使用するように SDK を設定します。
Checkout セッションを作成サーバー側
From your server, create a Checkout Session.
- ui_mode を
embedded
に設定します。 - Set the permissions.update.line_items to
server_
.only
By default, the Stripe Checkout client automatically updates the line_items of the Checkout Session object with the updates the customer makes during the session such as modifying the quantity, removing a line item, or adding a cross-sell.
警告
When permissions.update.line_items is server_
, it disables the automatic client-side update and only your server can update the line items using your Stripe secret key.
Dynamically update line itemsサーバー側
From your server, create a new endpoint to recompute the line items based on the updates the customer made.
- Retrieve the Checkout Session using the
checkoutSessionId
from the request body. - Validate the customer’s line items from the request body.
- Recompute the customer’s new line items based on the line item updates they made in the Checkout UI.
- Update the Checkout Session with the customer’s new line_items.
Checkout をマウントするクライアント側
注意
Always return a Promise
from your onLineItemsChange
function and resolve it with a ResultAction object.
The Checkout client updates the UI based on the result of your onLineItemsChange
function.
- When the result has
type: "accept"
, the Checkout UI renders the line items that you set from your server. - 結果に
type: "reject"
が含まれる場合、Checkout UI は結果で設定したエラーメッセージを表示します。
Checkout は、HTTPS 接続を介して支払い情報をStripeに安全に送信する iframe でレンダリングされます。
よくある間違い
一部の支払い方法では、別のページにリダイレクトして支払いを確定する必要があるため、Checkout は別の iframe 内に配置しないでください。
実装内容をテストする
Follow these steps to test your integration, and ensure your dynamic line items work correctly.
Set up a test environment that mirrors your production setup. Use your Stripe test mode API keys for this environment.
Simulate various line item updates to verify that your
recomputeLineItems
function handles different scenarios correctly.Verify server-side logic by using logging or debugging tools to confirm that your server:
- Retrieves the Checkout Session.
- Validates line items.
- Recomputes updated line items.
- Updates the Checkout Session with the new line items when your custom conditions are met. Make sure the update response contains the new line items. By default, the response doesn’t contain the line items 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 making a line item change. Make sure that:
- The
onLineItemsChange
function is called when expected. It’s called whenever a customer modifies the quantity, removes a line item, or adds a cross-sell. - Line items update correctly based on the customer’s line item updates.
- Error messages display properly when an update failed.
- The
Pass through invalid line items or simulate server errors to test error handling, both server-side and client-side.