I wanted to learn how to use the Braintree gem and payment solution so we installed it today on @daveande‘s project, SendFive. This post will walk you through the steps that we took to get it working. Full disclosure: the app was already set up to use Stripe, and we didn’t use TDD. Here’s the code for the app: https://github.com/jvbahnik/send_five.
First step is to sign up to get access to Braintree’s sandbox at http://www.braintreepayments.com/gateway. I was granted access within a couple of hours on a late Friday afternoon, very quick turnaround.
Next, we added the Braintree gem to our gemfile and ran bundle install.
Once that was done, we created a file config/initializer/braintree.rb in our project. We set merchant_id, public_key and private_key as environment variables and pasted this code in our file:
Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = ENV['braintree_MERCHANT_ID']
Braintree::Configuration.public_key = ENV['braintree_PUBLIC_KEY']
Braintree::Configuration.private_key = ENV['braintree_PRIVATE_KEY']
I say pasted because Braintree has this neat little tool in the sandbox dashboard where you select your language of choice, hit copy and your variables are ready to be pasted.

We removed the Stripe code from our controller. Eventually we wouldn’t be using that method anymore and instead point the redirect to a new method called confirm, but more on that later. We actually didn’t have to add any code to the method that included the code for Stripe. Instead of having form action point to the method in the gifts controller, we pointed the form action to Braintree. That also meant reworking our methods since the old method included sending the SMS and an email confirmation. But all we need to do to fix that was move whatever we wanted to keep to the new confirm method.
Once that was done, we changed the form action to:
<%= form_tag Braintree::TransparentRedirect.url do %>
We modified our payment form to include the following:
<%= hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data(
:redirect_url => confirm_url,
:transaction => {
:type => "sale",
:amount => "5.99",
:options => {:submit_for_settlement => true}
}
) %>
We also changed the name of our credit card and expiration date fields:
<%= text_field_tag :card_number, nil, :name => "transaction[credit_card][number]", :placeholder => "Credit Card Number", :maxlength => 16, :size => 20 %>
<%= text_field_tag :card_expiration, nil, :name => "transaction[credit_card][expiration_date]", :placeholder => "MM/YY", :maxlength => 5, :size => 5 %>
Our amount is always $5.99 so for now we hard-coded it. For more on submit_for_settlement check out the Braintree documentation. For :redirect_url => confirm_url to work we added a new method in our controller:
def confirm
@result = Braintree::TransparentRedirect.confirm(request.query_string)
if @result.success?
redirect_to new_user_url
else
redirect_to root_url
end
end
It’ll check the query string from the URL on the Redirect and look for success. We also needed a new route for our confirm method:
match '/confirm' => 'gifts#confirm', :as => :confirm
And that’s it! To test it, we used the Braintree sandbox reference guide for credit card numbers and CCVs. You can test various credit card types and simulate a lot of different errors as well.
This article still needs work but I was so excited about how easy and quick it was to get Braintree on the site that I wanted to share it quickly. I’ll update it soon, I promise.
Let me know what you think in the comments below!
Update 1/17/12: great related blog post that include testing using Cucumber at http://enlightsolutions.com/articles/integration-testing-braintrees-transparent-redirect-with-rails-and-cucumber