Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Connect Stripe plans to existing NetSuite items to help setup http://SuiteSync.io/ with Stripe Subscriptions
# Mike Bianco <mike@suitesync.io>
# Description: Connect Stripe plans to existing NetSuite items
# Usage: map_stripe_plans_to_netsuite.rb mapping.csv
require 'stripe'
require 'csv'
Stripe.api_key = ENV['STRIPE_KEY']
# CSV Mapping:
# - stripe_plan_id: cus_*
# - netsuite_internal_id
# - netsuite_item_key [optional]
if ARGV.empty?
puts "No CSV path specified"
exit 1
end
def wipe_netsuite_metadata_fields(stripe_resource)
stripe_resource.metadata.keys.map(&:to_s).each do |metadata_key|
if metadata_key.start_with?('netsuite_')
stripe_resource.metadata[metadata_key] = nil
end
end
end
csv_path = ARGV.first
csv_mapping = CSV.read(csv_path, headers: true)
csv_mapping.each do |csv_line|
stripe_plan_id = csv_line['stripe_plan_id']
ns_internal_id = csv_line['netsuite_internal_id']
if ns_internal_id.nil?
puts "#{stripe_plan_id}\tno netsuite ID"
next
end
stripe_plan = Stripe::Plan.retrieve(stripe_plan_id)
ns_metadata_key = csv_line['netsuite_item_key'] || 'netsuite_service_sale_item_id'
if stripe_plan.metadata[ns_metadata_key]
puts "#{stripe_plan_id}\tplan already linked"
next
end
puts "#{stripe_plan_id}\tmapping plan"
wipe_netsuite_metadata_fields(stripe_plan)
stripe_plan.metadata[ns_metadata_key] = ns_internal_id
stripe_plan.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment