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 <mike@suitesync.io>
# Description: Example of collecting a card and creating a subscription with Stripe
# this subscription can integrated directly into NetSuite using http://suitesync.io/
# 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']
STRIPE_PUBLIC_KEY = ENV['STRIPE_PUBLIC_KEY']
template :index do
<<EOL
<form action="/pay" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= STRIPE_PUBLIC_KEY %>"
data-amount="100"
data-name="A Subscription"
data-billing-address="true"
data-email="subscription@example.com"
data-label="Create Subscription"
data-locale="auto">
</script>
</form>
EOL
end
get '/' do
erb :index
end
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
# https://dashboard.suitesync.io/docs/field-customization#using-existing-records-or-existing-integrations
# 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: "http://stripe.com"
}
)
customer.sources.create(card: params[:stripeToken])
plan = Stripe::Plan.create(
amount: 100_00,
interval: 'month',
currency: 'usd',
name: 'A Example Plan',
id: "random_#{Time.now.to_i}",
# you can provide an ID of an existing item in Netsuite you'd like to use, or let SuiteSync create an item for you
# https://dashboard.suitesync.io/docs/field-customization
# metadata: {
# netsuite_non_inventory_sale_item_id: 123
# }
)
# Subscriptions are automatically pushed to NetSuite as an invoice each billing period
# https://dashboard.suitesync.io/docs/subscriptions
customer.subscriptions.create({
plan: plan.id,
# 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
# https://dashboard.suitesync.io/docs/field-customization
# metadata: {
# netsuite_class_id: 123,
# netsuite_department_id: 123
# }
})
# An invoice is automatically generated and pushed to Stripe
invoice = customer.invoices.first
"https://dashboard.stripe.com/test/invoices/#{invoice.id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment