Braintree with Nodejs and React
Braintree is one of those methods to get started with payments.
I've been looking for alternatives to Stripe more for comparisons sake to see what would be easiest to implement and more relevant to what I am trying to achieve en masse down the road.
This is a small hello world into getting up and running with Braintree using Express on the serverside and React on the frontend.
If you are following along, you should sign up for a Sandbox account.
This tutorial will use create-react-app and a user contributed npm module to abstract some of the time required for setup.
Another useful link comes from their website for Node.js and JS. Although I will be deterring away from it, it is still a useful reference.
We are going to treat this project as a monorepo for now, so in the root directory we need to create a subdirectory for the frontend and a directory for the server.
Building the Express server
For the sake of doing things right, let's just quickly update our file to ignore
node_modules and the
Here we need to update our file with the sandbox tokens given to us from the Braintree sandbox environment. The keys and configuration can be found on your Braintree dashboard:
Now we can use
dotenv in our app to access the variables and keep them out of our git history.
To set up the main server, update the server.js file to look like so:
server.js file will load the
routes/index.js file, which we will now update.
Let's now update our
routes/index.js to take setup the gateway and setup three routes - one for a simple ping test, another for fetching a client token and a third for making a payment:
Now if we run
node server.js, we should have our app up and running on port 5000!
We can run
curl http://localhost:5000/braintree from another terminal to see our
Braintree route is healthy response.
Now we need to set up the clientside.
Building the React frontend
Updating the app
Go to our App.js file and clean it out. Replace the file with the following:
This code is a variation of the intro code found on the Braintree Web Drop-in React Github intro.
If we run
yarn start we should load up the React app on locahost and you should be able to see the following:
If that is the case, perfect! We are ready to roll.
Making the payment
The following comes directly from the Node.js quickstart for Braintree.
If we insert both the card number and expiry, that should be enough for us to get to the end of the road! After inserting and making the payment, our front end should look like the following:
devtools, we can even inspect the response object we are logging to see our great success!
The server terminal
If we checkout the server terminal, we should be able to see how events went down thanks to Morgan doing our logging:
The image above can help us fully understand the process. When our frontend app loads, we make a
GET request to fetch the token from
/api/braintree/v1/getToken. This token is required for when that final payment request was made. The
OPTIONS 204 request we see there secondly is a CORS preflight request made to ensure we are allowed to make the call and finally and
POST 200 to
/api/braintree/v1/sandbox is our success response after making the payment.
If we now go back to our Sandbox dashboard, we can now see the successful transaction has been recorded!
Very cool. We just went from 0 to payment in a short amount of time.
What's next? Something I like about Braintree is UI extensibility. Although we used a frontend package on this occassion, try building out your own UI for taking payments!
Whether or not you are a fan of Braintree or Stripe really is up to you and your business needs at the end of the day. It is worth looking up both the pros and cons of the developer docs and the rates to decide what is best for your specific usecase (or any other payment platform for that case).
Original post: https://www.dennisokeeffe.com/blog/braintree-node-react
Depth is a series that goes into more detail for projects than it's friendly counterpart series "Hello".
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.