Class: PaidUp::SubscriptionsController

Inherits:
PaidUpController show all
Defined in:
app/controllers/paid_up/subscriptions_controller.rb

Overview

Subscriptions Controller

Instance Method Summary collapse

Instance Method Details

#createObject



16
17
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
57
58
59
60
61
62
63
64
65
# File 'app/controllers/paid_up/subscriptions_controller.rb', line 16

def create
  # @plan set by #set_plan
  current_user.update_attribute(:coupon_code, params[:coupon_code])
  if current_user.subscribe_to_plan(@plan, params[:stripeToken])
    subscription_id = current_user.stripe_data.subscriptions.first.id
    discount = current_user.stripe_data.discount
    if !discount.nil? && !discount.coupon.nil? && @plan.amount != 0
      orig_amount = @plan.amount
      amount = orig_amount
      amount -= (discount.coupon.percent_off || 0) * 0.01 * amount
      amount -= (discount.coupon.amount_off || 0)
      amount = amount > 0 ? amount : 0
      money = Money.new(amount, @plan.currency)
    else
      money = @plan.money
    end
    flash[:paid_up_google_analytics_data] = {
      transactionId: subscription_id,
      transactionTotal: money.dollars,
      transactionProducts: [
        sku: @plan.stripe_id,
        name: @plan.title,
        price: @plan.money.dollars,
        quantity: '1'
      ]
    }
    redirect_to(
      subscriptions_path,
      flash: {
        notice: :you_are_now_subscribed_to_the_plan.l(
          plan_name: current_user.plan.title
        )
      }
    )
  else
    redirect_to(
      new_plan_subscription_path(@plan),
      flash: {
        error: current_user.errors.full_messages ||
               :could_not_subscribe_to_plan.l(plan: @plan.title)
      }
    )
  end
rescue Stripe::InvalidRequestError => e
  flash[:error] = e.message
  redirect_to subscriptions_path
rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to new_plan_subscription_path
end

#indexObject



7
8
9
# File 'app/controllers/paid_up/subscriptions_controller.rb', line 7

def index
  # nothing to do, everything we need is in current_user
end

#newObject



11
12
13
14
# File 'app/controllers/paid_up/subscriptions_controller.rb', line 11

def new
  # nothing to do, @plan set by #set_plan
  (current_user.can_downgrade_to?(@plan) || @plan.amount == 0) && create
end