The line items for each Checkout Session keep track of what your customer is purchasing. You can configure the Checkout Session so customers can adjust line item quantities during checkout.
Note
Other line item updates, such as adding new line items, aren’t supported for this integration.
Set adjustable_quantity on your line_items when creating a Checkout Session to enable your customers to update the quantity of an item during checkout.
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_BQokikJOvBiI2HlWgH4olfQ2
:" \
-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][quantity]"=1 \
-d "line_items[0][adjustable_quantity][enabled]"=true \
-d "line_items[0][adjustable_quantity][maximum]"=100 \
-d "line_items[0][adjustable_quantity][minimum]"=0 \
-d mode=payment \
-d ui_mode=custom \
-d return_url={{RETURN_URL}}
You can customize the default settings for the minimum and maximum quantities allowed by setting adjustable_quantity.minimum and adjustable_quantity.maximum. By default, an item’s minimum adjustable quantity is 0 and the maximum adjustable quantity is 99. You can specify a value of up to 999999 for adjustable_quantity.maximum
.
Checkout prevents the customer from removing an item if it is the only item remaining.
Use updateLineItemQuantity to change a line item’s quantity in response to customer interaction, such as a button to increment the quantity. Pass the line item ID and the new quantity:
<button class="increment-quantity-button" data-line-item="{{line item ID}}">+</button>
stripe.initCheckout({fetchClientSecret}).then((checkout) => {
const button = document.querySelector('.increment-quantity-button');
const lineItem = button.getAttribute("data-line-item");
const quantity = checkout.session().lineItems.find((li) => li.id === lineItem).quantity;
button.addEventListener('click', () => {
checkout.updateLineItemQuantity({
lineItem,
quantity: quantity + 1,
})
})
});
Handle completed transactions
After the payment completes, you can make a request for the finalized line items and their quantities. If your customer removes a line item, it is also removed from the line items response. See the Fulfillment guide to learn how to create an event handler to handle completed Checkout Sessions.
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"
require 'sinatra'
endpoint_secret = 'whsec_...'
post '/webhook' do
event = nil
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
return status 400
rescue Stripe::SignatureVerificationError => e
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})
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)
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