diff --git a/slack-gamebot/app.rb b/slack-gamebot/app.rb index 022559f..6e88c2b 100644 --- a/slack-gamebot/app.rb +++ b/slack-gamebot/app.rb @@ -64,17 +64,23 @@ def check_trials! def check_subscribed_teams! Team.where(subscribed: true, :stripe_customer_id.ne => nil).each do |team| - team.stripe_customer.subscriptions.each do |subscription| - subscription_name = "#{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)})" - logger.info "Checking #{team} subscription to #{subscription_name}, #{subscription.status}." - case subscription.status - when 'past_due' - logger.warn "Subscription for #{team} is #{subscription.status}, notifying." - team.inform_admin! "Your subscription to #{subscription_name} is past due. #{team.update_cc_text}" - when 'canceled', 'unpaid' - logger.warn "Subscription for #{team} is #{subscription.status}, downgrading." - team.inform_admin! "Your subscription to #{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)}) was canceled and your team has been downgraded. Thank you for being a customer!" - team.update_attributes!(subscribed: false) + if team.stripe_customer.subscriptions.none? + logger.info "No active subscriptions for #{team} (#{team.stripe_customer_id}), downgrading." + team.inform! 'Your subscription was canceled and your team has been downgraded. Thank you for being a customer!' + team.update_attributes!(subscribed: false) + else + team.stripe_customer.subscriptions.each do |subscription| + subscription_name = "#{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)})" + logger.info "Checking #{team} subscription to #{subscription_name}, #{subscription.status}." + case subscription.status + when 'past_due' + logger.warn "Subscription for #{team} is #{subscription.status}, notifying." + team.inform_admin! "Your subscription to #{subscription_name} is past due. #{team.update_cc_text}" + when 'canceled', 'unpaid' + logger.warn "Subscription for #{team} is #{subscription.status}, downgrading." + team.inform_admin! "Your subscription to #{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)}) was canceled and your team has been downgraded. Thank you for being a customer!" + team.update_attributes!(subscribed: false) + end end end rescue StandardError => e diff --git a/spec/slack-gamebot/app_spec.rb b/spec/slack-gamebot/app_spec.rb index df386ec..2bbb44e 100644 --- a/spec/slack-gamebot/app_spec.rb +++ b/spec/slack-gamebot/app_spec.rb @@ -63,6 +63,13 @@ subject.send(:check_subscribed_teams!) expect(team.reload.subscribed?).to be false end + it 'notifies no active subscriptions' do + customer.subscriptions.data = [] + expect(Stripe::Customer).to receive(:retrieve).and_return(customer) + expect_any_instance_of(Team).to receive(:inform!).with('Your subscription was canceled and your team has been downgraded. Thank you for being a customer!') + subject.send(:check_subscribed_teams!) + expect(team.reload.subscribed?).to be false + end end end end