Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of creating a NetSuite Invoice using the Stripe Invoice API using http://SuiteSync.io/
# Michael Bianco <mike@suitesync.io>
# Description: Create a NetSuite Invoice through the Stripe Invoice API using http://SuiteSync.io
# Usage:
#
# export STRIPE_KEY=sk_test_123
# gem install stripe
# ruby stripe_invoice_line_item_metadata.rb
require 'stripe'
Stripe.api_key = ENV['STRIPE_KEY']
customer = Stripe::Customer.create({
:description => "Sample customer",
:email => "sample@example.com",
# optional: if you specify a shipping address here, it is added to the NetSuite customer
# https://dashboard.suitesync.io/docs/customers#billing--shipping-address
:shipping => {
:name => 'Sample Customer',
:phone => '123 123 1234',
:address => {
:line1 => '3180 18th St',
:city => 'San Francisco',
:postal_code => '94110',
:state => 'CA',
:country => 'US',
}
},
:metadata => {
# add additional business-specific data to the NetSuite customer using metadata
# https://dashboard.suitesync.io/docs/field-customization
netsuite_custom_id: 123
}
})
card_token = Stripe::Token.create({
:card => {
:number => 4242424242424242,
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314",
# optional: if a billing address is specified here, it is added to the NetSuite customer
:address_city => 'San Francisco',
:address_country => 'United States',
:address_line1 => '3180 18th St',
:address_line2 => '',
:address_state => 'CA',
:address_zip => '94110',
}
})
customer.sources.create(card: card_token.id)
Stripe::InvoiceItem.create({
:customer => customer.id,
:amount => 200_00,
:currency => "usd",
:description => "Base charge",
:metadata => {
# customize NetSuite line items using the invoice item metadata
netsuite_tax_code_id: 123
}
})
Stripe::InvoiceItem.create({
:customer => customer.id,
:amount => 50_00,
:currency => "usd",
:description => "Bonus Pack"
})
invoice = Stripe::Invoice.create({
:customer => customer.id,
# specify additional information to be passed to the NetSuite invoice
# https://dashboard.suitesync.io/docs/field-customization
:metadata => {
netsuite_class_id: 123,
netsuite_department_id: 123
}
})
# `pay` creates a Stripe payment on the customer's saved card
invoice.pay
# retrieve the internalId of the created CustomerPayment
# NOTE this only works if you have a SuiteSync account
loop do
invoice.refresh
if internal_id = invoice.metadata['netsuite_invoice_id']
puts "Invoice Internal ID: #{internal_id}"
exit(0)
end
puts "Waiting for Invoice to translate to NetSuite..."
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment