Class: StripeSaas::SubscriptionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/stripe_saas/subscriptions_controller.rb

Instance Method Summary collapse

Instance Method Details

#cancelObject



161
162
163
164
165
166
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 161

def cancel
  flash[:notice] = "You've successfully cancelled your subscription."
  @subscription.plan_id = nil
  @subscription.save
  redirect_to owner_subscription_path(@owner, @subscription)
end

#createObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 145

def create
  @subscription = ::Subscription.new(subscription_params)
  @subscription.subscription_owner = @owner

  if @subscription.save
    flash[:notice] = after_new_subscription_message
    redirect_to after_new_subscription_path
  else
    flash[:error] = 'There was a problem processing this transaction.'
    render :new
  end
end

#current_ownerObject

the following two methods allow us to show the pricing table before someone has an account. by default these support devise, but they can be overriden to support others.



73
74
75
76
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 73

def current_owner
  # e.g. "self.current_user"
  send "current_#{StripeSaas.subscriptions_owned_by}"
end

#editObject



168
169
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 168

def edit
end

#indexObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 99

def index
  # don't bother showing the index if they've already got a subscription.
  if current_owner and current_owner.subscription.present?
    redirect_to stripe_saas.edit_owner_subscription_path(current_owner, current_owner.subscription)
  end

  # Load all plans.
  @plans = ::Plan.order(:display_order).all

  # Don't prep a subscription unless a user is authenticated.
  unless no_owner?
    # we should also set the owner of the subscription here.
    @subscription = ::Subscription.new({StripeSaas.owner_id_sym => @owner.id})
    @subscription.subscription_owner = @owner
  end

end

#load_ownerObject

subscription.subscription_owner



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 18

def load_owner
  unless params[:owner_id].nil?
    if current_owner.present?

      # we need to try and look this owner up via the find method so that we're
      # taking advantage of any override of the find method that would be provided
      # by older versions of friendly_id. (support for newer versions default behavior
      # below.)
      searched_owner = current_owner.class.find(params[:owner_id]) rescue nil

      # if we couldn't find them that way, check whether there is a new version of
      # friendly_id in place that we can use to look them up by their slug.
      # in christoph's words, "why?!" in my words, "warum?!!!"
      # (we debugged this together on skype.)
      if searched_owner.nil? && current_owner.class.respond_to?(:friendly)
        searched_owner = current_owner.class.friendly.find(params[:owner_id]) rescue nil
      end

      if current_owner.try(:id) == searched_owner.try(:id)
        @owner = current_owner
      else
        customer = Subscription.find_customer(searched_owner)
        customer_2 = Subscription.find_customer(current_owner)
        # susbscription we are looking for belongs to the same user but to a different
        # subscription owner
        # e.g. user -> account -> subscription
        # same user but different accounts for example

        if customer_2.try(:id) == customer.try(:id)
          @owner = searched_owner
        else
          return unauthorized
        end
      end
    else
      return unauthorized
    end
  end
end

#load_plansObject



8
9
10
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 8

def load_plans
  @plans = ::Plan.order(:price_cents)
end

#load_subscriptionObject



62
63
64
65
66
67
68
69
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 62

def load_subscription
  # config.subscriptions_owned_by = :account
  # config.customer_accessor = :owner
  ownership_attribute = :"#{StripeSaas.subscriptions_owned_by}_id"
  @subscription = ::Subscription.where(ownership_attribute => current_owner.id).find_by_id(params[:id]) ||
                  ::Subscription.where(ownership_attribute => @owner.id).find_by_id(params[:id])
  return @subscription.present? ? @subscription : unauthorized
end

#newObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 117

def new
  if no_owner?

    if defined?(Devise)

      # by default these methods support devise.
      if current_owner
        redirect_to new_owner_subscription_path(current_owner, plan: params[:plan])
      else
        
      end

    else
      raise "This feature depends on Devise for authentication."
    end

  else
    @subscription = ::Subscription.new
    @subscription.plan = ::Plan.find_by_stripe_id(params[:plan])
  end
end

#no_owner?Boolean

Returns:

  • (Boolean)


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

def no_owner?
  @owner.nil?
end

#redirect_to_sign_upObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 78

def 
  # this is a Devise default variable and thus should not change its name
  # when we change subscription owners from :user to :company
  begin
    plan = ::Plan.find(params[:plan])
  rescue ActiveRecord::RecordNotFound
    plan = ::Plan.by_stripe_id(params[:plan])
  end

  if plan
    unless plan.free?
      session["user_return_to"] = new_subscription_path(plan: plan)
    end

    devise_scope = (StripeSaas.devise_scope || StripeSaas.subscriptions_owned_by).to_s
    redirect_to new_registration_path(devise_scope, plan: plan)
  else
    redirect_to new_registration_path
  end
end

#showObject



158
159
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 158

def show
end

#show_existing_subscriptionObject



139
140
141
142
143
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 139

def show_existing_subscription
  if @owner.subscription.present?
    redirect_to owner_subscription_path(@owner, @owner.subscription)
  end
end

#unauthorizedObject



12
13
14
15
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 12

def unauthorized
  render status: 401, template: "stripe_saas/subscriptions/unauthorized"
  false
end

#updateObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/stripe_saas/subscriptions_controller.rb', line 171

def update
  new_plan_id = subscription_params[:plan_id]
  new_plan = ::Plan.find(new_plan_id)

  if @subscription.plan.free? && !new_plan.free? && subscription_params[:credit_card_token].nil?
    flash[:notice] = "Please enter payment information to upgrade."
    redirect_to edit_owner_subscription_path(@owner, @subscription, update: 'card', plan: new_plan_id)
  else
    if @subscription.update_attributes(subscription_params)
      flash[:notice] = "You've successfully updated your subscription."
      redirect_to edit_owner_subscription_path(@owner, @subscription)
    else
      flash[:error] = 'There was a problem processing this transaction.'
      render :edit
    end
  end
end