Simple 5 Steps for Your Paypal Payment Integration in Node js

Tasadduq Ali
3 min readOct 2, 2023

--

Before we begin, Your support by clapping and leaving comments greatly influences my work. Additionally, if you’re interested in receiving a complimentary Full Stack Career consultation session every weekend, you can extend your support through PATREON.

Before you begin, ensure you have the following:

Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your system.

PayPal Developer Account: Create a PayPal Developer Account and obtain the necessary credentials (client ID and secret).

Setting Up the Project Initialize a Node.js Project: Open your terminal and navigate to your project directory. Run the following command to initialize a new Node.js project:


npm init

Install PayPal Package: Install the paypal-rest-sdk package using npm:


npm install paypal-rest-sdk

Implementing Payment Integration Import Required Modules: In your main application file (e.g., app.js), import the necessary modules and configurations:

javascript
Copy code
const paypal = require('paypal-rest-sdk');
const config = require('./config'); // Store your PayPal credentials here

Configure PayPal SDK: Configure the PayPal SDK with your client ID and secret:

javascript
Copy code
paypal.configure({
mode: 'sandbox', // 'sandbox' or 'live'
client_id: config.clientId,
client_secret: config.clientSecret
});

Create Payment: To initiate a payment, create a PayPal payment object and send it to PayPal for processing. Here's an example of creating a payment:

javascript
Copy code
app.post('/create-payment', (req, res) => {
const paymentData = {
intent: 'sale',
payer: {
payment_method: 'paypal'
},
redirect_urls: {
return_url: '<http://yourwebsite.com/success>',
cancel_url: '<http://yourwebsite.com/cancel>'
},
transactions: [{
amount: {
total: '10.00',
currency: 'USD'
},
description: 'Sample payment description'
}]
};
paypal.payment.create(paymentData, (error, payment) => {
if (error) {
res.status(500).json({ error: error.message });
} else {
// Redirect the user to the approval URL
const approvalUrl = payment.links.find(link => link.rel === 'approval_url').href;
res.json({ approvalUrl });
}
});
});

Execute Payment: After the user approves the payment on PayPal's website, they will be redirected to the return_url. Use the paymentId from the query parameters to execute the payment:

javascript
Copy code
app.get('/execute-payment', (req, res) => {
const paymentId = req.query.paymentId;
const payerId = req.query.PayerID;
paypal.payment.execute(paymentId, { payer_id: payerId }, (error, payment) => {
if (error) {
res.status(500).json({ error: error.message });
} else {
// Payment executed successfully
res.json({ success: true });
}
});
});

if you have any questions or suggestions just do let me know on my Instagram or at codeculturepro@gmail.com

--

--

Tasadduq Ali

I am MERN Stack developer working in UAE Govt to digitize their massive services. I will help you to become highly skilled Coder 😉