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 using the Stripe Invoice API
# Usage:
#
# export STRIPE_KEY=sk_test
# gem install stripe
# ruby stripe_invoice_line_item_metadata.rb
require 'stripe'
Stripe.api_key = ENV['STRIPE_KEY']
# 1. Create a customer and add a card
customer = Stripe::Customer.create({
:description => "Sample customer",
:email => "sample@example.com",
# this is pulled over to the NetSuite customer as a shipping address
# 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",
# this is used as the NetSuite customer billing address
# https://dashboard.suitesync.io/docs/customers#billing--shipping-address
: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)
# Create a Stripe invoice and pay it off
Stripe::InvoiceItem.create({
:customer => customer.id,
:amount => 200_00,
:currency => "usd",
:description => "Base charge",
:metadata => {
# customize NetSuite line items using the invoice item metadata
# https://dashboard.suitesync.io/docs/field-customization
netsuite_tax_code_id: 123
}
})
Stripe::InvoiceItem.create({
:customer => customer.id,
:amount => 50_00,
:currency => "usd",
# Instead of specifying an item ID, you can use the description to match to an existing NetSuite item
# https://dashboard.suitesync.io/docs/invoices#standalone-invoice-items
:description => "Bonus Pack"
})
invoice = Stripe::Invoice.create({
customer: customer.id,
metadata: {
# optionally specify additional metadata to be specified on the NetSuite invoice
netsuite_class_id: 123,
netsuite_department_id: 123
}
})
invoice.pay
# retrieve the internalId of the created CustomerPayment
# NOTE this will only work if your integration account is processing live translations
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