This is a clone of Stripe Docs.
Made using Code Hike. Code available on GitHub.

Prebuilt Checkout page

The cover of Stubborn Attachments

Stubborn Attachments

$20.00
server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Explore a full, working code sample of an integration with Stripe Checkout. The client- and server-side code redirects to a prebuilt payment page hosted on Stripe.

Before you get started, confirm the payment methods you want to offer in your payment method settings. We enable cards and other common payment methods for you by default, and we recommend that you enable additional payment methods that are relevant for your business and customers.

Set up the server

Install the Stripe Ruby library

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Install the Stripe ruby gem and require it in your code. Alternatively, if you’re starting from scratch and need a Gemfile, download the project files using the link in the code editor.

Create a Checkout Session

Add an endpoint on your server that creates a Checkout Session. A Checkout Session controls what your customer sees on the payment page such as line items, the order amount and currency, and acceptable payment methods. We enable cards and other common payment methods for you by default, and you can enable or disable payment methods directly in the Stripe Dashboard.

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Define a product to sell

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Always keep sensitive information about your product inventory, such as price and availability, on your server to prevent customer manipulation from the client. Define product information when you create the Checkout Session using predefined price IDs or on the fly with price_data.

Choose the mode

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Checkout has three modes: payment, subscription, or setup. Use payment mode for one-time purchases. Learn more about subscription and setup modes in the docs.

Supply success and cancel URLs

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

Specify URLs for success and cancel pages—make sure they’re publicly accessible so Stripe can redirect customers to them. You can also handle both the success and canceled states with the same URL.

Redirect to Checkout

server.rb
checkout.html
success.html
cancel.html

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

After creating the session, redirect your customer to the URL for the Checkout page returned in the response.

Build your checkout

Add a success page

server.rb
checkout.html
success.html
cancel.html

_16
<!DOCTYPE html>
_16
<html>
_16
<head>
_16
<title>Thanks for your order!</title>
_16
<link rel="stylesheet" href="style.css">
_16
</head>
_16
<body>
_16
<section>
_16
<p>
_16
We appreciate your business!
_16
If you have any questions, please email
_16
<a href="mailto:orders@example.com">orders@example.com</a>.
_16
</p>
_16
</section>
_16
</body>
_16
</html>

Create a success page for the URL you provided as the Checkout Session success_url to display order confirmation messaging or order details to your customer. Stripe redirects to this page after the customer successfully completes the checkout.

Add a canceled page

server.rb
checkout.html
success.html
cancel.html

_12
<!DOCTYPE html>
_12
<html>
_12
<head>
_12
<title>Checkout canceled</title>
_12
<link rel="stylesheet" href="style.css">
_12
</head>
_12
<body>
_12
<section>
_12
<p>Forgot to add something to your cart? Shop around then come back to pay!</p>
_12
</section>
_12
</body>
_12
</html>

Add another page for cancel_url. Stripe redirects to this page when the customer clicks the back button in Checkout.

Add an order preview page

server.rb
checkout.html
success.html
cancel.html

_26
<!DOCTYPE html>
_26
<html>
_26
<head>
_26
<title>Buy cool new product</title>
_26
<link rel="stylesheet" href="style.css">
_26
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
_26
<script src="https://js.stripe.com/v3/"></script>
_26
</head>
_26
<body>
_26
<section>
_26
<div class="product">
_26
<img
_26
src="https://i.imgur.com/EHyR2nP.png"
_26
alt="The cover of Stubborn Attachments"
_26
/>
_26
<div class="description">
_26
<h3>Stubborn Attachments</h3>
_26
<h5>$20.00</h5>
_26
</div>
_26
</div>
_26
<form action="/create-checkout-session" method="POST">
_26
<button type="submit" id="checkout-button">Checkout</button>
_26
</form>
_26
</section>
_26
</body>
_26
</html>

Finally, add a page to show a preview of the customer’s order. Allow them to review or modify their order—as soon as they’re sent to the Checkout page, the order is final and they can’t modify it without creating a new Checkout Session.

Add a checkout button

server.rb
checkout.html
success.html
cancel.html

_26
<!DOCTYPE html>
_26
<html>
_26
<head>
_26
<title>Buy cool new product</title>
_26
<link rel="stylesheet" href="style.css">
_26
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
_26
<script src="https://js.stripe.com/v3/"></script>
_26
</head>
_26
<body>
_26
<section>
_26
<div class="product">
_26
<img
_26
src="https://i.imgur.com/EHyR2nP.png"
_26
alt="The cover of Stubborn Attachments"
_26
/>
_26
<div class="description">
_26
<h3>Stubborn Attachments</h3>
_26
<h5>$20.00</h5>
_26
</div>
_26
</div>
_26
<form action="/create-checkout-session" method="POST">
_26
<button type="submit" id="checkout-button">Checkout</button>
_26
</form>
_26
</section>
_26
</body>
_26
</html>

