Skip to content

Commit

Permalink
Unsubscribe customers without an active stripe subscription.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 15, 2023
1 parent 2191ddc commit 608ad82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-06-13 04:06:48 UTC using RuboCop version 1.30.1.
# on 2023-11-15 14:19:32 UTC using RuboCop version 1.30.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -55,9 +55,9 @@ Naming/MethodParameterName:
- 'slack-market/app.rb'
- 'slack-market/models/team.rb'

# Offense count: 30
# Offense count: 32
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 244
Max: 246
28 changes: 17 additions & 11 deletions slack-market/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ def check_subscribed_teams!
Team.where(subscribed: true, :stripe_customer_id.ne => nil).each do |team|
begin
customer = Stripe::Customer.retrieve(team.stripe_customer_id)
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! "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! "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 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
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! "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! "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-market/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 608ad82

Please sign in to comment.