stripe sync task

This commit is contained in:
Andrew Brown 2020-01-22 13:33:45 -06:00
parent 190cdff990
commit 99b4db7f64

View File

@ -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