Tag Archives: Braintree

Apprenticeship week 11

This post was originally posted on Trunk Club’s tech blog at http://techblog.trunkclub.com/post/25201990811/trunk-club-apprenticeship-week-11.

Braintree’s CFO and two of their engineers came over on Wednesday for our team lunch to talk about how we use their service and what they have in their product pipeline. It was good to hear about their technology, their stack and their practices. Great company, great guys.

I finished reading Passionate Programmer this week. I really enjoyed it and highly recommend it to anyone who wants to start a career as a developer. It is full of good advice, examples and reminders. It is very pragmatic. I’ll write a blog post soon highlighting my favorite excerpts. Thanks Chad!

I also released the new sales report that I have been working on for the last couple of weeks. With the new features that were added to it, the finance team now has a tool that’s easier for them use to notify the company about the sales performance of every sales person. It’s also a more efficient way for them to do this daily task and will save them a lot of time. I do like that aspect of the job, making other employees’ job easier through the use of technology.

This week I feel like I turned a corner: things are easier and I can tackle larger projects. I’m less often asking “How do you do this?” so much, it’s more often “Is this how you do it?”. It feels good after two months of working pretty hard.

Next week: Building my first Sinatra application.

Installing the Braintree Payment Solution

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