Instantly share code, notes, and snippets.
iloveitaly/stripe_authorized_charge_with_checkout.rb
Last active Mar 9, 2017
Example of authorizing, but not capturing, a charge using Stripe Checkout (http://stripe.com/checkout) to be later captured via NetSuite using http://SuiteSync.io/
# Michael Bianco <mike@suitesync.io> | |
# Description: Example of authorizing a charge with Stripe checkout | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test STRIPE_PUBLIC_KEY=pk_test_ | |
# gem install sinatra stripe pry | |
# ruby stripe_authorized_charge_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="Authorize, but do not capture!" | |
data-billing-address="true" | |
data-email="authorize@example.com" | |
data-label="Authorize Charge" | |
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]) | |
authorized_charge = Stripe::Charge.create( | |
currency: 'usd', | |
amount: 10_00, | |
capture: false, | |
customer: customer.id | |
) | |
# later on, you can capture with `authorized_charge.capture(amount: 9_00)` | |
"https://dashboard.stripe.com/test/charges/#{authorized_charge.id}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment