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 | |
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')
-rw-r--r-- | billing/app/controllers/payments_controller.rb | 22 | ||||
-rw-r--r-- | billing/app/helpers/braintree_helper.rb | 50 | ||||
-rw-r--r-- | billing/app/views/payments/confirm.html.erb | 16 | ||||
-rw-r--r-- | billing/app/views/payments/new.html.erb | 38 |
4 files changed, 126 insertions, 0 deletions
diff --git a/billing/app/controllers/payments_controller.rb b/billing/app/controllers/payments_controller.rb new file mode 100644 index 0000000..2a76bb1 --- /dev/null +++ b/billing/app/controllers/payments_controller.rb @@ -0,0 +1,22 @@ +class PaymentsController < ApplicationController + def new + @amount = calculate_amount + end + + def confirm + @result = Braintree::TransparentRedirect.confirm(request.query_string) + if @result.success? + render :action => "confirm" + else + @amount = calculate_amount + render :action => "new" + end + end + + protected + + def calculate_amount + # in a real app this be calculated from a shopping cart, determined by the product, etc. + "100.00" + end +end 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 + diff --git a/billing/app/views/payments/confirm.html.erb b/billing/app/views/payments/confirm.html.erb new file mode 100644 index 0000000..5ab851e --- /dev/null +++ b/billing/app/views/payments/confirm.html.erb @@ -0,0 +1,16 @@ +<h1>Payment Result</h1> + +<div>Thank you for your payment.</div> + +<h2>Transaction Details</h2> + +<table> + <tr><td>Amount</td><td>$<%= @result.transaction.amount %></td></tr> + <tr><td>Transaction ID:</td><td><%= @result.transaction.id %></td></tr> + <tr><td>First Name:</td><td><%= h @result.transaction.customer_details.first_name %></td></tr> + <tr><td>Last Name:</td><td><%= h @result.transaction.customer_details.last_name %></td></tr> + <tr><td>Email:</td><td><%= h @result.transaction.customer_details.email %></td></tr> + <tr><td>Credit Card:</td><td><%= h @result.transaction.credit_card_details.masked_number %></td></tr> + <tr><td>Card Type:</td><td><%= h @result.transaction.credit_card_details.card_type %></td></tr> +</table> + diff --git a/billing/app/views/payments/new.html.erb b/billing/app/views/payments/new.html.erb new file mode 100644 index 0000000..092c518 --- /dev/null +++ b/billing/app/views/payments/new.html.erb @@ -0,0 +1,38 @@ +<h1>Payment: $<%= h @amount %></h1> + +<% if @result -%> + <div style="color: red;"><%= h @result.errors.size %> error(s)</div> +<% end -%> + +<%= form_for :transaction, + :params => @result && @result.params[:transaction], + :errors => @result && @result.errors.for(:transaction), + :builder => BraintreeHelper::BraintreeFormBuilder, + :url => Braintree::TransparentRedirect.url, + :html => {:autocomplete => "off"} do |f| -%> + <%= field_set_tag "Customer" do -%> + <%= f.fields_for :customer do |c| -%> + <div><%= c.label :first_name, "First Name" %></div> + <div><%= c.text_field :first_name %></div> + <div><%= c.label :last_name, "Last Name" %></div> + <div><%= c.text_field :last_name %></div> + <div><%= c.label :email, "Email" %></div> + <div><%= c.text_field :email %></div> + <% end -%> + <% end -%> + <%= field_set_tag "Credit Card" do -%> + <%= f.fields_for :credit_card do |c| -%> + <div><%= c.label :number, "Number" %></div> + <div><%= c.text_field :number %></div> + <div><%= c.label :expiration_date, "Expiration Date (MM/YY)" %></div> + <div><%= c.text_field :expiration_date %></div> + <div><%= c.label :cvv, "CVV" %></div> + <div><%= c.text_field :cvv %></div> + <% end -%> + <% end -%> + <%= hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data( + :redirect_url => confirm_payment_url, + :transaction => {:type => "sale", :amount => @amount} + ) %> + <%= f.submit "Submit" %> +<% end -%> |