Had a client who had a bad subscription plan in their stripe account that was applied over a year ago. They have enough volume that it didn’t make sense to go through the web admin to clean up the data so we cleaned it up using the Stripe ruby gem. Here is the process that I went through to discover the correct commands to issue.
To start, this was a promotional amount that was only applied for a few days in january for the start of the year. So off the bat I wanted to narrow it down to the related customers.
[ruby]
users = User.where("created_at >= ?", 1.year.ago).where("created_at <?", 330.days.ago)
paying_users = users.where.not(stripe_customer_id: nil)
customer_ids = users.map(&:stripe_payment_id)
[/ruby]
Now that we had the customer ids from tripe i wanted to see what the data looked like
[ruby]
Stripe::Customer.retrieve(customer_ids.first)
[/ruby]
the Stripe::Customer that is returned looks and acts like an open struct or hashie mash. So you can call methods on it similar to javascript objects in order to get values of a key that is in the object.
I didn’t know the specific name of the subscription (didn’t have web access). So needed to discover what it was called. I remembered it had the word special in it.
Next is to find the specific customers that we need to look at
[ruby]
stripe_customers = customer_ids.map{|c| Stripe::Customer.retrieve(c)}
stripe_customers.select(&:subscription).map{|c| c.subscription.plan.id}.uniq
[/ruby]
This returns an array of [‘premium’, ‘othersubscriptions’,….,’newyearsspecial’]. So i know that it is the newyearsspecial that i need to update.
[ruby]
special_customers = stripe_customers.select(&:subscription).select{|c| c.subscription.plan.id == ‘newyearsspecial’}
special_subscriptions = special_customers.map{|c| c.subscription.id}
special_subscriptions = special_customers.map(&:subscription)
[/ruby]
The next part is a little vague, in the documentation it says to set the ‘at_period_end’ parameter to true in order to delete it and keep it active until the end of the subscription, so i just did both.
[ruby]
special_subscriptions.each do |subscription|
subscription.at_period_end = true
subscription.delete(at_period_end: true)
end
[/ruby]Now all of the subscriptions will expire at the end of the year, Yeah!
Leave a Reply