Introduction
In This Article, We Will Learn How To Use Paypal As Our Payment Gateway Using Node.js.
To start this first you need to install these dependencies inside your node project which are listed below
npm i express
npm i nodemon
npm i paypal-rest-sdk
Install these dependencies inside your node.js project and create your index.js
file for your project ad copy paste the below code to it
const express = require('express'); const paypal = require('paypal-rest-sdk'); paypal.configure({ 'mode': 'sandbox', //sandbox or live 'client_id': '####yourclientid######', 'client_secret': '####yourclientsecret#####' }); const app = express(); app.get('/', (req, res) => res.sendFile(__dirname + "/index.html")); app.listen(PORT, () => console.log(`Server Started on ${PORT}`));
Here in this block of code, we need to replace the client-id
and the client-secret
of the PayPal like this
Replace client-id and client-secret from your PayPal dashboard inside the application. But in order to get this first, you need to create a business account on PayPal. We can simply create fake sandbox accounts on PayPal
Go to the Paypal Developer website and create a sandbox account like this
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>PayPal Node App</title> </head> <body> <h1>Coding Shiksha App Store</h1> <h2>Buy For $25</h2> <form action="/pay" method="post"> <input type="submit" value="Buy"> </form> </body> </html>
So now, we have a buy button here inside the app. When we click on this button a post request will be made to the route /pay so we need to make this route like this
app.post('/pay', (req, res) => { const create_payment_json = { "intent": "sale", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": "http://localhost:3000/success", "cancel_url": "http://localhost:3000/cancel" }, "transactions": [{ "item_list": { "items": [{ "name": "Red Sox Hat", "sku": "001", "price": "25.00", "currency": "USD", "quantity": 1 }] }, "amount": { "currency": "USD", "total": "25.00" }, "description": "Hat for the best team ever" }] }; paypal.payment.create(create_payment_json, function (error, payment) { if (error) { throw error; } else { for(let i = 0;i < payment.links.length;i++){ if(payment.links[i].rel === 'approval_url'){ res.redirect(payment.links[i].href); } } } }); });
Basically here in this block of code, we have made the JSON object which contains all the properties which are required for the transaction so now we need to make the routes for the success callback and also the cancel callback events like this
app.get('/success', (req, res) => { const payerId = req.query.PayerID; const paymentId = req.query.paymentId; const execute_payment_json = { "payer_id": payerId, "transactions": [{ "amount": { "currency": "USD", "total": "25.00" } }] }; paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) { if (error) { console.log(error.response); throw error; } else { console.log(JSON.stringify(payment)); res.send('Success'); } }); });
app.get('/cancel', (req, res) => res.send('Cancelled'));
If you have any questions or face any problems regarding this article, please let me know in the comments.
For new blogs check. here.
Thank You.
The post PAYPAL IMPLEMENTATION IN NODE.JS appeared first on The Code Hubs.