summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclaucece <soficeli0@gmail.com>2015-09-18 17:33:05 -0500
committerclaucece <soficeli0@gmail.com>2015-10-05 22:39:30 -0500
commitf55fd33542c68fc8fc4519d622a41ef9517ebee3 (patch)
treece6b48fa4d6d01476fc5cc3ff42e33733ce26059
parente4a012e09a6b16438bd1286b2057aed916a106f7 (diff)
added customers, recurring payment and payment_info
m---------app/assets/javascripts/srp0
-rw-r--r--app/models/anonymous_user.rb5
-rw-r--r--app/models/user.rb10
-rw-r--r--engines/billing/app/controllers/payments_controller.rb58
-rw-r--r--engines/billing/app/views/payments/new.html.erb6
5 files changed, 57 insertions, 22 deletions
diff --git a/app/assets/javascripts/srp b/app/assets/javascripts/srp
-Subproject 9e1a41733468d4a3f5102b04277b9cd7b52d0a4
+Subproject 8f33d32d40b1e21ae7fb9a92c78a275422af421
diff --git a/app/models/anonymous_user.rb b/app/models/anonymous_user.rb
index 0c1f540..73e95e5 100644
--- a/app/models/anonymous_user.rb
+++ b/app/models/anonymous_user.rb
@@ -12,6 +12,10 @@ class AnonymousUser < Object
def id
nil
end
+
+ def has_payment_info?
+ false
+ end
def email
nil
@@ -32,4 +36,5 @@ class AnonymousUser < Object
def is_anonymous?
true
end
+
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 52eabf2..391b2a5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -9,6 +9,7 @@ class User < CouchRest::Model::Base
property :contact_email, String, :accessible => true
property :contact_email_key, String, :accessible => true
property :invite_code, String, :accessible => true
+ property :braintree_customer_id, Integer, :accessible => true
property :enabled, TrueClass, :default => true
# these will be null by default but we shouldn't ever pull them directly, but only via the methods that will return the full ServiceLevel
@@ -176,10 +177,11 @@ class User < CouchRest::Model::Base
@message.save if @message
end
-
- # def has_payment_info?
- # braintree_customer_id
- # end
+
+ def has_payment_info?
+ braintree_customer_id
+ end
+
protected
##
diff --git a/engines/billing/app/controllers/payments_controller.rb b/engines/billing/app/controllers/payments_controller.rb
index 7c2b2d3..6ea149f 100644
--- a/engines/billing/app/controllers/payments_controller.rb
+++ b/engines/billing/app/controllers/payments_controller.rb
@@ -2,19 +2,10 @@ class PaymentsController < BillingBaseController
before_filter :require_login, :only => [:index]
def new
- @client_token = Braintree::ClientToken.generate
- end
-
- def confirm
- @result = Braintree::Transaction.sale(
- amount: params[:amount],
- payment_method_nonce: params[:payment_method_nonce],
- )
- if @result.success? == true
- redirect_to action: :new, notice: "Congraulations! Your transaction has been successfully!"
+ if current_user.has_payment_info?
+ @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
else
- flash[:alert] = "Something went wrong while processing your donation. Please try again!"
- redirect_to action: :new
+ @client_token = Braintree::ClientToken.generate
end
end
@@ -26,13 +17,46 @@ class PaymentsController < BillingBaseController
@transactions = braintree_data.transactions
end
- protected
+ def confirm
+ make_transaction
+ if @result.success?
+ flash[:success] = "Congratulations! Your transaction has been successfully!"
+ else
+ flash[:error] = "Something went wrong while processing your donation. Please try again!"
+ end
+ redirect_to action: :new, locale: params[:locale]
+ end
-
- def fetch_transparent_redirect
- @tr_data = Braintree::TransparentRedirect.transaction_data redirect_url: confirm_payment_url,
- transaction: { type: "sale", options: {submit_for_settlement: true } }
+ private
+ def make_transaction
+ unless current_user.has_payment_info?
+ transact_with_user_info
+ else
+ transact_without_user_info
+ end
end
+ def transact_with_user_info
+ @result = Braintree::Transaction.sale(
+ amount: params[:amount],
+ payment_method_nonce: params[:payment_method_nonce],
+ customer: {
+ first_name: params[:first_name],
+ last_name: params[:last_name],
+ company: params[:company],
+ email: current_user.email,
+ phone: params[:phone]
+ },
+ options: {
+ store_in_vault: true
+ })
+ current_user.update_attributes(braintree_customer_id: @result.transaction.customer_details.id) if @result.success?
+ end
+ def transact_without_user_info
+ @result = Braintree::Transaction.sale(
+ amount: params[:amount],
+ payment_method_nonce: params[:payment_method_nonce],
+ )
+ end
end
diff --git a/engines/billing/app/views/payments/new.html.erb b/engines/billing/app/views/payments/new.html.erb
index 5eb3e35..03fc5b9 100644
--- a/engines/billing/app/views/payments/new.html.erb
+++ b/engines/billing/app/views/payments/new.html.erb
@@ -1,9 +1,13 @@
<h2 class="mbs">New Donation</h2>
-<p>Please enter your donation details (this is a donation and will not be applied towards your account):</p>
<br>
<%= form_tag confirm_payment_path, id: "checkout-form" do %>
<%# add migration to user first. Not sure about the anonymous part. %>
<%# render 'customer_form' unless current_user.has_payment_info? && current_user.where.not.is_anonymous? %>
+ <% if current_user and !current_user.has_payment_info? %>
+ <%= render 'customer_form' unless @anonymous_user%>
+ <% end %>
+ <br>
+ <p>Please enter your donation details (this is a donation and will not be applied towards your account):</p>
<div id="payment-form"></div>
<div id='coinbase-container-id'></div>
<input type="text" name="amount" placeholder="Enter amount">