Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/6b533658f6d75eee8d4b7fd6c013a712 to your computer and use it in GitHub Desktop.
Save iloveitaly/6b533658f6d75eee8d4b7fd6c013a712 to your computer and use it in GitHub Desktop.
How to create a Stripe Subscription using Stripe Checkout & Sinatra
# Michael Bianco <>
# Description: Example of collecting a card and creating a subscription with Stripe
# this subscription can integrated directly into NetSuite using
# Usage:
# export STRIPE_KEY=sk_test STRIPE_PUBLIC_KEY=pk_test_
# gem install sinatra stripe pry
# ruby create_stripe_subscription_with_checkout.rb
# open http://localhost:4567
require 'sinatra'
require 'stripe'
require 'pry'
Stripe.api_key = ENV['STRIPE_KEY']
template :index do
<form action="/pay" method="POST">
src="" class="stripe-button"
data-key="<%= STRIPE_PUBLIC_KEY %>"
data-name="A Subscription"
data-label="Create Subscription"
get '/' do
erb :index
post '/pay' do
customer = Stripe::Customer.create(
description: params[:stripeBillingName],
email: params[:stripeEmail],
metadata: {
# NOTE there is where you can link this Stripe customer to a pre-existing NetSuite customer
# netsuite_customer_id: 123
# alternatively, you can let SuiteSync create the customer for you and pass over any unique data about the customer
# netsuite_web_address: ""
customer.sources.create(card: params[:stripeToken])
plan = Stripe::Plan.create(
amount: 100_00,
interval: 'month',
currency: 'usd',
name: 'A Example Plan',
id: "random_#{}",
# you can provide an ID of an existing item in Netsuite you'd like to use, or let SuiteSync create an item for you
# metadata: {
# netsuite_non_inventory_sale_item_id: 123
# }
# Subscriptions are automatically pushed to NetSuite as an invoice each billing period
# NOTE if you are using Avalara's integration with Stripe subscriptions you'll
# need to make sure this option is passed when the subscription is created
# pay_immediately: false,
# data specified on the subscription can be linked to each subsequent invoice created from this subscription
# metadata: {
# netsuite_class_id: 123,
# netsuite_department_id: 123
# }
# An invoice is automatically generated and pushed to Stripe
invoice = customer.invoices.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment