Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active February 21, 2019 19:24
Show Gist options
  • Save iloveitaly/1bca5e56c7aaef52affa85630c335d26 to your computer and use it in GitHub Desktop.
Save iloveitaly/1bca5e56c7aaef52affa85630c335d26 to your computer and use it in GitHub Desktop.
Connect existing NetSuite customers to Stripe customers using a CSV. Useful when migrating to Stripe from another card processor. http://SuiteSync.io/
# Mike Bianco <mike@suitesync.io>
# Description: Link Stripe customers to NetSuite using a CSV containing the Stripe
# customer ID and the corresponding NetSuite internal ID
# More info: https://dashboard.suitesync.io/docs/customers#using-existing-customers
#
# Link: https://gist.github.com/iloveitaly/1bca5e56c7aaef52affa85630c335d26
#
# Usage:
#
# gem install stripe
# export STRIPE_KEY=sk_test_
# ruby import_netsuite_customer_to_stripe_from_csv.rb mapping.csv
#
# mapping.csv should have two columns:
# - stripe_customer_id: cus_*
# - netsuite_internal_id
#
# Note that in order for the mapping to "stick" the customer needs to be "processed"
# by SuiteSync. In most cases, this happens automatically, but depending on your setup
# you may need to contact support and have then ensure customers are linked.
require 'stripe'
require 'csv'
Stripe.api_key = ENV['STRIPE_KEY']
Stripe.max_network_retries = 10
if ARGV.empty?
puts "No CSV path specified"
exit 1
end
csv_path = ARGV.first
csv_mapping = CSV.read(csv_path, headers: true)
netsuite_allow_integration = true
csv_mapping.each do |csv_line|
stripe_customer_id = csv_line['stripe_customer_id']
netsuite_customer_internal_id = csv_line['netsuite_internal_id']
if netsuite_customer_internal_id.nil?
puts "#{stripe_customer_id}\tno netsuite customer ID"
next
end
stripe_customer = Stripe::Customer.retrieve(stripe_customer_id)
# if stripe_customer.metadata['netsuite_customer_id']
# puts "#{stripe_customer.id}\tcustomer already mapped"
# next
# end
if netsuite_customer_internal_id =~ /[^0-9]+/
puts "netsuite internal is not a number\t#{netsuite_customer_internal_id}"
next
end
puts "#{stripe_customer.id}\tmapping customer"
# if you are translating data from Stripe livemode to NS sandbox, you'll need to use the `netsuite_sandbox_customer_id`
# more information: https://dashboard.suitesync.io/docs/field-customization
stripe_customer.metadata['netsuite_customer_id'] = netsuite_customer_internal_id
if netsuite_allow_integration
stripe_customer.metadata['netsuite_allow_integration'] = true
end
stripe_customer.save
end
puts "\n\nCustomers mapped!"
puts "Remaining unmapped customers\n\n"
mapped_customers = []
Stripe::Customer.list({ limit: 100 }).auto_paging_each do |customer|
if customer.metadata['netsuite_customer_id']
mapped_customers << customer
next
end
puts [
customer.id,
customer.description,
customer.email
].join("\t")
end
puts "\n\nMapped customers:\n\n"
mapped_customers.map do |customer|
puts [
customer.id,
customer.metadata['netsuite_customer_id'],
customer.description,
customer.email
].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment