Create & pay a NetSuite invoice using Stripe Checkout and http://SuiteSync.io/
# Michael Bianco <mike@suitesync.io> | |
# Description: Example of creating and paying a NetSuite invoice using Stripe | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test STRIPE_PUBLIC_KEY=pk_test_ | |
# gem install sinatra stripe pry | |
# ruby create_paid_netsuite_invoice_with_stripe.rb | |
# open http://localhost:4567 | |
require 'sinatra' | |
require 'stripe' | |
require 'pry' | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
STRIPE_PUBLIC_KEY = ENV['STRIPE_PUBLIC_KEY'] | |
NETSUITE_SERVICE_SALE_ITEM_ID = 384208 | |
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="Service Purchase" | |
<%# data-billing-address="true" %> | |
data-email="purchase@example.com" | |
data-label="Pay Invoice" | |
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]) | |
invoice_item = Stripe::InvoiceItem.create({ | |
customer: customer.id, | |
amount: 200_00, | |
currency: "usd", | |
description: "A great service", | |
metadata: { | |
# NOTE to avoid creating new items in NetSuite, link the line item to an existing NetSuite item | |
# https://dashboard.suitesync.io/docs/field-customization#using-existing-records-or-existing-integrations | |
netsuite_service_sale_item_id: NETSUITE_SERVICE_SALE_ITEM_ID | |
} | |
}) | |
invoice = Stripe::Invoice.create(customer: customer.id) | |
invoice.pay | |
redirect "https://dashboard.stripe.com/test/charges/#{invoice.charge}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment