Accéder directement au contenu
Créez un compte
ou
connecter-vous
Logo de la documentation Stripe
/
Demander à l'assistant IA
Créez un compte
Connectez-vous
Démarrer
Paiements
Revenus
Plateformes et places de marché
Gestion de fonds
Ressources pour les développeurs
Aperçu
À propos des paiements Stripe
Mettre votre intégration à niveau
Analyses des paiements
Paiements en ligne
PrésentationTrouver votre cas d'usageUse Managed Payments
Utiliser Payment Links
Utiliser une page de paiement préconfigurée
Build a custom integration with Elements
    Présentation
    Compare Checkout Sessions and PaymentIntents
    Quickstart guides
    Concevoir une intégration avancée
    Personnaliser l'apparence
    Gérer les moyens de paiement
    Collecter des informations supplémentaires
    Créer une intégration pour les abonnements
    Dynamic updates
      Shipping options
      Postes de facture
      Trial durations
      Discounts
      Payment amounts
      Line item quantities
    Ajouter des réductions
    Percevoir les taxes sur vos paiements
    Offrir aux clients la possibilité de payer dans leur devise locale
    Enregistrer et récupérer les moyens de paiement des clients
    Envoyer des reçus ou factures après paiement
    Approuver manuellement les paiements sur votre serveur
    Autoriser et capturer un paiement séparément
    Liste des modifications de la version bêta d'Elements avec l'API Checkout Sessions
Développer une intégration dans l'application
Moyens de paiement
Ajouter des moyens de paiement
Gérer les moyens de paiement
Paiement accéléré avec Link
Interfaces de paiement
Payment Links
Checkout
Elements pour le web
Paiements dans l'application
Scénarios de paiement
Gérer plusieurs devises
Tunnels de paiement personnalisés
Acquisition flexible
Orchestration
Paiements par TPE
Terminal
Au-delà des paiements
Constituez votre entreprise
Cryptomonnaies
Financial Connections
Climate
Comprendre la fraude
Radar pour la protection contre la fraude
Gestion des litiges
Vérifier l'identité
AccueilPaiementsBuild a custom integration with ElementsDynamic updates

Remarque

Cette page n'est pas encore disponible dans cette langue. Nous faisons tout notre possible pour proposer notre documentation dans davantage de langues et nous vous fournirons la version traduite dès qu'elle sera disponible.

Dynamically customize shipping options

Learn how to create different shipping rates for your customers.

Learn how to dynamically update shipping options based on the address that your customer enters in Checkout.

Use cases

  • Validate an address: Confirm whether you can ship a product to a customer’s address using your own custom validation rules. You can also create a custom UI for customers to confirm their preferred address.
  • Show relevant shipping options: Display only available shipping methods, based on the customer’s address. For example, show overnight shipping only for deliveries in your country.
  • Dynamically calculate shipping rates: Calculate and display shipping fees based on a customer’s delivery address.
  • Update shipping rates based on order total: Offer shipping rates based on the shipping address or order total, such as free shipping for orders over 100 USD. For checkouts allowing quantity changes or cross-sells, see Dynamically updating line items.

Limitations

  • Only supported in payment mode. Shipping rates aren’t available in subscription mode.

Payment Intents API

If you use the Payment Intents API, you must manually update shipping options and modify the payment amount based on a selected shipping option, or by creating a new PaymentIntent with adjusted amounts.

Configure update permissions for the Checkout Session
Server-side

Set the shipping_address_collection.allowed_countries to the list of countries you want to offer shipping to.

When you create the Checkout Session, pass the permissions.update_shipping_details=server_only option to disable the client-side updateShippingAddress method and to enable updating the shipping address and shipping options from your server.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/checkout/sessions \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d ui_mode=custom \ -d "permissions[update_shipping_details]"=server_only \ -d "shipping_address_collection[allowed_countries][0]"=US \ -d "line_items[0][price]"=
{{PRICE_ID}}
\ -d "line_items[0][quantity]"=1 \ -d mode=payment \ --data-urlencode return_url="https://example.com/return"

Customize shipping options
Server-side

