Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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 customers using a CSV import
# Usage: import_netsuite_customer_to_stripe_from_csv.rb mapping.csv
require 'stripe'
require 'csv'
Stripe.api_key = 'sk_test_'
if ARGV.empty?
puts "No CSV path specified"
exit 1
end
csv_path = ARGV.first
csv_mapping = CSV.read(csv_path, headers: true)
# CSV Mapping:
# - stripe_customer_id: cus_*
# - netsuite_internal_id
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
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
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