From 99b4db7f64d95a428707d68bb66522dc5ee668df Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Wed, 22 Jan 2020 13:33:45 -0600 Subject: [PATCH] stripe sync task --- lib/tasks/data_integrity.rake | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/tasks/data_integrity.rake b/lib/tasks/data_integrity.rake index 757e72e8..0890031a 100644 --- a/lib/tasks/data_integrity.rake +++ b/lib/tasks/data_integrity.rake @@ -4,6 +4,31 @@ namespace :data_integrity do PaypalInvoice.where(status: "COMPLETED", page_unlock_promo_code_id: nil).find_each(&:generate_promo_code!) end - + desc "Ensure that all Premium subscribers are still Premium in Stripe" + task subscription_synced_with_stripe: :environment do + BillingPlan::PREMIUM_IDS.each do |billing_plan_id| + active_billing_plan = BillingPlan.find(billing_plan_id) + + User.where(selected_billing_plan_id: billing_plan_id).find_each do |user| + stripe_customer = Stripe::Customer.retrieve(user.stripe_customer_id) + stripe_subscription = stripe_customer.subscriptions.data[0] + + # Go through each of the customer's subscription items and make sure their + # current billing plan is included as one. + should_downgrade_user = stripe_subscription.items.data.any? do |subscription_item| + subscription_item.plan.id == active_billing_plan.stripe_plan_id + end + + if should_downgrade_user + SlackService.post('#subscription', "Automatically downgrading #{user} from #{active_billing_plan.stripe_plan_id}") + # SubscriptionService.cancel_all_existing_subscriptions(user) + end + + # Aggressively throttle (way too much) just to keep Stripe happy if we plan on doing + # this for every user, every day. + sleep 1 + end + end + end end