Skip to content

Commit

Permalink
Check Stripe for subscription status, closes #150.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 6, 2017
1 parent 5791ebf commit 7c2fa2b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
12 changes: 6 additions & 6 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 2017-08-04 20:36:10 -0400 using RuboCop version 0.34.2.
# on 2017-08-06 15:39:01 -0400 using RuboCop version 0.34.2.
# 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 @@ -28,15 +28,15 @@ Lint/HandleExceptions:
Exclude:
- 'slack-gamebot/commands/matches.rb'

# Offense count: 51
# Offense count: 52
Metrics/AbcSize:
Max: 69

# Offense count: 5
Metrics/BlockNesting:
Max: 4

# Offense count: 4
# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 203
Expand All @@ -45,12 +45,12 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 19

# Offense count: 746
# Offense count: 763
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 213
Max: 250

# Offense count: 36
# Offense count: 37
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 48
Expand Down
21 changes: 18 additions & 3 deletions slack-gamebot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class App < SlackRubyBotServer::App

def prepare!
super
update_unbalanced_teams!
deactivate_dead_teams!
end

def after_start!
check_premium_teams!
nudge_sleeping_teams!
bother_free_teams!
end
Expand Down Expand Up @@ -54,8 +54,23 @@ def bother_free_teams!
end
end

def update_unbalanced_teams!
Team.where(unbalanced: nil).update_all(unbalanced: false)
def check_premium_teams!
Team.where(premium: true, :stripe_customer_id.ne => nil).each do |team|
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 premium 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 premium 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!(premium: false)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions slack-gamebot/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Team
field :premium, type: Boolean, default: false

scope :api, -> { where(api: true) }
scope :premium, -> { where(premium: true) }

validates_presence_of :game_id

Expand Down
25 changes: 25 additions & 0 deletions spec/slack-gamebot/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,29 @@
end
end
end
context 'subscribed' do
include_context :stripe_mock
let(:plan) { stripe_helper.create_plan(id: 'slack-playplay-yearly', amount: 2999) }
let(:customer) { Stripe::Customer.create(source: stripe_helper.generate_card_token, plan: plan.id, email: 'foo@bar.com') }
let!(:team) { Fabricate(:team, premium: true, stripe_customer_id: customer.id) }
context '#check_premium_teams!' do
it 'ignores active subscriptions' do
expect_any_instance_of(Team).to_not receive(:inform!)
subject.send(:check_premium_teams!)
end
it 'notifies past due subscription' do
customer.subscriptions.data.first['status'] = 'past_due'
expect(Stripe::Customer).to receive(:retrieve).and_return(customer)
expect_any_instance_of(Team).to receive(:inform!).with("Your premium subscription to StripeMock Default Plan ID ($29.99) is past due. #{team.update_cc_text}")
subject.send(:check_premium_teams!)
end
it 'notifies past due subscription' do
customer.subscriptions.data.first['status'] = 'canceled'
expect(Stripe::Customer).to receive(:retrieve).and_return(customer)
expect_any_instance_of(Team).to receive(:inform!).with('Your premium subscription to StripeMock Default Plan ID ($29.99) was canceled and your team has been downgraded. Thank you for being a customer!')
subject.send(:check_premium_teams!)
expect(team.reload.premium?).to be false
end
end
end
end

0 comments on commit 7c2fa2b

Please sign in to comment.