summaryrefslogtreecommitdiff
path: root/billing/app/controllers/customers_controller.rb
blob: 2532334de205343e5f6835cd768de9a5f667ef47 (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
class CustomersController < BillingBaseController
  before_filter :authorize
  before_filter :fetch_customer_data, :only => [:show, :edit]


  def show
    @active_subscription = @customer.subscriptions(@braintree_data)
  end

  def new
    if customer = Customer.find_by_user_id(current_user.id)
      redirect_to edit_customer_path(customer.braintree_customer_id), :notice => 'Here is your saved customer data'
    else
      @tr_data = Braintree::TransparentRedirect.
        create_customer_data(:redirect_url => confirm_customer_url)
    end
  end

  def edit
    @tr_data = Braintree::TransparentRedirect.
      update_customer_data(:redirect_url => confirm_customer_url,
                           :customer_id => params[:id])
  end

  def confirm
    @result = Braintree::TransparentRedirect.confirm(request.query_string)

    if @result.success?
      # customer = Customer.new(:user_id => current_user.id, :braintree_customer_id =>  @result.customer.id)
      customer = Customer.new(:braintree_customer_id =>  @result.customer.id)
      customer.user = current_user
      customer.save
      #current_user.save!
      render :action => "confirm"
    #elsif current_user.has_payment_info?
    elsif (customer = Customer.find_by_user_id(current_user.id)) and customer.has_payment_info?
      #customer.with_braintree_data!
      render :action => "edit"
    else
      render :action => "new"
    end
  end

  private

  def fetch_customer_data
    if ((@customer = Customer.find_by_user_id(current_user.id)) and
        (params[:id] == @customer.braintree_customer_id))
      #current_customer.with_braintree_data!
      @braintree_data = Braintree::Customer.find(params[:id]) #used in editing form
      @default_cc = @customer.default_credit_card(@braintree_data)
    else
      # TODO will want case for admins, presumably
      access_denied
    end
  end

end