Tagged with

This week at DailyDrip we saw how to work with payments via Braintree in Rails.

Here is our list of videos on the subject:

I will do a brief overview on using Braintree on Rails in this post. To get more implementation details, check out each episode.

How can Braintree help you?

I want to sell something online. Should I use Braintree ?

Yes. Braintree helps you do exactly that.

I don't have plans or recurring payments. Is Braintree still an option?

Yes. You have the option to only make simple transactions via Braintree.

I would like to set up Paypal on my website. Can Braintree help me with that?

Paypal’s developer integration sucks. Their test environments don’t function properly, and you constantly get stuck in redirect loops when setting up your accounts. The best part about the PayPal acquisition of Braintree is now you get access to PayPal users through Braintree’s APIs and test environments. This means you can write a modern site and not have to deal with the 10+ year old systems of PayPal.

Hopefully, you got some answers from the questions you have. Let's see how we can integrate it with our app.

Getting Started

The first thing you should do is create an account on the Braintree Website. Pay attention, they have two separate concepts: sandbox and production. You can do all your tests in sandbox. It's really useful and you can see the money coming in!

To use Braintree in your application, just set up the gem braintree, and get your keys from Braintree. Put those in an initializer.

Braintree::Configuration.environment = ENV['BT_ENVIRONMENT']
Braintree::Configuration.merchant_id = ENV['BT_MERCHANT_ID']
Braintree::Configuration.public_key = ENV['BT_PUBLIC_KEY']
Braintree::Configuration.private_key = ENV['BT_PRIVATE_KEY']

Also, you should have a token for your transactions generated by your server. If you're creating payment methods, you should pass your customer id on it.

@client_token = Braintree::ClientToken.generate

Creating your first transaction

There are two ways you can do that: Using the DropIn UI interface Using a custom Form

DropIn Payment UI can be implemented very easily. Although you can't customize it.

Basically it provides a form, and the user can put a card number and expiration date. It's also possible to use PayPal with the DropIn UI. The action of this form goes to your controller with a payment_method_nonce string, and you can use this as an authorization to create a transaction. You implement this DropInUI using the Javascript Braintree API. Just using something like:

braintree.setup('CLIENT-TOKEN-FROM-SERVER', 'dropin', {
  container: 'dropin-container'
});

DropIn UI is really easy, but your probably want something styled to fit in with your website de sign. Braintree also allows us to customize our form.

<form id="checkout" action="/your/server/endpoint" method="post">
  <input data-braintree-name="number" value="4111111111111111">
  <input data-braintree-name="expiration_date" value="10/20">
  <input type="submit" id="submit" value="Pay">
</form>

In this case, your form should have some data elements in the inputs for card number and expiration date. Remember we are using it with the Braintree Javascript SDK. Once we submit the form it generates a token for us, and we can create the Payment Method. To use the card, you should generate a token based on the customer id. So, if you didn't create the customer on Braintree, you should. Check out more details about custom forms on Braintree in our episode.

Updating my system with Braintree

Imagine you want to keep all the transactions and subscriptions in your system. You might want to run some numbers and see how profitable you are. You probably don't want to go to Braintree every time. Braintree provides webhooks. So, e.g., every time a transaction is canceled via Braintree, you can update your website and vice-versa. Check out more details on the Braintree Webhooks episode.

Subscriptions / Recurring Payments

Imagine you have a subscription system in your app, as we have here at DailyDrip. Currently, we have monthly plans and annual plans. Sometimes, we have promocodes. To handle all of that can be really painful with a lot of edge cases. Especially if you have trial periods in X, Y or Z days.

Braintree does all this work for us. We just need to set up our plans.

To set up your plan, you specify the billing cycle and amount. It also has some features to work with trial periods. This is very handy!

Check out our episode on implementing recurring payments using Braintree.

Is it worth using?

We at DailyDrip enjoyed using Braintree, and we think it's good in terms of implementation, features, and documentation. Give it a try and see if it suits your needs.

Resources


33b32be65cd662922a71dc1d051ddb9f?s=184&d=mm

Franzé Jr Software Engineer with experience working in multi-cultural teams, Franze wants to help people when he can, and he is passionate about programming and Computer Science. Founder of RemoteMeetup.com where he can meet people all over the World. When Franze is not coding, he is studying something about programming.