Last active
August 29, 2015 13:58
-
-
Save kshaw/10281738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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