配送オプションを動的にカスタマイズする
顧客の配送先住所に基づいて配送オプションを更新します。
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.
- Doesn’t support updates in response to changes from outside of the checkout page.
Checkout セッションを作成するサーバー側
サーバーで Checkout セッションを作成します。
- ui_mode を
embedded
に設定します。 - Set the permissions.update_shipping_details to
server_
.only - shipping_address_collection.allowed_countries に、配送先を提供する国のリストを設定します。
- shipping_options.shipping_rate_data を設定して、0 USD のダミー配送料を作成します。
デフォルトでは、Stripe Checkout クライアントは、Checkout セッションオブジェクトの shipping_details を、顧客が入力する顧客の名前や住所などの配送情報で自動的に更新します。
警告
When permissions.update_shipping_details is server_
, it disables the automatic client-side update and only your server can update the shipping details using your Stripe secret key.
配送オプションをカスタマイズするサーバー側
サーバーで、顧客の配送先住所に基づいて配送オプションを計算する新しいエンドポイントを作成します。
- リクエスト本文から取得した
checkoutSessionId
を使用して、Checkout セッションを取得します。 - リクエスト本文にある顧客の配送先情報を検証します。
- 顧客の配送先住所と Checkout セッションのラインアイテムに基づいて配送オプションを計算します。
- 顧客の shipping_details と shipping_options で Checkout セッションを更新します。
Checkout をマウントするクライアント側
注意
onShippingDetailsChange
関数からは必ず Promise
が返され、ResultAction オブジェクトで解決されます。
Checkout クライアントは、onShippingDetailsChange
関数の結果に基づいて UI を更新します。
- 結果に
type: "accept"
が含まれる場合、Checkout UI はサーバーで設定した配送オプションを表示します。 - 結果に
type: "reject"
が含まれる場合、Checkout UI は結果で設定したエラーメッセージを表示します。
必要に応じて、onShippingDetailsChange
をリッスンし、複数の可能な住所から希望の住所を選択して確認するための、カスタム UI を作成できます。
Checkout は、HTTPS 接続を介して支払い情報をStripeに安全に送信する iframe でレンダリングされます。
よくある間違い
一部の支払い方法では、別のページにリダイレクトして支払いを確定する必要があるため、Checkout は別の iframe 内に配置しないでください。
実装をテストする
Follow these steps to test your integration, and ensure your custom shipping options work correctly.
Set up a sandbox environment that mirrors your production setup. Use your Stripe sandbox API keys for this environment.
Simulate various shipping addresses to verify that your
calculateShippingOptions
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 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.
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
onShippingDetailsChange
function is called when expected. - Shipping options update correctly based on the provided address.
- Error messages display properly when shipping is unavailable.
- The
Enter invalid shipping addresses or simulate server errors to test error handling, both server-side and client-side.