Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created January 26, 2017 18:37
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/84bdf1283fd484a73bd2fca8d6c4e805 to your computer and use it in GitHub Desktop.
Save iloveitaly/84bdf1283fd484a73bd2fca8d6c4e805 to your computer and use it in GitHub Desktop.
# Michael Bianco <mike@suitesync.io>
# Description: Get a report of all Stripe transactions associated with other Stripe records
# that were created before a specific date
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
refunds = []
charges = []
disputes = []
# NOTE: adjust this date to the "cut off" date that you are inspecting
greater_than = DateTime.now
# refund list API guarantees refunds are returned in order
# NOTE right now, there are no date filters on the refund API
Stripe::Refund.list(limit: 100, expand: ['data.charge']).auto_paging_each do |refund|
if date_from_stripe_resource(refund) < greater_than
break
end
if date_from_stripe_resource(refund.charge) < greater_than
refunds << refund
elsif refund.charge.invoice
invoice = Stripe::Invoice.retrieve(refund.charge.invoice)
if date_from_stripe_resource(invoice) < greater_than
refunds << refund
end
end
end
Stripe::Charge.list(
limit: 100,
expand: ['data.invoice'],
created: { gt: greater_than }
).auto_paging_each do |charge|
if charge.invoice && date_from_stripe_resource(charge.invoice) < greater_than
charges << charge
end
end
Stripe::Dispute.list(
limit: 100,
expand: ['data.charge'],
created: { gt: greater_than }
).auto_paging_each do |dispute|
if date_from_stripe_resource(dispute.charge) < greater_than
disputes << dispute
elsif dispute.charge.invoice
invoice = Stripe::Invoice.retrieve(dispute.charge.invoice)
if date_from_stripe_resource(invoice) < greater_than
disputes << disputes
end
end
end
puts "Transaction ID\tRelated Transaction ID\tAmount\tDate"
(charges + refunds + disputes).sort_by { |t| date_from_stripe_resource(t) }.map do |transaction|
related_transaction_id = transaction.try(:charge).try(:id) || transaction.try(:invoice).try(:id)
puts "#{transaction.id}\t#{related_transaction_id}\t%.2f\t#{Time.at(date_from_stripe_resource(transaction))}" % (transaction.amount/100.0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment