expressCart/lib/payments/stripe.js

292 lines
11 KiB
JavaScript
Raw Normal View History

2019-07-12 18:06:34 +10:00
const express = require('express');
const common = require('../common');
const { indexOrders } = require('../indexing');
2019-07-12 18:06:34 +10:00
const numeral = require('numeral');
const stripe = require('stripe')(common.getPaymentConfig().secretKey);
const router = express.Router();
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 stripeConfig = common.getPaymentConfig();
2018-01-07 04:55:48 +10:00
// Create the Stripe payload
const chargePayload = {
2018-01-07 04:55:48 +10:00
amount: numeral(req.session.totalCartAmount).format('0.00').replace('.', ''),
currency: stripeConfig.stripeCurrency.toLowerCase(),
2018-01-07 04:55:48 +10:00
source: req.body.stripeToken,
description: stripeConfig.stripeDescription,
shipping: {
name: `${req.session.customerFirstname} ${req.session.customerFirstname}`,
address: {
line1: req.session.customerAddress1,
line2: req.session.customerAddress2,
postal_code: req.session.customerPostcode,
state: req.session.customerState,
country: req.session.customerCountry
}
}
};
// charge via stripe
stripe.charges.create(chargePayload, (err, charge) => {
2018-01-07 04:55:48 +10:00
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('/checkout/payment');
2018-01-07 04:55:48 +10:00
return;
}
// order status
let paymentStatus = 'Paid';
if(charge.paid !== true){
paymentStatus = 'Declined';
}
// new order doc
2019-07-12 18:06:34 +10:00
const orderDoc = {
2018-01-07 04:55:48 +10:00
orderPaymentId: charge.id,
orderPaymentGateway: 'Stripe',
orderPaymentMessage: charge.outcome.seller_message,
orderTotal: req.session.totalCartAmount,
orderShipping: req.session.totalCartShipping,
2019-12-30 17:35:43 +10:00
orderItemCount: req.session.totalCartItems,
orderProductCount: req.session.totalCartProducts,
orderCustomer: common.getId(req.session.customerId),
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: paymentStatus,
orderDate: new Date(),
orderProducts: req.session.cart,
orderType: 'Single'
2018-01-07 04:55:48 +10:00
};
// insert order into DB
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-04 16:39:05 +10:00
const newId = newDoc.insertedId;
2018-01-07 04:55:48 +10:00
// add to lunr index
2019-06-15 15:56:51 +10:00
indexOrders(req.app)
2018-01-07 22:14:17 +10:00
.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.ops[0].orderEmail;
2018-01-07 22:14:17 +10:00
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
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: 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){
2020-01-02 15:23:39 +10:00
common.emptyCart(req, res, 'function');
2018-01-07 22:14:17 +10:00
}
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
});
});
});
// Subscription hook from Stripe
router.all('/subscription_update', async (req, res, next) => {
const db = req.app.db;
const stripeSigSecret = common.getPaymentConfig().stripeWebhookSecret;
const stripeSig = req.headers['stripe-signature'];
let hook;
if(stripeSigSecret){
try{
hook = await stripe.webhooks.constructEvent(req.rawBody, stripeSig, stripeSigSecret);
console.info('Stripe Webhook received');
}catch(err){
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if(!hook.data.object.customer){
return res.status(400).json({ message: 'Customer not found' });
}
}else{
hook = req.body;
}
const order = await db.orders.findOne({
orderCustomer: hook.data.object.customer,
orderType: 'Subscription'
});
if(!order){
return res.status(400).json({ message: 'Order not found' });
}
let orderStatus = 'Paid';
if(hook.type === 'invoice.payment_failed'){
orderStatus = 'Declined';
}
// Update order status
await db.orders.updateOne({
_id: common.getId(order._id),
orderType: 'Subscription'
}, {
$set: {
orderStatus: orderStatus
}
});
return res.status(200).json({ message: 'Status successfully updated' });
});
router.post('/checkout_action_subscription', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
try{
const plan = await stripe.plans.retrieve(req.body.stripePlan);
if(!plan){
req.session.messageType = 'danger';
req.session.message = 'The plan connected to this product doesn\'t exist';
res.redirect('/checkout/payment');
return;
}
}catch(ex){
req.session.messageType = 'danger';
req.session.message = 'The plan connected to this product doesn\'t exist';
res.redirect('/checkout/payment');
return;
}
// Create customer
const customer = await stripe.customers.create({
source: req.body.stripeToken,
plan: req.body.stripePlan,
email: req.body.shipEmail,
name: `${req.body.shipFirstname} ${req.body.shipLastname}`,
phone: req.body.shipPhoneNumber
});
if(!customer){
req.session.messageType = 'danger';
req.session.message = 'Your subscripton has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = '';
res.redirect('/checkout/payment');
return;
}
// Check for a subscription
if(customer.subscriptions.data && customer.subscriptions.data.length === 0){
req.session.messageType = 'danger';
req.session.message = 'Your subscripton has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = '';
res.redirect('/checkout/payment');
return;
}
const subscription = customer.subscriptions.data[0];
// Create the new order document
const orderDoc = {
orderPaymentId: subscription.id,
orderPaymentGateway: 'Stripe',
orderPaymentMessage: subscription.collection_method,
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,
orderStatus: 'Pending',
orderDate: new Date(),
orderProducts: req.session.cart,
orderType: 'Subscription',
orderCustomer: customer.id
};
// insert order into DB
const order = await db.orders.insertOne(orderDoc);
const orderId = order.insertedId;
indexOrders(req.app)
.then(() => {
// set the results
req.session.messageType = 'success';
req.session.message = 'Your subscription was successfully created';
req.session.paymentEmailAddr = req.body.shipEmail;
req.session.paymentApproved = true;
req.session.paymentDetails = '<p><strong>Order ID: </strong>' + orderId + '</p><p><strong>Subscription ID: </strong>' + subscription.id + '</p>';
// set payment results for email
const paymentResults = {
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
paymentApproved: true,
paymentDetails: req.session.paymentDetails
};
// clear the cart
if(req.session.cart){
2020-01-02 15:23:39 +10:00
common.emptyCart(req, res, 'function');
}
// send the email with the response
common.sendEmail(req.session.paymentEmailAddr, 'Your payment with ' + config.cartTitle, common.getEmailTemplate(paymentResults));
// redirect to outcome
res.redirect('/payment/' + orderId);
});
});
2018-01-07 04:55:48 +10:00
module.exports = router;