Skip to content

Instantly share code, notes, and snippets.

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/0111973978b8146a07b88374fb03ffda to your computer and use it in GitHub Desktop.
Save iloveitaly/0111973978b8146a07b88374fb03ffda to your computer and use it in GitHub Desktop.
# Description: Identify all unpaid & open invoices that won't be attempted again and close them
require 'stripe'
Stripe.api_key = ENV['STRIPE_KEY']
# NOTE adjust the start & end dates to avoid processing *all* invoices in Stripe
start_timestamp = Time.now.to_i
end_timestamp = Time.now.to_i - (60 * 60 * 24 * 30)
# NOTE the API does not support filtering invoices by status. In order to find
Stripe::Invoice.list(date: { gte: start_timestamp, lte: start_timestamp }, limit: 100).auto_paging_each do |invoice|
if !invoice.closed && !invoice.paid && invoice.next_payment_attempt.nil?
puts "closing\tinvoice.id"
invoice.closed = true
invoice.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment