paypal-nodejs
v1.0.2
Published
This is the package that eases your paypal integration in nodejs.
Downloads
2
Readme
paypal-nodejs v1.0.2
Installation
Using npm:-
🖊️ Command 🖊️
npm i -g paypal-nodejs
npm i -save paypal-nodejs
🔥In NodeJS
🔥
PayPal Application Configuration
//Require the package(Load the full build).
const nooriNPM = require('paypal-nodejs');
//Now configure your paypal with Mode + Your paypal application Client_ID + Your paypal Secret_ID from your application.
//String
const Appmode = 'sandbox';
//Client ID from your PayPal Application.
const your_application_client_id = 'PasteYourPaypalClientIDHere';
//Secret ID from your PayPal Application.
const your_application_secret_id = 'PasteYourPaypalSecretIDHere';
//After successfully assigning your all essentials above, now you can proceed for configuration.
nooriNPM.configure(Appmode, your_application_client_id, your_application_secret_id);
//Above method configures the application of our PayPal.
Create PayPal Checkout Payment
//importing npm.
const nooriNPM = require('paypal-nodejs');
//Declare all of your mandatory variables that is getting passed to the 'one_time_payment_setup' module.
let return_url; //*required
let cancel_url; //*required
let payment_name; //*required
let price; //*required
let currency; //*required
let descriptioin; //Optional
//Here we are executing our one time payment checkout that returns the payment url link at the end of the response object.
nooriNPM.one_time_payment_setup(req, res, return_url, cancel_url, payment_name, price, currency, descriptioin);
>req:- request parameter of your API.
>res:- response parameter of your API.
>return_url:- Your success URL where you want to redirect after your payment gets successful.
NOTE:- You can pass extra things for your convenience in the URL which you can use after successful payment. eg, userID.
eg---> `http://localhost:5000/paypal/success/?_id=${userID}`;
>cancel_url:- Redirected to this URL when your payment gets failed for some reason.
>payment_name:- This specifies the payment purpose(String).
eg---> "Buying some product"
>price:- The price that you want to charge your customer/user.
>currency:- Whatever the currency you want your users to pay in.
NOTE:- This is simple string but should be in UPPERCASE.
eg---> 'USD'
>descriptioin:- This is optional(String). Helps for uniquely identifying something if needed.
After successful checkout, You will get 'payment_link' object at the bottom of the response object. That link redirects you to the next step where you put all of your needed details(Your Paypal account id and password) and pay.
Execute Payment
//importing npm.
const nooriNPM = require('paypal-nodejs');
//Your mandatory variable that is getting passed as parameter in the "oneTimePaymentExecution" function.
let PayerID = req.query.PayerID; //*required - coming from url.
let PaymentID = req.query.paymentId; //*required - coming from url.
let price; //*required - Same as checkout (Unacceptable if it varies from Checkout payment's price.)
let currency; //*required - Same as checkout (Unacceptable if it varies from Checkout payment's currency.)
>req:- request parameter of your API
>res:- response parameter of your API.
nooriNPM.oneTimePaymentExecution(req, res, PayerID, PaymentID, price, currency, function (result) {
//This call back function's result holds all of the payment details.
console.log(result);
//Use below sale_id variable that contains the transaction id for the refund if needed.
const sale_id = result.transactions[0].related_resources[0].sale.id;
>sale_id:- You can save this and use it for the refund or to get payment details.
});
Refund Payment
//importing npm.
const nooriNPM = require('paypal-nodejs');
//Your mandatory variable that is getting passed as parameter in the "refundPayment" function.
const amount = req.body.amount; //Should not vary from original price.
const currency = req.body.currency; //Should not vary from original currency.
const sale_id = req.body.sale_id; //The ID that callback returns in the object of result in "oneTimePaymentExecution" function.
>req:- request parameter of your API
>res:- response parameter of your API.
nooriNPM.refundPayment(req, res, amount, currency, sale_id, async function (result) {
//At this point payment should be refunded.
console.log("Refunded successfully");
//result variable gives all the details regarding refund.
console.log(result);
});