Skip to content

Commit 9cba502

Browse files
committed
Split out transactions into own report
1 parent 3cd9ea7 commit 9cba502

File tree

16 files changed

+510
-86
lines changed

16 files changed

+510
-86
lines changed

app.js

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const product = require('./routes/product');
5656
const customer = require('./routes/customer');
5757
const order = require('./routes/order');
5858
const user = require('./routes/user');
59+
const transactions = require('./routes/transactions');
5960
const reviews = require('./routes/reviews');
6061

6162
const app = express();
@@ -263,6 +264,12 @@ handlebars = handlebars.create({
263264
}
264265
return '';
265266
},
267+
toUpper: (value) => {
268+
if(value){
269+
return value.toUpperCase();
270+
}
271+
return value;
272+
},
266273
upperFirst: (value) => {
267274
if(value){
268275
return value.replace(/^\w/, (chr) => {
@@ -405,6 +412,7 @@ app.use('/', product);
405412
app.use('/', order);
406413
app.use('/', user);
407414
app.use('/', admin);
415+
app.use('/', transactions);
408416
app.use('/', reviews);
409417

410418
// Payment route(s)

lib/db.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function initDb(dbUrl, callback){ // eslint-disable-line
2929
db.products = db.collection('products');
3030
db.variants = db.collection('variants');
3131
db.orders = db.collection('orders');
32+
db.transactions = db.collection('transactions');
3233
db.pages = db.collection('pages');
3334
db.menu = db.collection('menu');
3435
db.customers = db.collection('customers');

lib/indexing.js

+34
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ const indexOrders = async (app) => {
9090
}
9191
};
9292

93+
const indexTransactions = async(app) => {
94+
// Get transactions
95+
const transactionsList = await app.db.transactions.find({}).toArray();
96+
97+
// setup lunr indexing
98+
const transactionsIndex = lunr(function(){
99+
this.field('gatewayReference', { boost: 10 });
100+
this.field('amount', { boost: 5 });
101+
this.field('customer', { boost: 5 });
102+
this.field('gatewayMessage');
103+
104+
const lunrIndex = this;
105+
106+
// add to lunr index
107+
for(const transaction of transactionsList){
108+
const doc = {
109+
gatewayReference: transaction.gatewayReference,
110+
amount: transaction.amount,
111+
customer: transaction.customer,
112+
gatewayMessage: transaction.gatewayMessage,
113+
id: transaction._id
114+
};
115+
lunrIndex.add(doc);
116+
};
117+
});
118+
119+
app.transactionsIndex = transactionsIndex;
120+
if(process.env.NODE_ENV !== 'test'){
121+
console.log(colors.cyan('- Transaction indexing complete'));
122+
}
123+
};
124+
93125
const indexReviews = async(app) => {
94126
// Get reviews
95127
const reviewsList = await app.db.reviews.find({}).toArray();
@@ -129,13 +161,15 @@ const runIndexing = async (app) => {
129161
await indexProducts(app);
130162
await indexOrders(app);
131163
await indexCustomers(app);
164+
await indexTransactions(app);
132165
await indexReviews(app);
133166
};
134167

135168
module.exports = {
136169
indexProducts,
137170
indexCustomers,
138171
indexOrders,
172+
indexTransactions,
139173
indexReviews,
140174
runIndexing
141175
};

lib/payments/adyen.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require('express');
2-
const { indexOrders } = require('../indexing');
2+
const { indexOrders, indexTransactions } = require('../indexing');
33
const numeral = require('numeral');
44
const got = require('got');
55
const { getId, sendEmail, getEmailTemplate } = require('../common');
@@ -48,18 +48,35 @@ router.post('/setup', async (req, res, next) => {
4848
router.post('/checkout_action', async (req, res, next) => {
4949
const db = req.app.db;
5050
const config = req.app.config;
51+
const adyenConfig = getPaymentConfig('adyen');
5152

5253
// Update response
5354
let paymentStatus = 'Paid';
55+
let approved = true;
5456
if(req.body.paymentCode !== 'Authorised'){
5557
paymentStatus = 'Declined';
58+
approved = false;
5659
}
5760

61+
// Create our transaction
62+
const transaction = await db.transactions.insertOne({
63+
gateway: 'adyen',
64+
gatewayReference: req.body.paymentId,
65+
gatewayMessage: req.body.paymentCode,
66+
approved: approved,
67+
amount: req.session.totalCartAmount,
68+
currency: adyenConfig.currency,
69+
customer: getId(req.session.customerId),
70+
created: new Date()
71+
});
72+
73+
const transactionId = transaction.insertedId;
74+
75+
// Index transactios
76+
await indexTransactions(req.app);
77+
5878
// new order doc
5979
const orderDoc = {
60-
orderPaymentId: req.body.paymentId,
61-
orderPaymentGateway: 'Adyen',
62-
orderPaymentMessage: req.body.paymentCode,
6380
orderTotal: req.session.totalCartAmount,
6481
orderShipping: req.session.totalCartShipping,
6582
orderItemCount: req.session.totalCartItems,
@@ -79,7 +96,8 @@ router.post('/checkout_action', async (req, res, next) => {
7996
orderStatus: paymentStatus,
8097
orderDate: new Date(),
8198
orderProducts: req.session.cart,
82-
orderType: 'Single'
99+
orderType: 'Single',
100+
transaction: transactionId
83101
};
84102

85103
// insert order into DB
@@ -88,6 +106,15 @@ router.post('/checkout_action', async (req, res, next) => {
88106
// get the new ID
89107
const newId = newOrder.insertedId;
90108

109+
// Update order to transaction
110+
await db.transactions.updateOne({
111+
_id: getId(transactionId)
112+
}, {
113+
$set: {
114+
order: getId(newId)
115+
}
116+
});
117+
91118
// add to lunr index
92119
indexOrders(req.app)
93120
.then(() => {

lib/payments/authorizenet.js

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const express = require('express');
22
const axios = require('axios');
33
const stripBom = require('strip-bom');
4-
const { indexOrders } = require('../indexing');
4+
const { indexOrders, indexTransactions } = require('../indexing');
55
const { getId, sendEmail, getEmailTemplate } = require('../common');
66
const { getPaymentConfig } = require('../config');
77
const { emptyCart } = require('../cart');
@@ -52,15 +52,33 @@ router.post('/checkout_action', (req, res, next) => {
5252

5353
// order status if approved
5454
let orderStatus = 'Paid';
55+
let approved = true;
56+
let paymentMessage = 'Your payment was successfully completed';
5557
if(txn && txn.responseCode !== '1'){
5658
console.log('Declined response payload', response.data);
59+
paymentMessage = 'Your payment was declined';
5760
orderStatus = 'Declined';
61+
approved = false;
5862
}
5963

64+
// Create our transaction
65+
const transaction = await db.transactions.insertOne({
66+
gateway: 'authorizenet',
67+
gatewayReference: txn.transHash,
68+
gatewayMessage: paymentMessage,
69+
approved: approved,
70+
amount: req.session.totalCartAmount,
71+
currency: config.currencyISO,
72+
customer: getId(req.session.customerId),
73+
created: new Date()
74+
});
75+
76+
const transactionId = transaction.insertedId;
77+
78+
// Index transactios
79+
await indexTransactions(req.app);
80+
6081
const orderDoc = {
61-
orderPaymentId: txn.transHash,
62-
orderPaymentGateway: 'AuthorizeNet',
63-
orderPaymentMessage: 'Your payment was successfully completed',
6482
orderTotal: req.session.totalCartAmount,
6583
orderShipping: req.session.totalCartShipping,
6684
orderItemCount: req.session.totalCartItems,
@@ -80,7 +98,8 @@ router.post('/checkout_action', (req, res, next) => {
8098
orderStatus: orderStatus,
8199
orderDate: new Date(),
82100
orderProducts: req.session.cart,
83-
orderType: 'Single'
101+
orderType: 'Single',
102+
transaction: transactionId
84103
};
85104

86105
// insert order into DB
@@ -90,6 +109,15 @@ router.post('/checkout_action', (req, res, next) => {
90109
// get the new ID
91110
const newId = newDoc.insertedId;
92111

112+
// Update order to transaction
113+
await db.transactions.updateOne({
114+
_id: getId(transactionId)
115+
}, {
116+
$set: {
117+
order: getId(newId)
118+
}
119+
});
120+
93121
// add to lunr index
94122
indexOrders(req.app)
95123
.then(() => {

lib/payments/paypal.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require('express');
2-
const { indexOrders } = require('../indexing');
2+
const { indexOrders, indexTransactions } = require('../indexing');
33
const { getId, sendEmail, getEmailTemplate } = require('../common');
44
const { getPaymentConfig } = require('../config');
55
const { emptyCart } = require('../cart');
@@ -14,11 +14,12 @@ router.get('/checkout_cancel', (req, res, next) => {
1414
router.get('/checkout_return', (req, res, next) => {
1515
const db = req.app.db;
1616
const config = req.app.config;
17+
const paypalConfig = getPaymentConfig('paypal');
1718
const paymentId = req.session.paymentId;
1819
const payerId = req.query.PayerID;
1920

2021
const details = { payer_id: payerId };
21-
paypal.payment.execute(paymentId, details, (error, payment) => {
22+
paypal.payment.execute(paymentId, details, async(error, payment) => {
2223
let paymentApproved = false;
2324
let paymentMessage = '';
2425
let paymentDetails = '';
@@ -50,7 +51,7 @@ router.get('/checkout_return', (req, res, next) => {
5051
if(payment.state === 'approved'){
5152
paymentApproved = true;
5253
paymentStatus = 'Paid';
53-
paymentMessage = 'Your payment was successfully completed';
54+
paymentMessage = 'Succeeded';
5455
paymentDetails = `<p><strong>Order ID: </strong>${paymentOrderId}</p><p><strong>Transaction ID: </strong>${payment.id}</p>`;
5556

5657
// clear the cart
@@ -62,16 +63,34 @@ router.get('/checkout_return', (req, res, next) => {
6263
// failed
6364
if(payment.failureReason){
6465
paymentApproved = false;
65-
paymentMessage = `Your payment failed - ${payment.failureReason}`;
66+
paymentMessage = `Declined: ${payment.failureReason}`;
6667
paymentStatus = 'Declined';
6768
}
6869

70+
// Create our transaction
71+
const transaction = await db.transactions.insertOne({
72+
gateway: 'paypal',
73+
gatewayReference: payment.id,
74+
gatewayMessage: paymentMessage,
75+
approved: paymentApproved,
76+
amount: req.session.totalCartAmount,
77+
currency: paypalConfig.paypalCurrency,
78+
customer: getId(req.session.customerId),
79+
created: new Date(),
80+
order: getId(paymentOrderId)
81+
});
82+
83+
const transactionId = transaction.insertedId;
84+
85+
// Index transactios
86+
await indexTransactions(req.app);
87+
6988
// update the order status
70-
db.orders.updateOne({ _id: getId(paymentOrderId) }, { $set: { orderStatus: paymentStatus } }, { multi: false }, (err, numReplaced) => {
89+
db.orders.updateOne({ _id: getId(paymentOrderId) }, { $set: { orderStatus: paymentStatus, transaction: transactionId } }, { multi: false }, (err, numReplaced) => {
7190
if(err){
7291
console.info(err.stack);
7392
}
74-
db.orders.findOne({ _id: getId(paymentOrderId) }, (err, order) => {
93+
db.orders.findOne({ _id: getId(paymentOrderId) }, async (err, order) => {
7594
if(err){
7695
console.info(err.stack);
7796
}
@@ -134,7 +153,7 @@ router.post('/checkout_action', (req, res, next) => {
134153
paypal.configure(paypalConfig);
135154

136155
// create payment
137-
paypal.payment.create(payment, (error, payment) => {
156+
paypal.payment.create(payment, async(error, payment) => {
138157
if(error){
139158
req.session.message = 'There was an error processing your payment. You have not been charged and can try again.';
140159
req.session.messageType = 'danger';

lib/payments/payway.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require('express');
2-
const { indexOrders } = require('../indexing');
2+
const { indexOrders, indexTransactions } = require('../indexing');
33
const { getId, sendEmail, getEmailTemplate } = require('../common');
44
const { getPaymentConfig } = require('../config');
55
const { emptyCart } = require('../cart');
@@ -50,15 +50,31 @@ router.post('/checkout_action', async (req, res, next) => {
5050

5151
// order status if approved
5252
let orderStatus = 'Paid';
53+
let approved = true;
5354
if(txn && txn.status === 'declined'){
5455
console.log('Declined response payload', txn);
5556
orderStatus = 'Declined';
57+
approved = false;
5658
}
5759

60+
// Create our transaction
61+
const transaction = await db.transactions.insertOne({
62+
gateway: 'payway',
63+
gatewayReference: txn.transactionId.toString(),
64+
gatewayMessage: `${txn.responseCode} - ${txn.responseText}`,
65+
approved: approved,
66+
amount: req.session.totalCartAmount,
67+
currency: 'aud',
68+
customer: getId(req.session.customerId),
69+
created: new Date()
70+
});
71+
72+
const transactionId = transaction.insertedId;
73+
74+
// Index transactios
75+
await indexTransactions(req.app);
76+
5877
const orderDoc = {
59-
orderPaymentId: txn.transactionId.toString(),
60-
orderPaymentGateway: 'PayWay',
61-
orderPaymentMessage: `${txn.responseCode} - ${txn.responseText}`,
6278
orderTotal: req.session.totalCartAmount,
6379
orderShipping: req.session.totalCartShipping,
6480
orderItemCount: req.session.totalCartItems,
@@ -78,7 +94,8 @@ router.post('/checkout_action', async (req, res, next) => {
7894
orderStatus: orderStatus,
7995
orderDate: new Date(),
8096
orderProducts: req.session.cart,
81-
orderType: 'Single'
97+
orderType: 'Single',
98+
transaction: transactionId
8299
};
83100

84101
// insert order into DB
@@ -88,6 +105,15 @@ router.post('/checkout_action', async (req, res, next) => {
88105
// get the new ID
89106
const newId = newDoc.insertedId;
90107

108+
// Update order to transaction
109+
await db.transactions.updateOne({
110+
_id: getId(transactionId)
111+
}, {
112+
$set: {
113+
order: getId(newId)
114+
}
115+
});
116+
91117
// add to lunr index
92118
indexOrders(req.app)
93119
.then(() => {

0 commit comments

Comments
 (0)