blob: 1d29cac5cc6f0bff1319aebaf4098644c1fa1592 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
66
67
68
69
70
71
72
|
class SubscriptionsController < BillingBaseController
before_filter :require_login
before_filter :assign_user
before_filter :confirm_cancel_subscription, only: [:destroy]
before_filter :generate_client_token, only: [:show]
before_filter :get_braintree_customer, only: [:subscribe]
def index
if @user.subscription_id
@subscription = Braintree::Subscription.find @user.subscription_id
@plan = Braintree::Plan.all.select{ |plan| plan.id == @subscription.plan_id }.first
else
@subscriptions = Braintree::Plan.all
end
end
def show
@plan = Braintree::Plan.all.select{ |plan| plan.id == params[:id] }.first
end
def subscribe
@result = Braintree::Subscription.create(payment_method_token: @customer.payment_methods.first.token,
plan_id: params[:id])
if @result.success?
@user.update_attributes subscription_id: @result.subscription.id
flash[:success] = I18n.t(:subscription_sucess)
else
flash[:error] = I18n.t(:subscription_not_sucess)
end
redirect_to action: :index, locale: params[:locale]
end
def unsubscribe
@result = Braintree::Subscription.cancel(@user.subscription_id)
if @result.success?
@user.update_attributes subscription_id: nil
flash[:success] = I18n.t(:unsubscription_sucess)
else
flash[:error] = I18n.t(:unsubscription_not_sucess)
end
redirect_to action: :index, locale: params[:locale]
end
private
def assign_user
@user = current_user
end
def generate_client_token
if current_user.braintree_customer_id
@client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
else
@client_token = Braintree::ClientToken.generate
end
end
def get_braintree_customer
if current_user.braintree_customer_id
@customer = Braintree::Customer.find(current_user.braintree_customer_id)
else
@customer = Braintree::Customer.create(
payment_method_nonce: params[:payment_method_nonce],
first_name: params[:first_name],
last_name: params[:last_name],
company: params[:company],
email: current_user.email,
phone: params[:phone]
).customer
current_user.update_attributes braintree_customer_id: @customer.id
end
end
end
|