summaryrefslogtreecommitdiff
path: root/engines/billing/app/helpers
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2014-04-17 10:12:05 +0200
committerazul <azul@riseup.net>2014-04-17 10:12:05 +0200
commit3513ad74f950b113af1ba1e3d06bc6a55c48fde5 (patch)
treedb49ebd4428053d5c8d720275b77594a531a1ad1 /engines/billing/app/helpers
parentcb6442c344d6bdaf52c3878b2de2fcf4d85f2648 (diff)
parent3d3688647fab7049e5b531c45b85c1e46a1d528f (diff)
Merge pull request #146 from azul/refactor/engines
Refactor/engines
Diffstat (limited to 'engines/billing/app/helpers')
-rw-r--r--engines/billing/app/helpers/billing_helper.rb51
-rw-r--r--engines/billing/app/helpers/braintree_form_helper.rb64
-rw-r--r--engines/billing/app/helpers/braintree_helper.rb5
3 files changed, 120 insertions, 0 deletions
diff --git a/engines/billing/app/helpers/billing_helper.rb b/engines/billing/app/helpers/billing_helper.rb
new file mode 100644
index 0000000..b9e5e2e
--- /dev/null
+++ b/engines/billing/app/helpers/billing_helper.rb
@@ -0,0 +1,51 @@
+module BillingHelper
+
+ def braintree_form_for(object, options = {}, &block)
+ options.reverse_merge! params: @result && @result.params[object],
+ errors: @result && @result.errors.for(object),
+ builder: BraintreeFormHelper::BraintreeFormBuilder,
+ url: Braintree::TransparentRedirect.url
+
+ form_for object, options, &block
+ end
+
+ def billing_top_link(user)
+ # for admins, top link will show special admin information, which has link to show their own customer information
+ if (admin? and user == current_user)
+ billing_admin_path
+ else
+ show_or_new_customer_link(user)
+ end
+ end
+
+ def show_or_new_customer_link(user)
+ # Link to show if user is admin viewing another user, or user is already a customer.
+ # Otherwise link to create a new customer.
+ if (admin? and (user != current_user)) or ((customer = Customer.find_by_user_id(user.id)) and customer.has_payment_info?)
+ show_customer_path(user)
+ else
+ new_customer_path
+ end
+ end
+
+ # a bit strange to put here, but we don't have a subscription model
+ def user_for_subscription(subscription)
+
+ if (transaction = subscription.transactions.first)
+ # much quicker, but will only work if there is already a transaction associated with subscription (should generally be)
+ braintree_customer_id = transaction.customer_details.id
+ else
+ credit_card = Braintree::CreditCard.find(subscription.payment_method_token)
+ braintree_customer_id = credit_card.customer_id
+ end
+
+ customer = Customer.find_by_braintree_customer_id(braintree_customer_id)
+ user = User.find(customer.user_id)
+
+ end
+
+ def allow_cancel_subscription(subscription)
+ ['Active', 'Pending'].include? subscription.status or (admin? and subscription.status == 'Past Due')
+ end
+
+end
diff --git a/engines/billing/app/helpers/braintree_form_helper.rb b/engines/billing/app/helpers/braintree_form_helper.rb
new file mode 100644
index 0000000..cb322fa
--- /dev/null
+++ b/engines/billing/app/helpers/braintree_form_helper.rb
@@ -0,0 +1,64 @@
+module BraintreeFormHelper
+ class BraintreeFormBuilder < ActionView::Helpers::FormBuilder
+ include ActionView::Helpers::AssetTagHelper
+ include ActionView::Helpers::TagHelper
+
+ def initialize(object_name, object, template, options, proc)
+ super
+ @braintree_params = @options[:params]
+ @braintree_errors = @options[:errors]
+ @braintree_existing = @options[:existing]
+ end
+
+ def fields_for(record_name, *args, &block)
+ options = args.extract_options!
+ options[:builder] = BraintreeFormBuilder
+ options[:params] = @braintree_params && @braintree_params[record_name]
+ options[:errors] = @braintree_errors && @braintree_errors.for(record_name)
+ new_args = args + [options]
+ super record_name, *new_args, &block
+ end
+
+ def text_field(method, options = {})
+ has_errors = @braintree_errors && @braintree_errors.on(method).any?
+ field = super(method, options.merge(:value => determine_value(method)))
+ result = content_tag("div", field, :class => has_errors ? "fieldWithErrors" : "")
+ result.safe_concat validation_errors(method)
+ result
+ end
+
+ protected
+
+ def determine_value(method)
+ if @braintree_params
+ @braintree_params[method]
+ elsif @braintree_existing
+
+ if @braintree_existing.kind_of?(Braintree::CreditCard)
+
+ case method
+ when :number
+ method = :masked_number
+ when :cvv
+ return nil
+ end
+ end
+
+ @braintree_existing.send(method)
+ else
+ nil
+ end
+ end
+
+ def validation_errors(method)
+ if @braintree_errors && @braintree_errors.on(method).any?
+ @braintree_errors.on(method).map do |error|
+ content_tag("div", ERB::Util.h(error.message), {:style => "color: red;"})
+ end.join
+ else
+ ""
+ end
+ end
+ end
+end
+
diff --git a/engines/billing/app/helpers/braintree_helper.rb b/engines/billing/app/helpers/braintree_helper.rb
new file mode 100644
index 0000000..2d18b6c
--- /dev/null
+++ b/engines/billing/app/helpers/braintree_helper.rb
@@ -0,0 +1,5 @@
+module BraintreeHelper
+
+
+end
+