Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Point http://SuiteSync.io/ to use existing NetSuite customers when migrating to Stripe
# Mike Bianco <mike@suitesync.io>
# Description: Script to link Stripe customers to NetSuite customers using email as the primary key
# requires the following environment variables:
#
# STRIPE_KEY
# NETSUITE_EMAIL
# NETSUITE_PASSWORD
# NETSUITE_ACCOUNT
require 'stripe'
require 'netsuite'
STDOUT.sync = true
Stripe.api_key = ENV['STRIPE_KEY']
NetSuite.configure do
reset!
# NOTE that API versions > 2015_1 require a more complicated authentication setup
api_version '2015_1'
read_timeout 60 * 3
silent ENV['NETSUITE_SILENT'].nil? || ENV['NETSUITE_SILENT'] == 'true'
email ENV['NETSUITE_EMAIL']
password ENV['NETSUITE_PASSWORD']
account ENV['NETSUITE_ACCOUNT']
soap_header({
'platformMsgs:preferences' => {
'platformMsgs:ignoreReadOnlyFields' => true,
}
})
end
# output is tab-deliminated for easy copy/paste to google sheets
def update_netsuite_customer_external_id(ns_customer, stripe_customer)
if ns_customer.external_id.nil? || ns_customer.external_id.empty?
ns_customer.external_id = stripe_customer.id
if ns_customer.comments.nil? || ns_customer.comments.empty?
ns_customer.comments = "Stripe: #{stripe_customer.id}"
else
puts "#{stripe_customer.id}\tskipping memo update"
end
NetSuite::Utilities.backoff { ns_customer.update(comments: ns_customer.comments) }
end
end
Stripe::Customer.list(limit: 100).auto_paging_each do |stripe_customer|
if !stripe_customer.metadata['netsuite_customer_id'].nil?
puts "#{stripe_customer.id}\talready linked"
next
end
stripe_customer_email = stripe_customer.email
stripe_customer_name = stripe_customer.description
# NOTE an alternative approach here is to match by companyName
# stripe_match_data = stripe_customer_name
# stripe_match_field = 'companyName'
stripe_match_data = stripe_customer_email
stripe_match_field = 'email'
if stripe_customer.description.nil?
puts "#{stripe_customer.id}\tmatch field blank"
next
end
# the NetSuite `contains` operator is case insensative, but it is sensative to whitespace
stripe_match_data = stripe_match_data.strip
# find NetSuite customer by a match field
search = NetSuite::Utilities.backoff { NetSuite::Records::Customer.search(
basic: [
{
field: 'isInactive',
value: false,
},
{
field: stripe_match_field,
operator: 'contains',
value: stripe_match_data
}
],
preferences: {
page_size: 10,
body_fields_only: true
}
) }
if search && !search.results.empty?
if search.results.size > 1
puts "#{stripe_customer.id}\tmultiple matches\t#{stripe_customer_email}\t#{stripe_customer_name}"
else
ns_customer = search.results.first
# if you are translating data from Stripe livemode to NS sandbox, you'll need to use the `netsuite_sandbox_customer_id`
stripe_customer.metadata['netsuite_customer_id'] = ns_customer.internal_id
stripe_customer.save
# NOTE optionally link customer on the netsuite side
# this is not required by SuiteSync and is available as an optional feature
# update_netsuite_customer_external_id(ns_customer, stripe_customer)
puts "#{stripe_customer.id}\tcustomer linked\t#{stripe_customer_email}\t#{stripe_customer_name}"
end
else
puts "#{stripe_customer.id}\tno match\t#{stripe_customer_email}\t#{stripe_customer_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment