Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created April 17, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/683a4664708ea972ca2e081e55b7c547 to your computer and use it in GitHub Desktop.
Save iloveitaly/683a4664708ea972ca2e081e55b7c547 to your computer and use it in GitHub Desktop.
# Michael Bianco <mike@suitesync.io>
# Description: An example script demonstrating how to remove customer cards. This
# is helpful for keeping a card around temporarily for reauthorizing a
# expired card authorization. More information:
# https://dashboard.suitesync.io/docs/auth-fulfill-capture#id-like-to-create-an-authorization-for-more-than-7-
# Link: https://gist.github.com/iloveitaly/683a4664708ea972ca2e081e55b7c547
# Usage:
#
# export STRIPE_KEY=sk_test NETSUITE_EMAIL=user@company.com NETSUITE_PASSWORD=password NETSUITE_ACCOUNT=
# gem install stripe netsuite
# ruby remove_cards_on_stripe_customers.rb
require 'stripe'
require 'netsuite'
# NOTE ruby clients are used here, but this code can be easily ported to other languages/platforms
# here's Stripe's clients for different languages: https://stripe.com/docs/libraries
Stripe.api_key = ENV['STRIPE_KEY']
Stripe.max_network_retries = 10
NetSuite.configure do
reset!
# NOTE that API versions > 2015_1 require a more complicated authentication setup
api_version '2015_1'
silent ENV['NETSUITE_SILENT'].nil? || ENV['NETSUITE_SILENT'] == 'true'
email ENV['NETSUITE_EMAIL']
password ENV['NETSUITE_PASSWORD']
account ENV['NETSUITE_ACCOUNT']
role ENV['NETSUITE_ROLE']
sandbox true
soap_header({
'platformMsgs:preferences' => {
'platformMsgs:ignoreReadOnlyFields' => true,
}
})
end
# NOTE customize this logic based on your specific business requirements
def should_delete_payment_for_customer?(ns_customer)
(rand() * 2).round == 1
end
Stripe::Customer.list(limit: 100).auto_paging_each do |customer|
next if customer.sources.count.zero?
# NOTE if the customer has more than one payment source, the assumption is
# it's a type of customer that you *don't* want to remove payment sources
# from. Customize this logic to fit your specific use-case.
if customer.sources.count == 1
if customer.metadata['netsuite_customer_id']
ns_customer_id = customer.metadata['netsuite_customer_id']
ns_customer = NetSuite::Utilities.get_record(NetSuite::Records::Customer, ns_customer_id)
if should_delete_payment_for_customer?(ns_customer)
puts "deleting source for customer\t#{customer.id}"
customer.sources.first.delete
else
# NOTE a possible improvement here would be adding a flag to the customer metadata
# to indicate that the payment methods on the customer are safe to keep
puts "not deleting source for customer\t#{customer.id}"
end
else
puts "customer is not linked to NetSuite\t#{customer.id}"
end
else
puts "customer has more than one source\t#{customer.id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment