diff options
author | jessib <jessib@leap.se> | 2013-03-12 13:32:46 -0700 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-07-17 10:46:25 +0200 |
commit | 3ffac4e77a118ee6ec7d369b0250c11830c2f7e0 (patch) | |
tree | 10bcae72b62b63cd8da65e93fb0c90f2c107fc3b /billing/app/helpers/braintree_helper.rb | |
parent | fe7a5ac0776e1bd7baf36382600e265def901e51 (diff) |
Basic functionality for one time transactions using Braintree's transparent redirect API based from https://github.com/braintree/braintree_ruby_examples/tree/master/rails3_tr_checkout (very little changes were required)
Diffstat (limited to 'billing/app/helpers/braintree_helper.rb')
-rw-r--r-- | billing/app/helpers/braintree_helper.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/billing/app/helpers/braintree_helper.rb b/billing/app/helpers/braintree_helper.rb new file mode 100644 index 0000000..3d6e887 --- /dev/null +++ b/billing/app/helpers/braintree_helper.rb @@ -0,0 +1,50 @@ +module BraintreeHelper + 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] + 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] + 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 + |