Skip to content

Instantly share code, notes, and snippets.

@kshaw
Last active August 29, 2015 13:58
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 kshaw/10281738 to your computer and use it in GitHub Desktop.
Save kshaw/10281738 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'stripe'
require 'pry'
Stripe.api_key = "sk_test_N1JXbZ1dYeBIZAhjqHfMA1qy"
customers = Stripe::Customer.all(:limit => 100)
#loop through invoices
last = nil
while customers
customers.each do |c|
#check if the customer account is delinquent
if c["delinquent"] == true
#Find the first invoice and close it
i = Stripe::Invoice.all(:customer => c["id"]).first
if i["paid"] == false
subs = i["lines"]["subscriptions"]
subs.each do |s|
begin
subscription = c.subscriptions.retrieve(s["id"])
#check if sub is past_due
if subscription["status"] == "past_due"
#close the invoice
#i.save(:closed => true)
puts "Closed invoice #{i['id']}"
#s.delete
puts "Closed Subscription #{c['subscriptions'].first['id']}"
end
rescue
#maybe we should still close the invoice ???
#i.save(:closed => true)
#puts "Closed invoice #{i['id']}"
puts $!
end
end
end
end
last = c
end
if customers["has_more"] == true
customers = Stripe::Customer.all(:limit => 100, :starting_after => last)
else
customers = false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment