Integrate Paypal In NodeJS
Posted By : Prabjot Singh | 07-Apr-2015
I will show you how to integrate paypal in NodeJS Web Application. So for this, I am going to use restful web services of paypal using “paypal-rest-sdk” module. So first step,you have to install “paypal-rest-sdk” module in your web application.
Install SDK
You need to use npm(Node Package Manager) to install SDK
npm install paypal-rest-sdk –save
using –save will automatically add module to your package.json file ,you can see in pacakge.json,it will looks like
"paypal-rest-sdk": "^1.5.2"
Setup Credentials
Paypal provides credentials for it's Rest-API over “Paypal link”,there are some steps,that you have to follow to get credentials
-
Sign in with your paypal account(create account if you haven't it already)
-
Go on dashboard,click on my apps
-
Create app,after it,paypal will give you a client_id,client_secret,please note down it,these are your credentials,don't share it with others
Require Module in app.js?
you need to import module in app.js,so it will be looks like
var paypal = require('paypal-rest-sdk');
Configure your credentials
You need to configure credentials, make a simple json of it and then configure it with paypal. A json will be like
var paypalCredentials = {
"host" : "api.sandbox.paypal.com",
"port" : "",
"client_id" : "XXXXXXXXXXXXXXXXXXXXX",
"client_secret" : "XXXXXXXXXXXXXXXXXXX"
};
paypal.configure(paypalCredentials);
Creating a payment with Paypal
You have to require a json for your transactions details,so make json ,fill up your details for passing over to paypal
var payment = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://yoururl.com/execute",
"cancel_url": "http://yoururl.com/cancel"
},
"transactions": [{
"amount": {
"total": "5.00",
"currency": "USD"
},
"description": "My awesome payment"
}]
};
There are some fiels which you have to focus
-
return_url – this is the url,when after creating successful payment,paypal redirect user to your app,these url also you have to provide in your app which you have created in paypal
-
cancel_url- this one will be use if transaction canceled
This json is valid if you are doing transaction using paypal account,but if you are using credit card ,then json will be little bit different from it. A json for credit card will be
var creaditCardPayment = {
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [{
"credit_card": {
"number": "5500005555555559",
"type": "mastercard",
"expire_month": 12,
"expire_year": 2018,
"cvv2": 111,
"first_name": "Joe",
"last_name": "Shopper"
}
}]
},
"transactions": [{
"amount": {
"total": "5.00",
"currency": "USD"
},
"description": "My awesome payment"
}]
};
There are major difference between these json is that you have not need to provide redirect urls,if you are using transaction payment
Passing Payment over to Paypal
After successful configuring your credentials and transactions details json,now need to pass payment over to paypal. We can pass payment using paypal create method. There are some difference in creation between using paypal method and credit card method.
Using paypal method
paypal.payment.create(payment, function (error, payment) {
if (error) {
console.log(error);
} else {
if(payment.payer.payment_method === 'paypal') {
req.session.paymentId = payment.id;
var redirectUrl;
for(var i=0; i < payment.links.length; i++) {
var link = payment.links[i];
if (link.method === 'REDIRECT') {
redirectUrl = link.href;
}
}
res.redirect(redirectUrl);
}
}
});
In case of paypal method,paypal returns approved url and execution url. We need to redirect user to approval url for approving of payment details,that is why we are redirecting user. But in case of credit card, paypal does not return any approval url. So no need to redirect user to any url as well as method of execute payment in case of credit card.
Using Credit Card
paypal.payment.create(payment, function (error, payment) {
if (error) {
console.log(error);
} else {
...
}
});
This was a final step for those are using credit card payment.
Execute Payment
After approving payment by user,now need to execute payment when user redirect to your application. This step is not for who are using credit card payment,this is only for ,who are using paypal method
exports.execute = function(req, res){
var paymentId = req.session.paymentId;
var payerId = req.param('PayerID');
var details = { "payer_id": payerId };
paypal.payment.execute(paymentId, details, function (error, payment) {
if (error) {
console.log(error);
} else {
res.send("Hell yeah!");
}
});
};
Cancel payment
in case of cancel payment,create you own method and your customize message to user.
exports.cancel = function(req, res){
res.send("The payment got canceled");
};
THANKS
Prabjot Singh
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prabjot Singh
Prabjot is a Java and Grails developer.He has a good hands on neo4J, AngularJS. He likes to work on new technologies