From bee5c4a93f6bf664609673d7e703476f15147fe3 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 1 Apr 2013 11:35:10 -0700 Subject: Some tweaks to optimize number of requests to braintree API. --- billing/app/controllers/customer_controller.rb | 10 +++++----- billing/app/controllers/payments_controller.rb | 2 +- billing/app/models/customer.rb | 9 +++++---- 3 files changed, 11 insertions(+), 10 deletions(-) (limited to 'billing/app') diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb index 8b4b124..9ad6d93 100644 --- a/billing/app/controllers/customer_controller.rb +++ b/billing/app/controllers/customer_controller.rb @@ -11,11 +11,11 @@ class CustomerController < BillingBaseController end def edit - if (params[:id] == Customer.find_by_user_id(current_user.id).braintree_customer_id) + if ((customer = Customer.find_by_user_id(current_user.id)) and + (params[:id] == customer.braintree_customer_id)) #current_customer.with_braintree_data! - # @credit_card = current_customer.default_credit_card - @braintree_data = Braintree::Customer.find(params[:id]) - @default_cc = @braintree_data.credit_cards.find { |cc| cc.default? } + @braintree_data = Braintree::Customer.find(params[:id]) #used in editing form + @default_cc = customer.default_credit_card(@braintree_data) @tr_data = Braintree::TransparentRedirect. update_customer_data(:redirect_url => confirm_customer_url, :customer_id => params[:id]) @@ -37,7 +37,7 @@ class CustomerController < BillingBaseController 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! #todo + #customer.with_braintree_data! render :action => "edit" else render :action => "new" diff --git a/billing/app/controllers/payments_controller.rb b/billing/app/controllers/payments_controller.rb index 965e417..41b7b3e 100644 --- a/billing/app/controllers/payments_controller.rb +++ b/billing/app/controllers/payments_controller.rb @@ -3,7 +3,7 @@ class PaymentsController < ApplicationController 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? } + @default_cc = @customer.default_credit_card(@braintree_data) @tr_data = transparent_redirect(@customer.braintree_customer_id) else redirect_to new_customer_path diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 0fc3751..8085d32 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -16,21 +16,22 @@ class Customer < CouchRest::Model::Base end # from braintree_ruby_examples/rails3_tr_devise and should be tweaked +=begin def with_braintree_data! return self unless has_payment_info? braintree_data = Braintree::Customer.find(braintree_customer_id) - debugger #FIELDS.each do |field| # send(:"#{field}=", braintree_data.send(field)) #end self end +=end - ##?? - def default_credit_card + #slow to get Braintree Customer data, so pass it if have already retrieved it + def default_credit_card(braintree_data = nil) return unless has_payment_info? - braintree_data = Braintree::Customer.find(braintree_customer_id) + braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id) braintree_data.credit_cards.find { |cc| cc.default? } end -- cgit v1.2.3