From 968448cea22364f1fd423cedc54cf6d8a0f2e09e Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 2 Jul 2013 12:52:16 +0200 Subject: billing: update customer records - do not create new ones --- billing/app/controllers/customer_controller.rb | 45 +++++++++++--------------- 1 file changed, 18 insertions(+), 27 deletions(-) (limited to 'billing/app/controllers/customer_controller.rb') diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb index 1fa19c9..887dbfd 100644 --- a/billing/app/controllers/customer_controller.rb +++ b/billing/app/controllers/customer_controller.rb @@ -1,14 +1,12 @@ class CustomerController < BillingBaseController before_filter :authorize - before_filter :fetch_customer_data, :only => [:show, :edit] #confirm??? - def show - @active_subscription = @customer.subscriptions(@braintree_data) + @active_subscription = customer.subscriptions 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' + if customer.has_payment_info? + redirect_to edit_customer_path(customer), :notice => 'Here is your saved customer data' else fetch_new_transparent_redirect_data end @@ -20,17 +18,12 @@ class CustomerController < BillingBaseController 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.braintree_customer = @result.customer customer.save - #current_user.save! - #debugger 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! + elsif customer.has_payment_info? fetch_edit_transparent_redirect_data render :action => "edit" else @@ -39,18 +32,7 @@ class CustomerController < BillingBaseController end end - private - - def fetch_customer_data - if ((@customer = Customer.find_by_user_id(current_user.id)) and - (params[:id] == @customer.braintree_customer_id)) - @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 + protected def fetch_new_transparent_redirect_data @tr_data = Braintree::TransparentRedirect. @@ -58,10 +40,19 @@ class CustomerController < BillingBaseController end def fetch_edit_transparent_redirect_data + customer.with_braintree_data! + @default_cc = customer.default_credit_card @tr_data = Braintree::TransparentRedirect. update_customer_data(:redirect_url => confirm_customer_url, - :customer_id => params[:id]) ##?? - + :customer_id => customer.braintree_customer_id) ##?? end + def customer + @customer ||= Customer.find(params[:id]) if params[:id] # edit, show + @customer ||= Customer.find_by_user_id(current_user.id) # confirm + @customer ||= Customer.new(user: current_user) + # TODO will want case for admins, presumably + access_denied unless @customer.user == current_user + return @customer + end end -- cgit v1.2.3