diff options
-rw-r--r-- | billing/app/controllers/payments_controller.rb | 15 | ||||
-rw-r--r-- | billing/app/helpers/braintree_form_helper.rb | 64 | ||||
-rw-r--r-- | billing/app/helpers/braintree_helper.rb | 59 | ||||
-rw-r--r-- | billing/app/views/customer/edit.html.haml | 2 | ||||
-rw-r--r-- | billing/app/views/customer/new.html.haml | 2 |
5 files changed, 77 insertions, 65 deletions
diff --git a/billing/app/controllers/payments_controller.rb b/billing/app/controllers/payments_controller.rb index bd01fea..965e417 100644 --- a/billing/app/controllers/payments_controller.rb +++ b/billing/app/controllers/payments_controller.rb @@ -4,15 +4,13 @@ class PaymentsController < ApplicationController 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", :customer_id => @customer.braintree_customer_id, :options => {:submit_for_settlement => true } }) + @tr_data = transparent_redirect(@customer.braintree_customer_id) 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", :options => {:submit_for_settlement => true } }) + @tr_data = transparent_redirect end end @@ -26,4 +24,13 @@ class PaymentsController < ApplicationController end end + protected + + def transparent_redirect(braintree_customer_id = nil) + Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url, + :transaction => {:type => "sale", :customer_id => braintree_customer_id, :options => {:submit_for_settlement => true } }) + + end + + end diff --git a/billing/app/helpers/braintree_form_helper.rb b/billing/app/helpers/braintree_form_helper.rb new file mode 100644 index 0000000..cb322fa --- /dev/null +++ b/billing/app/helpers/braintree_form_helper.rb @@ -0,0 +1,64 @@ +module BraintreeFormHelper + class BraintreeFormBuilder < ActionView::Helpers::FormBuilder + include ActionView::Helpers::AssetTagHelper + include ActionView::Helpers::TagHelper + + def initialize(object_name, object, template, options, proc) + super + @braintree_params = @options[:params] + @braintree_errors = @options[:errors] + @braintree_existing = @options[:existing] + end + + def fields_for(record_name, *args, &block) + options = args.extract_options! + options[:builder] = BraintreeFormBuilder + options[:params] = @braintree_params && @braintree_params[record_name] + options[:errors] = @braintree_errors && @braintree_errors.for(record_name) + new_args = args + [options] + super record_name, *new_args, &block + end + + def text_field(method, options = {}) + has_errors = @braintree_errors && @braintree_errors.on(method).any? + field = super(method, options.merge(:value => determine_value(method))) + result = content_tag("div", field, :class => has_errors ? "fieldWithErrors" : "") + result.safe_concat validation_errors(method) + result + end + + protected + + 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 + end + + def validation_errors(method) + if @braintree_errors && @braintree_errors.on(method).any? + @braintree_errors.on(method).map do |error| + content_tag("div", ERB::Util.h(error.message), {:style => "color: red;"}) + end.join + else + "" + end + end + end +end + diff --git a/billing/app/helpers/braintree_helper.rb b/billing/app/helpers/braintree_helper.rb index bc78c02..2d18b6c 100644 --- a/billing/app/helpers/braintree_helper.rb +++ b/billing/app/helpers/braintree_helper.rb @@ -1,64 +1,5 @@ module BraintreeHelper - class BraintreeFormBuilder < ActionView::Helpers::FormBuilder - include ActionView::Helpers::AssetTagHelper - include ActionView::Helpers::TagHelper - def initialize(object_name, object, template, options, proc) - super - @braintree_params = @options[:params] - @braintree_errors = @options[:errors] - @braintree_existing = @options[:existing] - end - def fields_for(record_name, *args, &block) - options = args.extract_options! - options[:builder] = BraintreeFormBuilder - options[:params] = @braintree_params && @braintree_params[record_name] - options[:errors] = @braintree_errors && @braintree_errors.for(record_name) - new_args = args + [options] - super record_name, *new_args, &block - end - - def text_field(method, options = {}) - has_errors = @braintree_errors && @braintree_errors.on(method).any? - field = super(method, options.merge(:value => determine_value(method))) - result = content_tag("div", field, :class => has_errors ? "fieldWithErrors" : "") - result.safe_concat validation_errors(method) - result - end - - protected - - 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 - end - - def validation_errors(method) - if @braintree_errors && @braintree_errors.on(method).any? - @braintree_errors.on(method).map do |error| - content_tag("div", ERB::Util.h(error.message), {:style => "color: red;"}) - end.join - else - "" - end - end - end end diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml index 21fcfa3..25dfc79 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 => @braintree_data, :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 => BraintreeFormHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| | = field_set_tag "Customer" do %dl %dt= f.label :first_name, 'First Name' diff --git a/billing/app/views/customer/new.html.haml b/billing/app/views/customer/new.html.haml index 0e1557a..2ff8229 100644 --- a/billing/app/views/customer/new.html.haml +++ b/billing/app/views/customer/new.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], :builder => BraintreeHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| += form_for :customer, :url => Braintree::TransparentRedirect.url, :params => @result && @result.params[:customer], :builder => BraintreeFormHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| = field_set_tag "Customer" do %dl %dt= f.label :first_name, 'First Name' |