summaryrefslogtreecommitdiff
path: root/billing/app/controllers/customer_controller.rb
diff options
context:
space:
mode:
authorjessib <jessib@leap.se>2013-06-10 10:25:37 -0700
committerAzul <azul@leap.se>2013-07-17 10:47:14 +0200
commit87dc4f9d11af4610f534d57d5e51fabca9bb328a (patch)
treeafe5f40cc57fba1b54b539c3e8bc540bd0716241 /billing/app/controllers/customer_controller.rb
parentc4379dcfdc8b27fae15eea5c4ba15b8906e3bb76 (diff)
Back to singular-customer controller, in hopes that would help some confusions.
Diffstat (limited to 'billing/app/controllers/customer_controller.rb')
-rw-r--r--billing/app/controllers/customer_controller.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb
new file mode 100644
index 0000000..505aa6e
--- /dev/null
+++ b/billing/app/controllers/customer_controller.rb
@@ -0,0 +1,57 @@
+class CustomerController < BillingBaseController
+ before_filter :authorize
+ before_filter :fetch_customer_data, :only => [:show, :edit] #confirm???
+
+ def show
+ @active_subscription = @customer.subscriptions(@braintree_data)
+ 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'
+ else
+ @tr_data = Braintree::TransparentRedirect.
+ # create_customer_data(:redirect_url => confirm_customer_url(-1)) # trial
+ create_customer_data(:redirect_url => confirm_customer_url)
+ end
+ end
+
+ def edit
+ @tr_data = Braintree::TransparentRedirect.
+ update_customer_data(:redirect_url => confirm_customer_url,
+ :customer_id => params[:id])
+ end
+
+ 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.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!
+ render :action => "edit"
+ else
+ render :action => "new"
+ 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
+
+end