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
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
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
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
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
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
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.
- How WooCommerce and Developer Agencies Optimize Payments for Small Businesses with PayPal
- New REST API Feature: Setting a Receiver for Payments
- PayPal is Now Available Through WooCommerce 2.6 Onboarding Wizard
- Adaptive Payments is Moving to Limited Release – What you Need to Know
Connect with us!
- March 2017
- January 2017
- December 2016
- October 2016
- September 2016
- July 2016
- May 2016
- March 2016
- November 2015
- September 2015
- August 2015
- June 2015
- April 2015
- March 2015
- November 2014
- October 2014
- August 2014
- July 2014
- March 2014
- February 2014
- January 2014
- December 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013