Node.js is an extremly popular and efficient server environment that we at PayPal fully embrace in several projects. Being written in JavaScript and using the power of the V8 engine it easily outperforms lots of other environments and is definitely worth having a look at.

This is going to be a tutorial which consists of three parts: Initial setup, using the PayPal SDK for Node.js and UI plus deployment to Heroku. After following all three steps you should be able to create an experience similar to this.

Part 1 – Setup

For the first part of the tutorial I’ll be going into the initial setup & required packages that we’re going to use to build our Node.js web app.

Packages

Our application is going to leverage the Express framework which basically is a Node.js alternative to the popular Ruby framework Sinatra. Looking into most popular Node projects, there is nearly not a single one server-side application running that doesn’t also leverage the capabilities that Express offers – it can be considered the de facto standard for Node.js development.

In this tutorial I assume that you’ve got the node & npm executables setup already. If you didn’t do so already: Have a look at this well-written tutorial or use package managers like brew!

Installing Express

To install Express globally use the following command: npm install -g express.

Setting up the project

After this has been sucessfully installed we’re going to create a new folder & use Express to setup a “blank” Node.js project:


The two files we’re going to concentrate on in this tutorial are app.js and package.json.

app.js is basicaly the skeleton of your application and is responsible for different things like setting up the right requirements (like Express or the PayPal SDK) and your application’s routes.

package.json contains all requirements and is important for the npm install command which automatically gets executed when deploying a Node.js app to services like Heroku. After creation it should look something like this:

You’ll see that next to Express another dependency got defined already. Jade is an amazing template engine for Node.js that allows to write very minimal HTML. The asterisk defines that any version of Jade will do (which usually either uses an existant global version or installs the newest stable version available).

Having a look at app.js you’ll see that there are two lines that we can get rid off:

Also delete /routes/user.js as we are not going to need it for this project.

The reason for getting rid of these two lines and user.js is that they are an plainly an auto generated example for creating an API endpoint that returns a resource (in this case your application’s registered users).

In this tutorial most of the payments implementation is going to happen in routes/index.js. This is the place that allows you to define the functions that get executed when your application receives HTTP requests.

Our app.js file should look now like this:

Having executed with the -s parameter adds session support to your project which we’re going to need later to store some information during the checkout process. The two lines adding support for this are:

Feel free to change the your secret here to anything else that suits your needs.

You can run the project by executing node app.js in your project’s root folder and navigating over to http://localhost:3000.

Part 2

Having finished all required setup we’re going to concentrate on the PayPal SDK integration in the next part of this tutorial. If you want to have a look already: Feel free to go over to pprestnode.herokuapp.com or the GitHub repository!

About Tim Messerschmidt

As a long time mobile and web developer specializing in Android, Tim channels his knowledge and experience as Developer Evangelist across Europe for PayPal’s Developer Network. Tim is very passionate about startups, and serves as a mentor of Seedcamp, Wayra, Startup Wise Guys & Rockstart. In his spare time, he leads and creates training classes in all sorts of Android related topics, and is the author of the Android chapter of the Mobile Developer's Guide to the Galaxy, as well as numerous articles published in magazines like web&mobile developer.

Tagged with:
 

2 Responses to Building a web app with Node.js & the PayPal SDK

  1. kwippe says:

    Hello Tim, Thanks very much for this, and the repo on GitHub w/ your Paypal restful SDK sample for Node.js. The sample looks great, and works perfectly (I did convert it to Dust.js, and would be happy to share those files 🙂 – but I’m a bit confused as now the app is supposed to get the user’s paypal authorization. I am using Passport auth in my main app – so I can add the Paypal auth strategy, but is there any way you could give a sample of exactly how I could integrate that into the Paypal payment in your app? (not the credit card, only the Paypal). Basically I want a really simple ‘upgrade’ button, that will then ask the user to login to Paypal, and make the payment. At first I was trying to do go the IPN route but so far the Node.js modules I’ve tried for ipn have not been successful.

  2. Insane IT Skills says:

    Check this Free Node.js Training that gives you all the keys to a good start for a complete masteration of node.js by taking all the power of it.

    This course is regularly updated.

    https://www.udemy.com/nodejs-tutorial-from-scratch-by-examples