Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Wipe all metadata fields from Stripe used by http://SuiteSync.io/ when a sandbox refresh is performed
# Mike Bianco <mike@suitesync.io>
# Description: Wipe all metadata fields from Stripe used by SuiteSync.
# Helpful after a sandbox refresh.
#
# Usage:
# export STRIPE_KEY=sk_test_
# ruby suitesync_wipe_metadata_fields.rb
require 'stripe'
Stripe.api_key = ENV['STRIPE_KEY']
def date_from_stripe_resource(stripe_resource)
if [ Stripe::Invoice, Stripe::Transfer ].include?(stripe_resource.class)
stripe_resource.date
elsif [ Stripe::Charge, Stripe::Refund, Stripe::Dispute ].include?(stripe_resource.class)
stripe_resource.created
else
fail "uncaught stripe resource class #{stripe_resource}"
end
end
# TODO should optionally remove all keys starting with the prefix
# NOTE this prefix should be customized depending on your livemode & sandbox setup
prefix = 'netsuite_'
# NOTE this backfill date should be adjusted to match the data you'd like to scrub
greater_than = (DateTime.now - 3.months).to_i
Stripe::Customer.list(limit: 100).auto_paging_each do |customer|
next if customer.metadata.to_h.empty?
customer.metadata["#{prefix}customer_id"] = nil
customer.metadata["#{prefix}allow_integration"] = nil
customer.save
end
Stripe::Refund.list(limit: 100).auto_paging_each do |refund|
if date_from_stripe_resource(refund) < greater_than
break
end
next if refund.metadata.to_h.empty?
refund.metadata["#{prefix}allow_integration"] = nil
refund.metadata["#{prefix}credit_memo_id"] = nil
refund.metadata["#{prefix}customer_refund_id"] = nil
refund.save
end
Stripe::Charge.list(
limit: 100,
created: { gt: greater_than }
).auto_paging_each do |charge|
next if charge.metadata.to_h.empty?
charge.metadata["#{prefix}allow_integration"] = nil
charge.metadata["#{prefix}customer_payment_id"] = nil
charge.metadata["#{prefix}invoice_id"] = nil
charge.save
end
Stripe::Dispute.list(
limit: 100,
created: { gt: greater_than }
).auto_paging_each do |dispute|
next if dispute.metadata.to_h.empty?
dispute.metadata["#{prefix}allow_integration"] = nil
dispute.metadata["#{prefix}credit_memo_id"] = nil
dispute.metadata["#{prefix}customer_refund_id"] = nil
dispute.save
end
Stripe::Transfer.list(limit: 100, created: { gt: greater_than }).auto_paging_each do |transfer|
next if transfer.metadata.to_h.empty?
transfer.metadata["#{prefix}allow_integration"] = nil
transfer.metadata["#{prefix}deposit_id"] = nil
transfer.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment