From 04288c2a2179d7aa71a2fa21267e6f02fb0400e2 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 21 Mar 2013 13:51:58 -0700 Subject: Unauthenticated users can make single payments (like donations), but payments from authenticated users will be as a Braintree Customer stored in the braintree vault. --- billing/app/controllers/customer_controller.rb | 12 +++++++----- billing/app/controllers/payments_controller.rb | 15 +++++++++++++++ billing/app/helpers/braintree_helper.rb | 14 ++++++++++++++ billing/app/views/customer/edit.html.haml | 9 +++++---- .../app/views/payments/_customer_data.html.haml | 15 +++++++++++++++ .../views/payments/_non_customer_fields.html.haml | 16 ++++++++++++++++ billing/app/views/payments/new.html.haml | 22 +++++----------------- 7 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 billing/app/views/payments/_customer_data.html.haml create mode 100644 billing/app/views/payments/_non_customer_fields.html.haml diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb index dcb94eb..bdb89f7 100644 --- a/billing/app/controllers/customer_controller.rb +++ b/billing/app/controllers/customer_controller.rb @@ -7,12 +7,14 @@ class CustomerController < ApplicationController end def edit - current_customer = Customer.find_by_user_id(current_user.id) + customer = Customer.find_by_user_id(current_user.id) #current_customer.with_braintree_data! - #@credit_card = current_customer.default_credit_card + # @credit_card = current_customer.default_credit_card + @braintree_data = Braintree::Customer.find(customer.braintree_customer_id) + @default_cc = @braintree_data.credit_cards.find { |cc| cc.default? } @tr_data = Braintree::TransparentRedirect. update_customer_data(:redirect_url => confirm_customer_url, - :customer_id => current_customer.braintree_customer_id) + :customer_id => customer.braintree_customer_id) end def confirm @@ -26,8 +28,8 @@ class CustomerController < ApplicationController #current_user.save! render :action => "confirm" #elsif current_user.has_payment_info? - elsif (current_customer = Customer.find_by_user_id(current_user.id)) and current_customer.has_payment_info? - current_customer.with_braintree_data! #todo + elsif (customer = Customer.find_by_user_id(current_user.id)) and customer.has_payment_info? + customer.with_braintree_data! #todo render :action => "edit" else render :action => "new" diff --git a/billing/app/controllers/payments_controller.rb b/billing/app/controllers/payments_controller.rb index 2a76bb1..79a6433 100644 --- a/billing/app/controllers/payments_controller.rb +++ b/billing/app/controllers/payments_controller.rb @@ -1,6 +1,21 @@ class PaymentsController < ApplicationController def new @amount = calculate_amount + if current_user + if @customer = Customer.find_by_user_id(current_user.id) + @braintree_data = Braintree::Customer.find(@customer.braintree_customer_id) + @default_cc = @braintree_data.credit_cards.find { |cc| cc.default? } + @tr_data = Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url, + :transaction => {:type => "sale", :amount => @amount, :customer_id => @customer.braintree_customer_id, :options => {:submit_for_settlement => true } }) + else + redirect_to new_customer_path + end + else + # anonymous payment not attributed to any user (ie, donation) + @tr_data = Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url, + :transaction => {:type => "sale", :amount => @amount, :options => {:submit_for_settlement => true } }) + end + end def confirm diff --git a/billing/app/helpers/braintree_helper.rb b/billing/app/helpers/braintree_helper.rb index 3d6e887..bc78c02 100644 --- a/billing/app/helpers/braintree_helper.rb +++ b/billing/app/helpers/braintree_helper.rb @@ -7,6 +7,7 @@ module BraintreeHelper super @braintree_params = @options[:params] @braintree_errors = @options[:errors] + @braintree_existing = @options[:existing] end def fields_for(record_name, *args, &block) @@ -31,6 +32,19 @@ module BraintreeHelper def determine_value(method) if @braintree_params @braintree_params[method] + elsif @braintree_existing + + if @braintree_existing.kind_of?(Braintree::CreditCard) + + case method + when :number + method = :masked_number + when :cvv + return nil + end + end + + @braintree_existing.send(method) else nil end diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml index c203bb5..21fcfa3 100644 --- a/billing/app/views/customer/edit.html.haml +++ b/billing/app/views/customer/edit.html.haml @@ -2,7 +2,7 @@ #total-errors{:style => "color:red;"} = h(@result.errors.size) error(s) -= form_for :customer, :url => Braintree::TransparentRedirect.url, :params => @result && @result.params[:customer],:existing => current_user, :builder => BraintreeHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| | += form_for :customer, :url => Braintree::TransparentRedirect.url, :params => @result && @result.params[:customer],:existing => @braintree_data, :builder => BraintreeHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| | = field_set_tag "Customer" do %dl %dt= f.label :first_name, 'First Name' @@ -11,10 +11,11 @@ %dd= f.text_field :last_name %dt= f.label :phone, 'Phone' %dd= f.text_field :phone - - if @credit_card + - if @default_cc %ul %li - = @credit_card.masked_number - (#{link_to @credit_card.token, edit_credit_card_info_path(:id => @credit_card.token)}) + Default Credit Card + = @default_cc.masked_number + = # (#{link_to @default_cc.token, edit_credit_card_info_path(:id => @default_cc.token)}) = hidden_field_tag :tr_data, @tr_data = f.submit 'Save Payment Info' \ No newline at end of file diff --git a/billing/app/views/payments/_customer_data.html.haml b/billing/app/views/payments/_customer_data.html.haml new file mode 100644 index 0000000..900fbd8 --- /dev/null +++ b/billing/app/views/payments/_customer_data.html.haml @@ -0,0 +1,15 @@ +%h2 Customer Information +%dl + %dt First Name + %dd= @braintree_data.first_name + %dt Last Name + %dd= @braintree_data.last_name + %dt Phone + %dd= @braintree_data.phone +%h2 Credit Card Information +%dl + %dt Number + %dd= @default_cc.masked_number + %dt Expiration Date + %dd= @default_cc.expiration_date += link_to 'edit saved data', edit_customer_path(@customer.braintree_customer_id), :class => :btn diff --git a/billing/app/views/payments/_non_customer_fields.html.haml b/billing/app/views/payments/_non_customer_fields.html.haml new file mode 100644 index 0000000..99b420d --- /dev/null +++ b/billing/app/views/payments/_non_customer_fields.html.haml @@ -0,0 +1,16 @@ += field_set_tag "Customer" do + = f.fields_for :customer do |c| + %div= c.label :first_name, "First Name" + %div= c.text_field :first_name + %div= c.label :last_name, "Last Name" + %div= c.text_field :last_name + %div= c.label :email, "Email" + %div= c.text_field :email += field_set_tag "Credit Card" do + = f.fields_for :credit_card do |c| + %div= c.label :number, "Number" + %div= c.text_field :number + %div= c.label :expiration_date, "Expiration Date (MM/YY)" + %div= c.text_field :expiration_date + %div= c.label :cvv, "CVV" + %div= c.text_field :cvv \ No newline at end of file diff --git a/billing/app/views/payments/new.html.haml b/billing/app/views/payments/new.html.haml index b6dd713..d9c41ce 100644 --- a/billing/app/views/payments/new.html.haml +++ b/billing/app/views/payments/new.html.haml @@ -5,21 +5,9 @@ = h @result.errors.size error(s) = form_for :transaction, :params => @result && @result.params[:transaction], :errors => @result && @result.errors.for(:transaction), :builder => BraintreeHelper::BraintreeFormBuilder, :url => Braintree::TransparentRedirect.url, :html => {:autocomplete => "off"} do |f| - = field_set_tag "Customer" do - = f.fields_for :customer do |c| - %div= c.label :first_name, "First Name" - %div= c.text_field :first_name - %div= c.label :last_name, "Last Name" - %div= c.text_field :last_name - %div= c.label :email, "Email" - %div= c.text_field :email - = field_set_tag "Credit Card" do - = f.fields_for :credit_card do |c| - %div= c.label :number, "Number" - %div= c.text_field :number - %div= c.label :expiration_date, "Expiration Date (MM/YY)" - %div= c.text_field :expiration_date - %div= c.label :cvv, "CVV" - %div= c.text_field :cvv - = hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url,:transaction => {:type => "sale", :amount => @amount, :options => {:submit_for_settlement => true } }) + - if !@customer + = render :partial => 'non_customer_fields', :locals => {:f => f} + - else + = render :partial => 'customer_data' + = hidden_field_tag :tr_data, @tr_data = f.submit "Submit" \ No newline at end of file -- cgit v1.2.3