Last active
January 29, 2020 01:07
-
-
Save iloveitaly/f866460b4ad2f92f5d2a1286534518cc to your computer and use it in GitHub Desktop.
Connect Stripe plans to existing NetSuite items to help setup http://SuiteSync.io/ with Stripe Subscriptions
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
# Mike Bianco <mike@suitesync.io> | |
# Description: Connect Stripe plans to existing NetSuite items | |
# Usage: map_stripe_plans_to_netsuite.rb mapping.csv | |
# Link: https://gist.github.com/iloveitaly/f866460b4ad2f92f5d2a1286534518cc | |
require 'stripe' | |
require 'csv' | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
Stripe.api_version = '2018-08-23' | |
Stripe.max_network_retries = 10 | |
# CSV Mapping: | |
# - stripe_plan_id: Stripe pricing plan, or SKU, ID | |
# - netsuite_item_key [optional, defaults to netsuite_service_sale_item_id] | |
# - netsuite_internal_id | |
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) rescue nil) | |
stripe_plan ||= (Stripe::SKU.retrieve(stripe_plan_id) rescue nil) | |
if stripe_plan.nil? | |
puts "#{stripe_plan_id}\tinvalid plan or SKU ID" | |
next | |
end | |
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 or sku" | |
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