Class: Billingly::SubscriptionsController

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

Overview

This controller takes care of managing subscriptions.

Instance Method Summary collapse

Instance Method Details

#createObject

Subscribe the customer to a plan, or change his current plan.



16
17
18
19
20
21
22
23
# File 'app/controllers/billingly/subscriptions_controller.rb', line 16

def create
  plan = Billingly::Plan.find(params[:plan_id])
  unless current_customer.can_subscribe_to?(plan)
    return redirect_to subscriptions_path, notice: 'Cannot subscribe to that plan'
  end
  current_customer.subscribe_to_plan(plan)
  on_subscription_success
end

#deactivateObject

Unsubscribes the customer from his last subscription and deactivates his account. Performing this action would set the deactivation reason to be ‘left_voluntarily’



39
40
41
42
# File 'app/controllers/billingly/subscriptions_controller.rb', line 39

def deactivate
  current_customer.deactivate_left_voluntarily
  redirect_to(action: :index)
end

#indexObject

Index shows the current subscription to customers while they are active. It’s also the page that prompts them to reactivate their account when deactivated. It’s likely the only reachable page for deactivated customers.



9
10
11
12
13
# File 'app/controllers/billingly/subscriptions_controller.rb', line 9

def index
  @subscription = current_customer.active_subscription
  @plans = Billingly::Plan.where('hidden = false')
  @invoices = current_customer.invoices.order('created_at DESC')
end

#invoiceObject

TODO:

This should actually be the #show action of an InvoicesController but we’re lazy ATM.

Shows an invoice.



47
48
49
# File 'app/controllers/billingly/subscriptions_controller.rb', line 47

def invoice
  @invoice = current_customer.invoices.find(params[:invoice_id])
end

#on_reactivation_successObject

Should be overriden to provide a response when the user account is reactivated.

Defaults to a redirect to :index



65
66
67
# File 'app/controllers/billingly/subscriptions_controller.rb', line 65

def on_reactivation_success
  redirect_to(action: :index) 
end

#on_subscription_successObject

When a subscription is sucessful this callback is triggered. Host applications should override it by subclassing this subscriptionscontroller, and include their own behaviour, and for example grant the privileges associated to the subscription plan.

Redirects to the last invoice by default.



57
58
59
60
# File 'app/controllers/billingly/subscriptions_controller.rb', line 57

def on_subscription_success
  current_customer.reload
  redirect_to(invoice_subscriptions_path(current_customer.active_subscription.invoices.last.id))
end

#reactivateObject

Action to reactivate an account for a customer that left voluntarily and does not owe any money to us. Their account will be reactivated to their old subscription plan immediately. They can change plans afterwards.



29
30
31
32
33
34
35
# File 'app/controllers/billingly/subscriptions_controller.rb', line 29

def reactivate
  plan = Billingly::Plan.find_by_id(params[:plan_id])
  if current_customer.reactivate(plan).nil?
    return render nothing: true, status: 403
  end
  on_reactivation_success
end