Create an endpoint on your server to calculate the shipping options based on the customer’s shipping address.

  1. Retrieve the Checkout Session using the checkoutSessionId from the request body.
  2. Validate the customer’s shipping details from the request body.
  3. Calculate the shipping options based on the customer’s shipping address and the line items in the Checkout Session.
  4. Update the Checkout Session with the customer’s shipping_details and the shipping_options.
Ruby
Python
PHP
Node.js
.NET
Go
Java
No results
require 'sinatra' require 'json' require 'stripe' set :port, 4242 # Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/apikeys Stripe.api_key =
"sk_test_BQokikJOvBiI2HlWgH4olfQ2"
# Return a boolean indicating whether the shipping details are valid def validate_shipping_details(shipping_details) # TODO: Remove error and implement... raise NotImplementedError.new(<<~MSG) Validate the shipping details the customer has entered. MSG end # Return an array of the updated shipping options or the original options if no update is needed def calculate_shipping_options(shipping_details, session) # TODO: Remove error and implement... raise NotImplementedError.new(<<~MSG) Calculate shipping options based on the customer's shipping details and the Checkout Session's line items. MSG end post '/calculate-shipping-options' do content_type :json request.body.rewind

Update the client SDK
Client-side

Initialize Stripe.js with the custom_checkout_server_updates_1 beta header.

checkout.js
const stripe = Stripe(
'pk_test_TYooMQauvdEDq54NiTphI7jx'
, { betas: ['custom_checkout_server_updates_1'], });

Request server updates
Client-side

Create an asynchronous function that makes a request to your server to update the shipping options and wrap it in runServerUpdate. A successful request updates the Session object with the new shipping options.

The following code example shows how to update the shipping options with the AddressElement.

index.html
<div id="shipping-form"> <!-- Shipping Address Element will be mounted here --> <div id="shipping-address-element"></div> <button id="save-button">Save</button> <div id="error-message"></div> </div> <div id="shipping-display" style="display: none"> <div id="address-display"></div> <button id="edit-button">Edit</button> </div>
checkout.js
// mount the Shipping Address Element const shippingAddressElement = checkout.createShippingAddressElement(); shippingAddressElement.mount('#shipping-address-element'); const toggleViews = (isEditing) => { document.getElementById('shipping-form').style.display = isEditing ? 'block' : 'none'; document.getElementById('shipping-display').style.display = isEditing ? 'none' : 'block'; } const displayAddress = (address) => { const displayDiv = document.getElementById('address-display'); displayDiv.innerHTML = ` <div>${address.name}</div> <div>${address.address.line1}</div> <div>${address.address.city}, ${address.address.state} ${address.address.postal_code}</div> `; } const updateShippingOptions = async (shippingDetails) => { const response = await fetch("/calculate-shipping-options", { method: "POST", headers: { 'Content-type': 'application/json' }, body: JSON.stringify({ checkout_session_id: 'session_id', shipping_details: shippingDetails }) }); const result = await response.json();

Test the integration

Follow these steps to test your integration, and ensure your custom shipping options work correctly.

  1. Set up a sandbox environment that mirrors your production setup. Use your Stripe sandbox API keys for this environment.

  2. Simulate various shipping addresses to verify that your calculateShippingOptions function handles different scenarios correctly.

  3. Verify server-side logic by using logging or debugging tools to confirm that your server:

    • Retrieves the Checkout Session.
    • Validates shipping details.
    • Calculates shipping options.
    • Updates the Checkout Session with new shipping details and options. Make sure the update response contains the new shipping details and options.
  4. Verify client-side logic by completing the checkout process multiple times in your browser. Pay attention to how the UI updates after entering shipping details. Make sure that:

    • The runServerUpdate function is called when expected.
    • Shipping options update correctly based on the provided address.
    • Error messages display properly when shipping is unavailable.
  5. Enter invalid shipping addresses or simulate server errors to test error handling, both server-side and client-side.

Cette page vous a-t-elle été utile ?
OuiNon
  • Besoin d'aide ? Contactez le service Support.
  • Rejoignez notre programme d'accès anticipé.
  • Consultez notre log des modifications.
  • Des questions ? Contactez l'équipe commerciale.
  • LLM ? Lire llms.txt.
  • Propulsé par Markdoc