Add a button to your order preview page. When your customer clicks this button, they’re redirected to the Stripe-hosted payment page.

Congratulations!

You have a basic Checkout integration working. Learn how to customize the appearance of your checkout page.

Next steps

Fulfillment

Set up a webhook to fulfill orders after a payment succeeds. Webhooks are the most reliable way to handle business-critical events.

Payouts

Learn how to move funds out of your Stripe account into your bank account.

Refunds

Handle requests for refunds by using the Stripe API or Dashboard.

Prebuilt Checkout page

Explore a full, working code sample of an integration with Stripe Checkout. The client- and server-side code redirects to a prebuilt payment page hosted on Stripe.

Before you get started, confirm the payment methods you want to offer in your payment method settings. We enable cards and other common payment methods for you by default, and we recommend that you enable additional payment methods that are relevant for your business and customers.

Set up the server

Install the Stripe Ruby library

Install the Stripe ruby gem and require it in your code. Alternatively, if you’re starting from scratch and need a Gemfile, download the project files using the link in the code editor.

Create a Checkout Session

Add an endpoint on your server that creates a Checkout Session. A Checkout Session controls what your customer sees on the payment page such as line items, the order amount and currency, and acceptable payment methods. We enable cards and other common payment methods for you by default, and you can enable or disable payment methods directly in the Stripe Dashboard.

Define a product to sell

Always keep sensitive information about your product inventory, such as price and availability, on your server to prevent customer manipulation from the client. Define product information when you create the Checkout Session using predefined price IDs or on the fly with price_data.

Choose the mode

Checkout has three modes: payment, subscription, or setup. Use payment mode for one-time purchases. Learn more about subscription and setup modes in the docs.

Supply success and cancel URLs

Specify URLs for success and cancel pages—make sure they’re publicly accessible so Stripe can redirect customers to them. You can also handle both the success and canceled states with the same URL.

Redirect to Checkout

After creating the session, redirect your customer to the URL for the Checkout page returned in the response.

Build your checkout

Add a success page

Create a success page for the URL you provided as the Checkout Session success_url to display order confirmation messaging or order details to your customer. Stripe redirects to this page after the customer successfully completes the checkout.

Add a canceled page

Add another page for cancel_url. Stripe redirects to this page when the customer clicks the back button in Checkout.

Add an order preview page

Finally, add a page to show a preview of the customer’s order. Allow them to review or modify their order—as soon as they’re sent to the Checkout page, the order is final and they can’t modify it without creating a new Checkout Session.

Add a checkout button

Add a button to your order preview page. When your customer clicks this button, they’re redirected to the Stripe-hosted payment page.

Congratulations!

You have a basic Checkout integration working. Learn how to customize the appearance of your checkout page.

Next steps

Fulfillment

Set up a webhook to fulfill orders after a payment succeeds. Webhooks are the most reliable way to handle business-critical events.

Payouts

Learn how to move funds out of your Stripe account into your bank account.

Refunds

Handle requests for refunds by using the Stripe API or Dashboard.

server.rb
checkout.html
success.html
cancel.html
ExpandClose

_28
require 'stripe'
_28
require 'sinatra'
_28
_28
# This is a public sample test API key.
_28
# Don’t submit any personal information in requests made with this key.
_28
# Sign in to see your own test API key embedded in code samples.
_28
Stripe.api_key = 'sk_test_CGGvfNiIPwLXiDwaOfZ3oX6Y'
_28
_28
set :static, true
_28
set :port, 4242
_28
_28
YOUR_DOMAIN = 'http://localhost:4242'
_28
_28
post '/create-checkout-session' do
_28
content_type 'application/json'
_28
_28
session = Stripe::Checkout::Session.create({
_28
line_items: [{
_28
# Provide the exact Price ID (e.g. pr_1234) of the product
_28
price: '{{PRICE_ID}}',
_28
quantity: 1,
_28
}],
_28
mode: 'payment',
_28
success_url: YOUR_DOMAIN + '/success.html',
_28
cancel_url: YOUR_DOMAIN + '/cancel.html',
_28
})
_28
redirect session.url, 303
_28
end

The cover of Stubborn Attachments

Stubborn Attachments

$20.00