Skip to content

Commit

Permalink
Minor cleanup of controller
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Nov 11, 2024
1 parent c4855a9 commit c61fd03
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
24 changes: 8 additions & 16 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
class AlaveteliPro::SubscriptionsController < AlaveteliPro::BaseController
before_action :check_has_current_subscription, only: [:index]

skip_before_action :html_response, only: [:create, :authorise]
skip_before_action :pro_user_authenticated?, only: [:create, :authorise]

before_action :authenticate, only: [:create, :authorise]

before_action :check_allowed_to_subscribe_to_pro, only: [:create]
before_action :prevent_duplicate_submission, only: [:create]
before_action :load_plan, :load_coupon, only: [:create]
before_action :check_has_current_subscription, only: [:index]

def index
@customer = current_user.pro_account.try(:stripe_customer)
@subscriptions = current_user.pro_account.subscriptions
end

# TODO: remove reminder of Stripe params once shipped
#
# params =>
# {"utf8"=>"✓",
# "authenticity_token"=>"Ono2YgLcl1eC1gGzyd7Vf5HJJhOek31yFpT+8z+tKoo=",
# "stripe_token"=>"tok_s3kr3t…",
# "controller"=>"alaveteli_pro/subscriptions",
# "action"=>"create",
# "plan_id"=>"WDTK-pro"}
def create
begin
@pro_account = current_user.pro_account ||= current_user.build_pro_account

# Ensure previous incomplete subscriptions are cancelled to prevent them
# from using the new card
# from using the new token/card
@pro_account.subscriptions.incomplete.map(&:delete)

@token = Stripe::Token.retrieve(params[:stripe_token])
Expand Down Expand Up @@ -114,10 +106,10 @@ def authorise
end

rescue Stripe::RateLimitError,
Stripe::InvalidRequestError,
Stripe::AuthenticationError,
Stripe::APIConnectionError,
Stripe::StripeError => e
Stripe::InvalidRequestError,
Stripe::AuthenticationError,
Stripe::APIConnectionError,
Stripe::StripeError => e
if send_exception_notifications?
ExceptionNotifier.notify_exception(e, env: request.env)
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/alaveteli_pro/plan_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def billing_frequency(plan)
elsif interval(plan) == 'year' && interval_count(plan) == 1
_('Billed: Annually')
else
_('Billed: every {{interval}}', interval: interval(plan))
_('Billed: every {{interval}}', interval: pluralize_interval(plan))
end
end

def billing_interval(plan)
if interval_count(plan) == 1
_('per user, per {{interval}}', interval: pluralize_interval(plan))
_('per user, per {{interval}}', interval: interval(plan))
else
_('per user, every {{interval}}', interval: pluralize_interval(plan))
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/alaveteli_pro/subscription/discount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def discounted_amount
end

def discounted?
discounted_amount < plan.amount
reduction > 0
end

def discount_name
Expand Down
6 changes: 6 additions & 0 deletions spec/helpers/alaveteli_pro/plan_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
allow(plan).to receive(:interval_count).and_return(1)
expect(helper.billing_frequency(plan)).to eq('Billed: every quarter')
end

it 'returns custom message for intervals with count greater then 1' do
allow(plan).to receive(:interval).and_return('week')
allow(plan).to receive(:interval_count).and_return(2)
expect(helper.billing_frequency(plan)).to eq('Billed: every 2 weeks')
end
end

describe '#billing_interval' do
Expand Down

0 comments on commit c61fd03

Please sign in to comment.