Last active
June 26, 2017 20:59
-
-
Save iloveitaly/fe9d4dfa74c8eeee37b4909dc7dda68e to your computer and use it in GitHub Desktop.
Customize NetSuite Invoices created from Stripe Subscriptions using http://SuiteSync.io/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael Bianco <mike@suitesync.io> | |
# Description: Customize NetSuite invoice data from Stripe Subscription | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test STRIPE_PUBLIC_KEY=sk_test_ | |
# gem install stripe | |
require 'stripe' | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
customer = Stripe::Customer.create({ | |
description: "First Last", | |
email: 'invoice-test@example.com', | |
# comes over as the shipping address in NetSuite | |
# https://dashboard.suitesync.io/docs/customers#billing--shipping-address | |
shipping: { | |
name: 'First Last', | |
phone: '123 123 1234', | |
address: { | |
line1: '3180 18th St', | |
city: 'San Francisco', | |
postal_code: '94110', | |
state: 'CA', | |
country: 'US', | |
} | |
} | |
}) | |
customer.sources.create(card: 'tok_visa') | |
# add two one-time costs to be included on the first invoice | |
Stripe::InvoiceItem.create({ | |
customer: customer.id, | |
amount: 10_00, | |
currency: "usd", | |
description: "One-time cost", | |
metadata: { | |
# you can link this line item to a specific item | |
# alternatively, you can rely on SuiteSync's matching to pick an existing NetSuite item | |
# https://dashboard.suitesync.io/docs/invoices#standalone-invoice-items | |
# netsuite_service_sale_item_id: 123 | |
} | |
}) | |
Stripe::InvoiceItem.create({ | |
customer: customer.id, | |
amount: 10_00, | |
currency: "usd", | |
description: "Bonus Pack", | |
}) | |
plan = Stripe::Plan.create( | |
amount: 60_00, | |
interval: 'month', | |
currency: 'usd', | |
name: 'Random Plan', | |
id: "random_#{Time.now.to_i}", | |
) | |
customer.subscriptions.create({ | |
plan: plan.id, | |
metadata: { | |
# data specified on the subscription can be linked to each subsequent invoice created from this subscription | |
# https://dashboard.suitesync.io/docs/field-customization | |
netsuite_class_id: 123, | |
netsuite_department_id: 123 | |
} | |
}) | |
invoice = customer.invoices.first | |
puts invoice.id | |
loop do | |
invoice.refresh | |
if ns_invoice_id = invoice.metadata['netsuite_invoice_id'] | |
puts "Found Invoice: #{ns_invoice_id}" | |
break | |
end | |
puts "Waiting for invoice to be translated to Netsuite..." | |
sleep(2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment