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
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
|