summaryrefslogtreecommitdiff
path: root/engines/billing/app/controllers/payments_controller.rb
blob: 871f1b45d1914ce4b62516bd92a47eb49e0e1161 (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
class PaymentsController < BillingBaseController
  before_filter :require_login, :only => [:index]

  def new
    if current_user.has_payment_info?
      @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
    else
      @client_token = Braintree::ClientToken.generate
   end
  end

# not sure if this should be kept
  def index
    access_denied unless admin? or (@user == current_user)
    customer = Customer.find_by_user_id(@user.id)
    braintree_data = Braintree::Customer.find(customer.braintree_customer_id)
    # these will be ordered by created_at descending, per http://stackoverflow.com/questions/16425475/
    @transactions = braintree_data.transactions
  end

  def confirm
    make_transaction
    if @result.success?
      flash[:success] = I18n.t(:donation_sucess)
    else
      flash[:error] = I18n.t(:donation_not_sucess)
    end
    redirect_to action: :new, locale: params[:locale]
  end


  private
  def make_transaction
    if current_user.has_payment_info?
      transact_without_user_info
   elsif current_user.is_anonymous?
      transact_without_user_info
    else
      transact_with_user_info
    end
  end

  def transact_with_user_info
    @result = Braintree::Transaction.sale(
               amount: params[:amount],
               payment_method_nonce: params[:payment_method_nonce],
               customer: {
                  first_name: params[:first_name],
                  last_name: params[:last_name],
                  company: params[:company],
                  email: current_user.email,
                  phone: params[:phone]
                },
                options: {
                  store_in_vault: true
                })
    current_user.update_attributes(braintree_customer_id: @result.transaction.customer_details.id) if @result.success?
  end

  def transact_without_user_info
    @result = Braintree::Transaction.sale(
               amount: params[:amount],
               payment_method_nonce: params[:payment_method_nonce],
              )
  end
end