Implementing a Braintree Payment API Integration with Python and Django

I was overwhelmed with the reception of my last python post and the many conversations it initiated, mainly in Twitter. The main thing that I think was more interesting for people reading my blog is the Proof of Concept that Python is good for MVP, Production and even more.

In this post I am going to discuss another python use case inspired once again from a real life project that I am currently working. In my previous post I discussed about Flask, while now I give emphasis to Django.

A BattleHack

This story is a bit different from other ones. It starts by joining the BattleHack organised in Athens by BrainTree. There, apart from the nice food, the awesome games and the super cool mentors, there were also nice tutorials and a lot of Hack! This is something that I particularly enjoyed as well as anyone attending BattleHack in general.

Bellow you can see some of our awesome moments!

For me as a developer and evangelist, the tutorials were the most interesting part of the overall event.

The most important tools for integrating payments with BrainTree into my Django app that I was developing in the hack are listed below:

For the hack we used the last one, and within a few minutes we had a functional payment system within our application, even though none of us has ever done something similar before.

Just to conclude this experience, we finally couldn’t perform well on the stage because we had so much to eat!!!

Nevertheless, it was super fun!

Payment API Integration
Minutes before the Hack finishes, Myself (on the left) and George (on the right)

Buying your Music at Orfium

I will be talking about Orfium in a future post, but now I want to reference it here, as the overall result from the Battlehack experience was actually the know-how that lead us to implement the payment functionality that anyone can interact with, at the Payment Tab.

Screen Shot 2015-11-11 at 16.09.05 Payment API Integration
Paying through BrainTree on

A Python Sample of Code doing Some Payment Stuff

[code language=”python”]

def index():

# Generate client token for the dropin ui
client_token = braintree.ClientToken.generate({})

return render_template(‘index.html’, token=client_token)

@app.route("/proc", methods=[‘GET’, ‘POST’])
def proc():

result ={
"amount": request.form["amount"],
"payment_method_nonce": request.form["payment_method_nonce"]

return render_template(‘proc.html’, result=result, request=request.form)

APIs Can be Production Ready from Day 1

This is a proof that even the simplest integration can be secure and can be production ready if you are implementing on top of a good API.

BrainTree has managed to do a great job on their API. It is super easy to integrate their various features, including payments with paypal, credit card, fraud detection, subscriptions and more.

They also have a sandbox which is a practice that anyone providing an API as the core product should actually think of. The steps to going in production are super easy:

  1. Receive your Sandbox API Keys
  2. Make sure that everything is working with any of the listed above libraries
  3. Receive your Production API Keys
  4. Replace the Sandbox keys with the Production ones
  5. Receive real money!

APIs are here to make our life simpler and help us create products collaboratively with others.

Braintree is such a good example of an API. Hack it!