Out of the heart the mouth speaks and the pen writes.

There might be a curiosity, a fascination as to why you see that one person clacking keys on a keyboard or who always brings a journal and utensil with them, no matter where they go. I have found…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Handling PayPal IPN Messages With NodeJS

If you ever developed an app that had any kind of business model you probably needed to support payments from PayPal.

Using PayPal as a platform to accept payments and managing subscriptions is a great solution that as easy as opening PayPal account, creating payment buttons, and adding a few lines of code to your web app. Yup. It’s that easy. 1, 2, 3 and you’re rich!

As a developer, I want to get notify on events such as incoming payments, subscription signups, cancellations, etc, in order to change my users’ plan, status, account balance, and even create invoices. PayPal created a webhook solution called “Instant Payment Notification” (or IPN) which allows you to set a webhook URL for your app, so once a payment event occur — PayPal will send an IPN message to the URL you set with the relevant payload.

The result: This article 🤗

In your main app file, make sure you’re using the body-parser middleware so you’ll end up with an app.js file that looks like that:

Next, since PayPal IPN messages are being sent in POST method, we should add a POST route to our app that will handle these requests:

Note that I created a class called IPNController which has only one static method called index . The router will route the incoming POST requests from PayPal to this method. For now, the only thing this method does is to return a 200 status code and log a line of text, so every time our Node server will get a POST request to the /ipn route, it will return 200.

Important: PayPal require us to return an OK status even before we processed the request. This way they know that the IPN endpoint is valid.

Before continuing, let’s make a quick test to ensure everything works as we expect.

Next, select a transaction type (at the moment it’s not really matter which one), scroll to the bottom of the page, and click the “Send IPN” button.

If everything works, you should get both a success message in the simulator, and the following log in your console application:

Now that we ensured everything works it’s time to move on.

Before processing an IPN message, we must validate that the message did come from PayPal and not from a malicious source.

In order to validate the IPN message with PayPal, there are a few steps we need to produce.

We’ll create a new service called PayPalService . This service will be in charge of validating the incoming IPN messages with PayPal.

As you see, this simple service follows the steps above. Once the service receives response from PayPal it will either resolve the promise if the message is verified by PayPal, or reject it if not.

This is how our IPNController will look like after validating the message with PayPal (using async / await):

Now that we know for sure that the IPN message is valid and sent by PayPal, we may process the message body.

Processing an IPN message could be tricky, and there are multiple transaction types needed to be handled. I’ll try to save you the hassle by mapping the important transaction types.

Finally, after mapping the most important transaction types, our IPNController should look like this:

We have a working NodeJS service that knows how to handle PayPal IPN messages. You may test it again with PayPal’s IPN simulator just to make sure that everything plays nice, and then deploy it.

Once deployed, our final step will be to update the IPN URL in PayPal’s business settings:

And… That’s pretty much it.

Hope you liked this article and found it useful, if you did — go for the claps! 👏🏻

Add a comment

Related posts:

This Popular Diet Made Me Sick

There seems to be at least one new diet every year that promises to be the answer to achieving permanent weight loss, optimum health, and more. As a health coach and trainer, I’ve studied many of…

An introduction about Academic Writing

Writing is the basic element in favor of every one of student’s modish privileged education. It is a process. It starts as well as arrangement your task. It at that moment goes preceding en route for…

How To Get Started In Cryptocurrency Trading

In our previous article, we took you through some general principles on how to make money Bitcoin trading. Similarly, as in the last article, we will highlight a few of them principles again and…