Skip to content

Commit

Permalink
Unsubscribe canceled subscriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 15, 2023
1 parent 1bc5245 commit a037bd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
28 changes: 17 additions & 11 deletions slack-gamebot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/slack-gamebot/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a037bd3

Please sign in to comment.