Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/a4f25ed84fe86c26f6cb0463c838df8d to your computer and use it in GitHub Desktop.
Save iloveitaly/a4f25ed84fe86c26f6cb0463c838df8d to your computer and use it in GitHub Desktop.
Example of creating a Stripe subscription-created invoice with line items linked to existing NetSuite Service Sale Items using http://SuiteSync.io
# Michael Bianco <mike@suitesync.io>
require 'stripe'
Stripe.api_key = 'sk_test'
customer = Stripe::Customer.create({
:description => "Sample Customer"
})
card_token = Stripe::Token.create(
{
:card => {
:number => 4242424242424242,
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314"
}
}
)
customer.sources.create(card: card_token.id)
invoice_item = Stripe::InvoiceItem.create({
:customer => customer.id,
:amount => 11_00,
:currency => "usd",
:description => "Custom line item",
:metadata => {
netsuite_service_sale_item_id: 123
}
})
if !(plan = Stripe::Plan.retrieve('existing-plan') rescue nil)
plan = Stripe::Plan.create({
:amount => 10_00,
:interval => 'month',
:name => 'Existing Plan',
:currency => 'usd',
:id => "existing-plan",
:metadata => {
netsuite_service_sale_item_id: 234
}
})
end
subscription = customer.subscriptions.create(
:plan => plan.id,
)
invoice = customer.invoices.first
# `invoice.lines` will contain two items:
#
# - The $11 line item, linked to NS ID 123
# - A $10 plan-created line item, linked to 234
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