Class: Effective::SubscriptionsController

Inherits:
ApplicationController
  • Object
show all
Includes:
EffectiveCartsHelper, EffectiveStripeHelper
Defined in:
app/controllers/effective/subscriptions_controller.rb

Constant Summary

Constants included from EffectiveStripeHelper

EffectiveStripeHelper::STRIPE_CONNECT_AUTHORIZE_URL, EffectiveStripeHelper::STRIPE_CONNECT_TOKEN_URL

Instance Method Summary collapse

Methods included from EffectiveStripeHelper

#is_stripe_connect_seller?, #link_to_new_stripe_connect_customer, #stripe_coupon_description, #stripe_plan_description, #stripe_plans_collection

Methods included from EffectiveCartsHelper

#current_cart, #link_to_add_to_cart, #link_to_checkout, #link_to_current_cart, #link_to_empty_cart, #link_to_remove_from_cart, #render_cart, #render_purchasables

Instance Method Details

#createObject



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

def create
  @page_title ||= 'New Subscription'

  # Don't let the user create another Subscription object if it's already created
  @subscription = @customer.subscriptions.where(:stripe_plan_id => subscription_params[:stripe_plan_id]).first_or_initialize

  EffectiveOrders.authorized?(self, :create, @subscription)

  if @subscription.update_attributes(subscription_params) && (current_cart.find(@subscription).present? || current_cart.add(@subscription))
    flash[:success] = "Successfully added subscription to cart"
    redirect_to effective_orders.new_order_path
  else
    purchased_plans = @customer.subscriptions.purchased.map(&:stripe_plan_id)
    @plans = Stripe::Plan.all.reject { |stripe_plan| purchased_plans.include?(stripe_plan.id) }

    flash[:danger] ||= 'Unable to add subscription to cart.  Please try again.'
    render :action => :new
  end
end

#destroyObject

Raises:

  • (ActiveRecord::RecordNotFound)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/effective/subscriptions_controller.rb', line 82

def destroy
  @plan = Stripe::Plan.retrieve(params[:id])
  raise ActiveRecord::RecordNotFound unless @plan.present?

  @subscription = @customer.subscriptions.find { |subscription| subscription.stripe_plan_id == params[:id] }
  @stripe_subscription = @subscription.try(:stripe_subscription)
  raise ActiveRecord::RecordNotFound unless @subscription.present?

  EffectiveOrders.authorized?(self, :destroy, @subscription)

  if @subscription.present?
    begin
      @stripe_subscription.delete if @stripe_subscription
      @subscription.destroy
      flash[:success] = "Successfully unsubscribed from #{params[:id]}"
    rescue => e
      flash[:danger] = "Unable to unsubscribe.  Message: \"#{e.message}\"."
    end
  else
    flash[:danger] = "Unable to find stripe subscription for #{params[:id]}" unless @subscription.present?
  end

  redirect_to effective_orders.subscriptions_path
end

#indexObject

This is a ‘My Subscriptions’ page



12
13
14
15
16
17
18
# File 'app/controllers/effective/subscriptions_controller.rb', line 12

def index
  @page_title ||= 'My Subscriptions'

  @subscriptions = @customer.subscriptions.purchased

  EffectiveOrders.authorized?(self, :index, Effective::Subscription)
end

#newObject



20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/effective/subscriptions_controller.rb', line 20

def new
  @page_title ||= 'New Subscription'

  @subscription = @customer.subscriptions.new()

  purchased_plans = @customer.subscriptions.purchased.map(&:stripe_plan_id)
  @plans = Stripe::Plan.all.reject { |stripe_plan| purchased_plans.include?(stripe_plan.id) }

  EffectiveOrders.authorized?(self, :new, @subscription)
end

#showObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/effective/subscriptions_controller.rb', line 51

def show
  @plan = Stripe::Plan.retrieve(params[:id])

  unless @plan.present?
    flash[:danger] = "Unrecognized Stripe Plan: #{params[:id]}"
    raise ActiveRecord::RecordNotFound
  end

  @subscription = @customer.subscriptions.find { |subscription| subscription.stripe_plan_id == params[:id] }

  unless @subscription.present?
    flash[:danger] = "Unable to find Customer Subscription for plan: #{params[:id]}"
    raise ActiveRecord::RecordNotFound
  end

  @stripe_subscription = @subscription.try(:stripe_subscription)

  unless @stripe_subscription.present?
    flash[:danger] = "Unable to find Stripe Subscription for plan: #{params[:id]}"
    raise ActiveRecord::RecordNotFound
  end

  EffectiveOrders.authorized?(self, :show, @subscription)

  @invoices = @customer.stripe_customer.invoices.all.select do |invoice| 
    invoice.lines.any? { |line| line.id == @stripe_subscription.id }
  end

  @page_title ||= "#{@plan.name}"
end