Class: Account::SubscriptionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/tang/account/subscriptions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/tang/account/subscriptions_controller.rb', line 33

def create
  # TODO: check how it's possible to get here without a stripe_token
  plan = Plan.find(subscription_params[:plan])
  @subscription = CreateSubscription.call(
    plan,
    current_customer,
    params[:stripe_token]
  )

  if @subscription.errors.blank?
    flash[:upgrade] = 'true' if @subscription.upgraded
    redirect_to , notice: 'Subscription was successfully created.'
  else
    render :new
  end
end

#destroyObject



72
73
74
75
76
# File 'app/controllers/tang/account/subscriptions_controller.rb', line 72

def destroy
  # @subscription.destroy
  @subscription.cancel!
  redirect_to , notice: 'Subscription was successfully cancelled.'
end

#newObject



25
26
27
28
29
30
31
# File 'app/controllers/tang/account/subscriptions_controller.rb', line 25

def new
  plan = Plan.find(params[:plan])
  @subscription = Subscription.new(
    plan: plan,
    customer: current_customer
  )
end

#showObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/tang/account/subscriptions_controller.rb', line 7

def show
  render :not_stripe and return unless current_customer.stripe_enabled?

  @plans = Plan.where(interval: 'month').order(:order)

  if @subscription.present? && @subscription.plan.present?
    puts "subscription present and plan present"
    @next_plan = @plans.where('tang_plans.order > ?', @subscription.plan.order).first
    @previous_plan = @plans.where('tang_plans.order < ?', @subscription.plan.order).last
  else
    puts "subscription not present or plan not present"
    @next_plan = @plans.first
    @previous_plan = nil
  end

  @receipts = current_customer.charges.order(created: :desc).limit(5)
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/tang/account/subscriptions_controller.rb', line 50

def update
  if @subscription.nil?
    redirect_to , notice: 'Sorry, we could not find your subscription.' and return
  end

  plan = Plan.find(params[:plan])
  @subscription = ChangeSubscription.call(
    @subscription,
    plan
  )
  if @subscription.errors.blank?
    flash[:upgrade] = 'true' if @subscription.upgraded
    redirect_to , notice: 'Subscription was successfully changed.'
  else
    @plans = Plan.order(:order)
    @next_plan = @plans.where('tang_plans.order > ?', @subscription.plan.order).first
    @previous_plan = @plans.where('tang_plans.order < ?', @subscription.plan.order).last
    @receipts = current_customer.charges.order(created: :desc).limit(5)
    render :show
  end
end