summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjessib <jessib@leap.se>2013-03-19 15:54:24 -0700
committerAzul <azul@leap.se>2013-07-17 10:46:25 +0200
commita1837914b8f989e2c45fb7b78fc648f0d3f957d6 (patch)
tree0f43e54764acaad1ba8af6265432fe5606e6fbec
parent925534524317d0b6e7786d5a891e2b462b897d0a (diff)
Start to adding customers to braintree vault.
-rw-r--r--billing/app/controllers/customer_controller.rb36
-rw-r--r--billing/app/models/customer.rb38
-rw-r--r--billing/app/views/customer/confirm.html.haml12
-rw-r--r--billing/app/views/customer/edit.html.haml20
-rw-r--r--billing/app/views/customer/new.html.haml24
-rw-r--r--billing/app/views/payments/new.html.haml2
-rw-r--r--billing/config/routes.rb15
7 files changed, 146 insertions, 1 deletions
diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb
new file mode 100644
index 0000000..dcb94eb
--- /dev/null
+++ b/billing/app/controllers/customer_controller.rb
@@ -0,0 +1,36 @@
+class CustomerController < ApplicationController
+ before_filter :authorize
+
+ def new
+ @tr_data = Braintree::TransparentRedirect.
+ create_customer_data(:redirect_url => confirm_customer_url)
+ end
+
+ def edit
+ current_customer = Customer.find_by_user_id(current_user.id)
+ #current_customer.with_braintree_data!
+ #@credit_card = current_customer.default_credit_card
+ @tr_data = Braintree::TransparentRedirect.
+ update_customer_data(:redirect_url => confirm_customer_url,
+ :customer_id => current_customer.braintree_customer_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!
+ 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
+ render :action => "edit"
+ else
+ render :action => "new"
+ end
+ end
+end
diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb
new file mode 100644
index 0000000..611b9d1
--- /dev/null
+++ b/billing/app/models/customer.rb
@@ -0,0 +1,38 @@
+class Customer < CouchRest::Model::Base
+
+ #FIELDS = [:first_name, :last_name, :phone, :website, :company, :fax, :addresses, :credit_cards, :custom_fields]
+
+ use_database "customers"
+ belongs_to :user
+ property :braintree_customer_id
+
+ design do
+ view :by_user_id
+ view :by_braintree_customer_id
+ end
+
+ def has_payment_info?
+ !!braintree_customer_id
+ end
+
+ # from braintree_ruby_examples/rails3_tr_devise and should be tweaked
+ 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
+
+ ##??
+ def default_credit_card
+ return unless has_payment_info?
+
+ credit_cards.find { |cc| cc.default? }
+ end
+
+
+end
diff --git a/billing/app/views/customer/confirm.html.haml b/billing/app/views/customer/confirm.html.haml
new file mode 100644
index 0000000..cfe127e
--- /dev/null
+++ b/billing/app/views/customer/confirm.html.haml
@@ -0,0 +1,12 @@
+%h1 Payment Info Confirmation
+%p Your payment information was successfully saved.
+%dl
+ %dt First Name
+ %dd= @result.customer.first_name
+ %dt Last Name
+ %dd= @result.customer.last_name
+ %dt Phone
+ %dd= @result.customer.phone
+ %dt Credit Card
+ - @result.customer.credit_cards.each do |cc|
+ %dd= cc.masked_number \ No newline at end of file
diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml
new file mode 100644
index 0000000..c203bb5
--- /dev/null
+++ b/billing/app/views/customer/edit.html.haml
@@ -0,0 +1,20 @@
+- if @result
+ #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| |
+ = field_set_tag "Customer" do
+ %dl
+ %dt= f.label :first_name, 'First Name'
+ %dd= f.text_field :first_name
+ %dt= f.label :last_name, 'Last Name'
+ %dd= f.text_field :last_name
+ %dt= f.label :phone, 'Phone'
+ %dd= f.text_field :phone
+ - if @credit_card
+ %ul
+ %li
+ = @credit_card.masked_number
+ (#{link_to @credit_card.token, edit_credit_card_info_path(:id => @credit_card.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/customer/new.html.haml b/billing/app/views/customer/new.html.haml
new file mode 100644
index 0000000..0e1557a
--- /dev/null
+++ b/billing/app/views/customer/new.html.haml
@@ -0,0 +1,24 @@
+- if @result
+ #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|
+ = field_set_tag "Customer" do
+ %dl
+ %dt= f.label :first_name, 'First Name'
+ %dd= f.text_field :first_name
+ %dt= f.label :last_name, 'Last Name'
+ %dd= f.text_field :last_name
+ %dt= f.label :phone, 'Phone'
+ %dd= f.text_field :phone
+ = field_set_tag "Credit Card" do
+ - f.fields_for :credit_card do |cc|
+ %dl
+ %dt= cc.label :number, 'Number'
+ %dd= cc.text_field :number
+ %dt= cc.label :expiration_date, 'Exipration Date (MM/YY)'
+ %dd= cc.text_field :expiration_date
+ %dt= cc.label :cvv, 'CVV'
+ %dd= cc.text_field :cvv
+ = 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/new.html.haml b/billing/app/views/payments/new.html.haml
index 9b06050..b6dd713 100644
--- a/billing/app/views/payments/new.html.haml
+++ b/billing/app/views/payments/new.html.haml
@@ -21,5 +21,5 @@
%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})
+ = hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url,:transaction => {:type => "sale", :amount => @amount, :options => {:submit_for_settlement => true } })
= f.submit "Submit" \ No newline at end of file
diff --git a/billing/config/routes.rb b/billing/config/routes.rb
new file mode 100644
index 0000000..c103e2b
--- /dev/null
+++ b/billing/config/routes.rb
@@ -0,0 +1,15 @@
+Rails.application.routes.draw do
+
+ match 'payments/new' => 'payments#new', :as => :new_payment
+ match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment
+
+ resources :customer, :only => [:new, :edit]
+ resources :credit_card_info, :only => [:edit]
+
+ match 'customer/confirm' => 'customer#confirm', :as => :confirm_customer
+ match 'credit_card_info/confirm' => 'credit_card_info#confirm', :as => :confirm_credit_card_info
+ #match 'transactions/:product_id/new' => 'transactions#new', :as => :new_transaction
+ #match 'transactions/confirm/:product_id' => 'transactions#confirm', :as => :confirm_transaction
+
+
+end