Having done the setup of our web application in the first part of this tutorial we’re going to concentrate on setting up the PayPal SDK in this part of the tutorial.

Part 2 – PayPal SDK

PayPal’s new REST API launched earlier this year and makes it as easy as writing a few lines of code to integrate payments via PayPal accounts and credit cards into any web project. We’re going to setup the 2 needed API calls to create & execute payments using the PayPal Node.js SDK in the following section.


To get going we need to leverage the npm (Node package manager) to install the SDK:

Using the optional --save parameter automatically adds the following entry to the dependency definitions of your package.json file:

It can occur that your version is going to be newer than 0.6.4 – it’s just the current one available right now.


PayPal requires that you obtain API credentials for it’s REST API over at developer.paypal.com.

To do so, create a new application and choose Web as Integration Type. After a few seconds the site is going to provide you a set of production and test REST API credentials and the needed endpoints.

For the sake of convenience we’re going to use the test credentials that allow you to work with our Sandbox test environment.

Setting up the configuration

To allow maintenance and overview we are going to create a file in our project’s root folder called config.json with the following content:

We need to parse this file in app.js and create a new method that initializes the PayPal SDK in routes.js:

Right now app.js should look like this:

As the function init doesn’t exist in routes.js we’re going to define it and additionally add the PayPal SDK plus a variable holding the configuration:

Having made these changes we’re finally fully setup to start our payments integration.

Creating a payment

A payment is a simple JSON object that looks like this for a PayPal payment:

And like this for credit cards:

You’ll see that the main difference between the two is that a PayPal payment doesn’t need any specific funding instruments as the user selects this when confirming the payment after being redirected to PayPal. Therefore two redirect_urls need to be defined: return_url gets called after the user confirmed the payment, cancel_url if the payment gets canceled.

Credit card based payments can be either done by passing all required credit card details (as detailed above) or using tokens that get created by storing the details via the Vault API and requesting tokens afterwards. They don’t need any redirects and provide a seamless experience as the API caller already has all information needed to finalize the payment.

Passing the payment over to PayPal

To create the payment we need to pass the payment’s details to the /payment endpoint using the following function:

In the first part of the tutorial I’ve mentioned that we require session support in this project: Using req.session allows to store session details that we require for later usage. In our case we need to save the payment’s id to execute it later.

To make the payment creation accessible via an API call we’ll extend routes.js and move all code required into a new exported route called create:

Additionally app.js requires the route’s definition (directly below the already existing route for the index):

As mentioned earlier only PayPal payments need to be redirected. All credit card payments are completed after receiving the callback. You can use the payment object being passed to get additional details like the Sale’s ID that can be used to trigger a refund.

When running your app now using node app.js it should create a payment and redirect you to PayPal when navigating over to http://yoururl.com/create – when running local it might be something like http://localhost:5000/create.

Executing PayPal payments

After the user confirmed the payment over at PayPal he’s being redirected back to your service and with some additional information in the GET request. We’re mostly interested in the PayerID that’s being passed in the parameters.

Again we need to create a new route in your application for the /execute endpoint and handle the final part of the payment – the actual execution:

Don’t forget to add the route to your app.js, too:

Canceled PayPal payments

Needless to say PayPal payments can be canceled. When doing so the user is being redirected to the cancel_url you defined in your payment’s redirect_urls. Nothing too exciting happens over here and for the sake of doing something we’ll just show the user a confirmation:

Also we add the following route in our app.js:

Part 3

Our application creates & executes PayPal and credit card payments successfully but does have a very poor user experience as we just send some plain text confirmations and don’t use any UI. In the last part of this tutorial I’m going to focus on Jade and the interaction with our existing code and also go into the deployment of the app to Heroku.

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:

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

  1. systemovich says:

    Thank you very much for the tutorial.

    How many transactions can be sent at a time?

    The payment object’s transactions property is an array containing an object, which suggest that a payment can contain more than one transaction. When I submit a payment with one transaction (paypal as payment method), it works. With two transactions, I get a validation error with the details simply [ [Object] ].

  2. Dean Codemo says:

    You don’t need to use the fs module to read a json file, just use require().

  3. Sean says:

    I get the same thing! How is this overcome!

  4. Pedro Checkos says:

    The project I’m working on is an Express/Ember app that does not use sessions. Is there any way I could retrieve the payment id in the callback method?

  5. disqus_PCRJsFwmus says:

    Does anyone knows why the credit card from a sandbox account doesnt work for testing?

  6. disqus_PCRJsFwmus says:

    I need some credit card with a valid CSC for testing

  7. Kingz says:

    I got this issue as well as ember does not accept query parameters officially. What i did is to create an endpoint on the server for the callback url and use the server to parse the query and then redirect the browser to a valid ember route

  8. Kingz says:

    Hi i tried everything as stated here but i get redirected to paypal page that has error message “Dispatch Error, please check logfile for more information”. Note that i am using sandbox for testing. Is there anything that is missing in this tutorial?

  9. Kingz says:

    NVM, just figured out that it’s because I’m already logged with with a real paypal account. I used another browser and it works.

  10. Simon says:

    Great tutorial, thanks!!! BUT for those who wish to use the NOSHIPPING option from the classic API, this is currently not possible with the REST API. See: https://github.com/paypal/rest-api-sdk-nodejs/issues/6

  11. tuanquynet says:

    It’s super useful.

  12. mattwarren says:

    Can this be expanded to include the webhooks handling so that the express app can be notified if payment is refunded in the future?