expressCart/routes/payments/stripe.js

113 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-01-07 04:55:48 +10:00
let express = require('express');
2018-01-08 04:35:59 +10:00
let common = require('../common');
2018-01-07 04:55:48 +10:00
let numeral = require('numeral');
let stripe = require('stripe')(common.getPaymentConfig().secretKey);
let router = express.Router();
// The homepage of the site
router.post('/checkout_action', (req, res, next) => {
let db = req.app.db;
let config = common.getConfig();
let stripeConfig = common.getPaymentConfig();
// charge via stripe
stripe.charges.create({
amount: numeral(req.session.totalCartAmount).format('0.00').replace('.', ''),
currency: stripeConfig.stripeCurrency,
source: req.body.stripeToken,
description: stripeConfig.stripeDescription
}, (err, charge) => {
if(err){
console.info(err.stack);
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = '';
res.redirect('/pay');
return;
}
// order status
let paymentStatus = 'Paid';
if(charge.paid !== true){
paymentStatus = 'Declined';
}
// new order doc
let orderDoc = {
orderPaymentId: charge.id,
orderPaymentGateway: 'Stripe',
orderPaymentMessage: charge.outcome.seller_message,
orderTotal: req.session.totalCartAmount,
orderEmail: req.body.shipEmail,
orderFirstname: req.body.shipFirstname,
orderLastname: req.body.shipLastname,
orderAddr1: req.body.shipAddr1,
orderAddr2: req.body.shipAddr2,
orderCountry: req.body.shipCountry,
orderState: req.body.shipState,
orderPostcode: req.body.shipPostcode,
orderPhoneNumber: req.body.shipPhoneNumber,
orderStatus: paymentStatus,
orderDate: new Date(),
orderProducts: req.session.cart
};
// insert order into DB
db.orders.insert(orderDoc, (err, newDoc) => {
if(err){
console.info(err.stack);
}
// get the new ID
2018-01-07 22:14:17 +10:00
let newId = newDoc.insertedIds;
2018-01-07 04:55:48 +10:00
// add to lunr index
2018-01-07 22:14:17 +10:00
common.indexOrders(req.app)
.then(() => {
// if approved, send email etc
if(charge.paid === true){
// set the results
req.session.messageType = 'success';
req.session.message = 'Your payment was successfully completed';
req.session.paymentEmailAddr = newDoc.orderEmail;
req.session.paymentApproved = true;
req.session.paymentDetails = '<p><strong>Order ID: </strong>' + newId + '</p><p><strong>Transaction ID: </strong>' + charge.id + '</p>';
2018-01-07 04:55:48 +10:00
2018-01-07 22:14:17 +10:00
// set payment results for email
let paymentResults = {
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
paymentApproved: true,
paymentDetails: req.session.paymentDetails
};
2018-01-07 04:55:48 +10:00
2018-01-07 22:14:17 +10:00
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
}
2018-01-07 04:55:48 +10:00
2018-01-07 22:14:17 +10:00
// 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));
2018-01-07 04:55:48 +10:00
2018-01-07 22:14:17 +10:00
// redirect to outcome
res.redirect('/payment/' + newId);
}else{
// redirect to failure
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = '<p><strong>Order ID: </strong>' + newId + '</p><p><strong>Transaction ID: </strong>' + charge.id + '</p>';
res.redirect('/payment/' + newId);
}
});
2018-01-07 04:55:48 +10:00
});
});
});
module.exports = router;