Dynamically update line itemsPrivate Vorschau
Update line items in response to changes made during checkout
Beta
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.
Limitations
- 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.
Set up StripeServer-side
First, you need a Stripe account. Register now.
Use our official libraries to access the Stripe API from your application:
Set the SDK to use the checkout_
beta version header.
Create a Checkout SessionServer-side
From your server, create a Checkout Session.
- Set the ui_mode to
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.
Achtung
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 itemsServer-side
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.
Mount CheckoutClient-side
Vorsicht
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. - When the result has
type: "reject"
, the Checkout UI shows the error message that you set in the result.
Checkout renders in an iframe that securely sends payment information to Stripe over an HTTPS connection.
Häufiger Fehler
Avoid placing Checkout within another iframe because some payment methods require redirecting to another page for payment confirmation.
Test the integration
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.