# ラインアイテムの数量を調整可能にする 顧客が決済時にラインアイテムの数量を調整できるようにします。 # ホスト型ページ > This is a ホスト型ページ for when payment-ui is stripe-hosted. View the full page at https://docs.stripe.com/payments/checkout/adjustable-quantity?payment-ui=stripe-hosted. 各 [Checkout セッション](https://docs.stripe.com/api/checkout/sessions.md)のラインアイテムは、顧客が購入しようとしているものを追跡します。顧客が決済時にラインアイテムの数量を調整できるように、Checkout セッションを設定できます。 ## 調整数量が適用された Checkout セッションを作成する Checkout セッションを作成する際、`line_items` に `adjustable_quantity` を設定して、決済時に顧客が商品の数量を変更できるようにします。 `adjustable_quantity.minimum` と `adjustable_quantity.maximum` を設定することで、デフォルトの最小 / 最大数量をカスタマイズできます。デフォルトでは、商品の最小調整数量は `0`、最大調整数量は `99` に設定されています。`adjustable_quantity.maximum` には最大 `999999` の値を指定できます。 `line_items[].quantity` の値が `99` (デフォルトの最大調整数量) よりも大きい調整数量を適用する場合、`adjustable_quantity.maximum` をその商品の数量以上の値に設定してください。 調整数量を適用する場合は、Checkout セッションの作成時に `adjustable_quantity.maximum` を使用して、`line_items` の数量の代わりに在庫数量をリザーブするように設定を変更します。 Checkout では、項目が残り 1 つの場合、顧客はそれを削除できません。 ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ -d "line_items[0][price_data][currency]=usd" \ -d "line_items[0][price_data][product_data][name]=T-shirt" \ -d "line_items[0][price_data][unit_amount]=2000" \ -d "line_items[0][price_data][tax_behavior]=exclusive" \ -d "line_items[0][adjustable_quantity][enabled]=true" \ -d "line_items[0][adjustable_quantity][minimum]=1" \ -d "line_items[0][adjustable_quantity][maximum]=10" \ -d "line_items[0][quantity]=1" \ -d "automatic_tax[enabled]=true" \ -d mode=payment \ --data-urlencode "success_url=https://example.com/success" ``` ## 完了した取引を処理する 決済が完了したら、確定済みの[明細項目](https://docs.stripe.com/api/checkout/sessions/line_items.md)とその数量に対するリクエストを作成できます。顧客が明細項目を削除した場合、その項目は明細項目レスポンスからも削除されます。完了した Checkout Sessions を処理するイベントハンドラーの作成方法については、[フルフィルメントガイド](https://docs.stripe.com/checkout/fulfillment.md)をご覧ください。 > イベントハンドラをテストするには、[Stripe CLI をインストール](https://docs.stripe.com/stripe-cli.md)し、`stripe listen --forward-to localhost:4242/webhook` を使用して[イベントをローカルサーバーに転送](https://docs.stripe.com/webhooks.md#test-webhook)します。 #### Ruby ```ruby # 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 = "<>" require 'sinatra' # You can find your endpoint's secret in your webhook settings endpoint_secret = 'whsec_...' post '/webhook' do event = nil # Verify webhook signature and extract the event # See https://stripe.com/docs/webhooks#verify-events for more information. begin sig_header = request.env['HTTP_STRIPE_SIGNATURE'] payload = request.body.read event = Stripe::Webhook.construct_event(payload, sig_header, endpoint_secret) rescue JSON::ParserError => e # Invalid payload return status 400 rescue Stripe::SignatureVerificationError => e # Invalid signature return status 400 end if event['type'] == 'checkout.session.completed' checkout_session = event['data']['object'] line_items = Stripe::Checkout::Session.list_line_items(checkout_session['id'], {limit: 100}) # Fulfill the purchase... begin fulfill_order(checkout_session, line_items) rescue NotImplementedError => e return status 400 end end status 200 end def fulfill_order(checkout_session, line_items) # TODO: Remove error and implement... raise NotImplementedError.new(<<~MSG) Given the Checkout Session "#{checkout_session.id}" load your internal order from the database here. Then you can reconcile your order's quantities with the final line item quantity purchased. You can use `checkout_session.metadata` and `price.metadata` to store and later reference your internal order and item ids. MSG end ``` # 埋め込みページ > This is a 埋め込みページ for when payment-ui is embedded-form. View the full page at https://docs.stripe.com/payments/checkout/adjustable-quantity?payment-ui=embedded-form. 各 [Checkout セッション](https://docs.stripe.com/api/checkout/sessions.md)のラインアイテムは、顧客が購入しようとしているものを追跡します。顧客が決済時にラインアイテムの数量を調整できるように、Checkout セッションを設定できます。 ## 調整数量が適用された Checkout セッションを作成する Checkout セッションを作成する際、`line_items` に `adjustable_quantity` を設定して、決済時に顧客が商品の数量を変更できるようにします。 `adjustable_quantity.minimum` と `adjustable_quantity.maximum` を設定することで、デフォルトの最小 / 最大数量をカスタマイズできます。デフォルトでは、商品の最小調整数量は `0`、最大調整数量は `99` に設定されています。`adjustable_quantity.maximum` には最大 `999999` の値を指定できます。 `line_items[].quantity` の値が `99` (デフォルトの最大調整数量) よりも大きい調整数量を適用する場合、`adjustable_quantity.maximum` をその商品の数量以上の値に設定してください。 調整数量を適用する場合は、Checkout セッションの作成時に `adjustable_quantity.maximum` を使用して、`line_items` の数量の代わりに在庫数量をリザーブするように設定を変更します。 Checkout では、項目が残り 1 つの場合、顧客はそれを削除できません。 ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ -d "line_items[0][price_data][currency]=usd" \ -d "line_items[0][price_data][product_data][name]=T-shirt" \ -d "line_items[0][price_data][unit_amount]=2000" \ -d "line_items[0][price_data][tax_behavior]=exclusive" \ -d "line_items[0][adjustable_quantity][enabled]=true" \ -d "line_items[0][adjustable_quantity][minimum]=1" \ -d "line_items[0][adjustable_quantity][maximum]=10" \ -d "line_items[0][quantity]=1" \ -d "automatic_tax[enabled]=true" \ -d mode=payment \ -d ui_mode=embedded_page \ --data-urlencode "return_url=https://example.com/return" ``` ## 完了した取引を処理する 決済が完了したら、確定済みの[明細項目](https://docs.stripe.com/api/checkout/sessions/line_items.md)とその数量に対するリクエストを作成できます。顧客が明細項目を削除した場合、その項目は明細項目レスポンスからも削除されます。完了した Checkout Sessions を処理するイベントハンドラーの作成方法については、[フルフィルメントガイド](https://docs.stripe.com/checkout/fulfillment.md)をご覧ください。 > イベントハンドラをテストするには、[Stripe CLI をインストール](https://docs.stripe.com/stripe-cli.md)し、`stripe listen --forward-to localhost:4242/webhook` を使用して[イベントをローカルサーバーに転送](https://docs.stripe.com/webhooks.md#test-webhook)します。 #### Ruby ```ruby # 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 = "<>" require 'sinatra' # You can find your endpoint's secret in your webhook settings endpoint_secret = 'whsec_...' post '/webhook' do event = nil # Verify webhook signature and extract the event # See https://stripe.com/docs/webhooks#verify-events for more information. begin sig_header = request.env['HTTP_STRIPE_SIGNATURE'] payload = request.body.read event = Stripe::Webhook.construct_event(payload, sig_header, endpoint_secret) rescue JSON::ParserError => e # Invalid payload return status 400 rescue Stripe::SignatureVerificationError => e # Invalid signature return status 400 end if event['type'] == 'checkout.session.completed' checkout_session = event['data']['object'] line_items = Stripe::Checkout::Session.list_line_items(checkout_session['id'], {limit: 100}) # Fulfill the purchase... begin fulfill_order(checkout_session, line_items) rescue NotImplementedError => e return status 400 end end status 200 end def fulfill_order(checkout_session, line_items) # TODO: Remove error and implement... raise NotImplementedError.new(<<~MSG) Given the Checkout Session "#{checkout_session.id}" load your internal order from the database here. Then you can reconcile your order's quantities with the final line item quantity purchased. You can use `checkout_session.metadata` and `price.metadata` to store and later reference your internal order and item ids. MSG end ```