How to Integrate PayPal Payment Gateway in Node js


Here are the steps to integrate PayPal payment gateway in Node.js:

  1. Create a PayPal account if you don't already have one.
  2. Create a sandbox account to test the integration.
  3. Install the PayPal SDK in your Node.js project using npm:
npm install paypal-rest-sdk
  1. Require the SDK in your Node.js code:
const paypal = require('paypal-rest-sdk');
  1. Configure the SDK with your PayPal credentials:
paypal.configure({
mode: 'sandbox', // or 'live' for production client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET' });
  1. Create a payment:
const create_payment_json = { "intent": "sale", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": "http://return.url", "cancel_url": "http://cancel.url" }, "transactions": [{ "item_list": { "items": [{ "name": "item", "sku": "item", "price": "1.00", "currency": "USD", "quantity": 1 }] }, "amount": { "currency": "USD", "total": "1.00" }, "description": "This is the payment description." }] }; paypal.payment.create(create_payment_json, function (error, payment) { if (error) { throw error; } else { console.log("Create Payment Response"); console.log(payment); } });
  1. Execute the payment:
const execute_payment_json = { "payer_id": "payer_id", "transactions": [{ "amount": { "currency": "USD", "total": "1.00" } }] }; paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) { if (error) { console.log(error.response); throw error; } else { console.log("Get Payment Response"); console.log(JSON.stringify(payment)); } }); 

This is a basic example to get you started. You may need to modify the code to meet the requirements of your specific use case.