expressCart/routes/payments/paypal.js

212 lines
7.8 KiB
JavaScript
Raw Normal View History

2019-07-12 18:06:34 +10:00
const express = require('express');
const common = require('../../lib/common');
2019-06-15 15:58:19 +10:00
const { indexOrders } = require('../../lib/indexing');
2019-07-12 18:06:34 +10:00
const paypal = require('paypal-rest-sdk');
const router = express.Router();
2018-01-07 04:55:48 +10:00
router.get('/checkout_cancel', (req, res, next) => {
// return to checkout for adjustment or repayment
res.redirect('/checkout');
});
router.get('/checkout_return', (req, res, next) => {
2019-07-12 18:06:34 +10:00
const db = req.app.db;
const config = req.app.config;
const paymentId = req.session.paymentId;
2019-11-03 09:18:34 +10:00
const payerId = req.query.PayerID;
2018-01-07 04:55:48 +10:00
2019-07-12 18:06:34 +10:00
const details = { payer_id: payerId };
2018-01-07 04:55:48 +10:00
paypal.payment.execute(paymentId, details, (error, payment) => {
let paymentApproved = false;
let paymentMessage = '';
let paymentDetails = '';
if(error){
paymentApproved = false;
if(error.response.name === 'PAYMENT_ALREADY_DONE'){
paymentApproved = false;
paymentMessage = error.response.message;
}else{
paymentApproved = false;
paymentDetails = error.response.error_description;
}
// set the error
req.session.messageType = 'danger';
req.session.message = error.response.error_description;
req.session.paymentApproved = paymentApproved;
req.session.paymentDetails = paymentDetails;
res.redirect('/payment/' + req.session.orderId);
return;
}
2019-07-12 18:06:34 +10:00
const paymentOrderId = req.session.orderId;
2018-01-07 04:55:48 +10:00
let paymentStatus = 'Approved';
// fully approved
if(payment.state === 'approved'){
paymentApproved = true;
paymentStatus = 'Paid';
paymentMessage = 'Your payment was successfully completed';
paymentDetails = '<p><strong>Order ID: </strong>' + paymentOrderId + '</p><p><strong>Transaction ID: </strong>' + payment.id + '</p>';
// clear the cart
if(req.session.cart){
2020-01-02 15:23:39 +10:00
common.emptyCart(req, res, 'function');
2018-01-07 04:55:48 +10:00
}
}
// failed
if(payment.failureReason){
paymentApproved = false;
paymentMessage = 'Your payment failed - ' + payment.failureReason;
paymentStatus = 'Declined';
}
// update the order status
2019-10-29 18:26:30 +10:00
db.orders.updateOne({ _id: common.getId(paymentOrderId) }, { $set: { orderStatus: paymentStatus } }, { multi: false }, (err, numReplaced) => {
2018-01-07 04:55:48 +10:00
if(err){
console.info(err.stack);
}
2019-06-15 14:46:08 +10:00
db.orders.findOne({ _id: common.getId(paymentOrderId) }, (err, order) => {
2018-01-07 04:55:48 +10:00
if(err){
console.info(err.stack);
}
// add to lunr index
2019-06-15 15:56:51 +10:00
indexOrders(req.app)
2018-01-07 22:14:17 +10:00
.then(() => {
// set the results
req.session.messageType = 'success';
req.session.message = paymentMessage;
req.session.paymentEmailAddr = order.orderEmail;
req.session.paymentApproved = paymentApproved;
req.session.paymentDetails = paymentDetails;
2019-07-12 18:06:34 +10:00
const paymentResults = {
2018-01-07 22:14:17 +10:00
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
paymentApproved: req.session.paymentApproved,
paymentDetails: req.session.paymentDetails
};
// send the email with the response
2018-01-22 07:20:46 +10:00
// TODO: Should fix this to properly handle result
2018-01-07 22:14:17 +10:00
common.sendEmail(req.session.paymentEmailAddr, 'Your payment with ' + config.cartTitle, common.getEmailTemplate(paymentResults));
res.redirect('/payment/' + order._id);
});
2018-01-07 04:55:48 +10:00
});
});
});
});
// The homepage of the site
router.post('/checkout_action', (req, res, next) => {
2019-07-12 18:06:34 +10:00
const db = req.app.db;
const config = req.app.config;
const paypalConfig = common.getPaymentConfig();
2018-01-07 04:55:48 +10:00
// setup the payment object
2019-07-12 18:06:34 +10:00
const payment = {
intent: 'sale',
payer: {
payment_method: 'paypal'
2018-01-07 04:55:48 +10:00
},
2019-07-12 18:06:34 +10:00
redirect_urls: {
return_url: config.baseUrl + '/paypal/checkout_return',
cancel_url: config.baseUrl + '/paypal/checkout_cancel'
2018-01-07 04:55:48 +10:00
},
2019-07-12 18:06:34 +10:00
transactions: [{
amount: {
total: req.session.totalCartAmount,
currency: paypalConfig.paypalCurrency
2018-01-07 04:55:48 +10:00
},
2019-07-12 18:06:34 +10:00
description: paypalConfig.paypalCartDescription
2018-01-07 04:55:48 +10:00
}]
};
// set the config
paypal.configure(paypalConfig);
// create payment
paypal.payment.create(payment, (error, payment) => {
if(error){
req.session.message = 'There was an error processing your payment. You have not been changed and can try again.';
req.session.messageType = 'danger';
res.redirect('/checkout/payment');
2018-01-07 04:55:48 +10:00
return;
}
if(payment.payer.payment_method === 'paypal'){
req.session.paymentId = payment.id;
let redirectUrl;
for(let i = 0; i < payment.links.length; i++){
2019-07-12 18:06:34 +10:00
const link = payment.links[i];
2018-01-07 04:55:48 +10:00
if(link.method === 'REDIRECT'){
redirectUrl = link.href;
}
}
// if there is no items in the cart then render a failure
if(!req.session.cart){
req.session.message = 'The are no items in your cart. Please add some items before checking out';
req.session.messageType = 'danger';
res.redirect('/');
return;
}
// new order doc
2019-07-12 18:06:34 +10:00
const orderDoc = {
2018-01-07 04:55:48 +10:00
orderPaymentId: payment.id,
orderPaymentGateway: 'Paypal',
orderTotal: req.session.totalCartAmount,
orderShipping: req.session.totalCartShipping,
2019-12-30 17:35:43 +10:00
orderItemCount: req.session.totalCartItems,
orderProductCount: req.session.totalCartProducts,
orderEmail: req.session.customerEmail,
orderCompany: req.session.customerCompany,
orderFirstname: req.session.customerFirstname,
orderLastname: req.session.customerLastname,
orderAddr1: req.session.customerAddress1,
orderAddr2: req.session.customerAddress2,
orderCountry: req.session.customerCountry,
orderState: req.session.customerState,
orderPostcode: req.session.customerPostcode,
orderPhoneNumber: req.session.customerPhone,
orderComment: req.session.orderComment,
2018-01-07 04:55:48 +10:00
orderStatus: payment.state,
orderDate: new Date(),
2020-01-02 15:23:39 +10:00
orderProducts: req.session.cart,
orderType: 'Single'
2018-01-07 04:55:48 +10:00
};
if(req.session.orderId){
// we have an order ID (probably from a failed/cancelled payment previosuly) so lets use that.
// send the order to Paypal
res.redirect(redirectUrl);
}else{
// no order ID so we create a new one
2019-10-29 18:26:30 +10:00
db.orders.insertOne(orderDoc, (err, newDoc) => {
2018-01-07 04:55:48 +10:00
if(err){
console.info(err.stack);
}
// get the new ID
2019-11-03 15:33:11 +10:00
const newId = newDoc.insertedId;
2018-01-07 04:55:48 +10:00
// set the order ID in the session
req.session.orderId = newId;
// send the order to Paypal
res.redirect(redirectUrl);
});
}
}
});
});
module.exports = router;