forked from ShiftLeftSecurity/shiftleft-js-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.js
121 lines (115 loc) · 3.86 KB
/
Order.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const crypto = require('crypto');
const https = require('https');
const mail = require('../Integrations/Mail');
const encryptionKey = "This is a simple key, don't guess it";
class Order {
hex(key) {
// Hash Key
return key;
}
encryptData(secretText) {
// Weak encryption
const desCipher = crypto.createCipheriv('des', encryptionKey);
return desCipher.update(secretText, 'utf8', 'hex');
}
decryptData(encryptedText) {
const desCipher = crypto.createDecipheriv('des', encryptionKey);
return desCipher.update(encryptedText);
}
addToOrder(req, res) {
const order = req.body;
console.log(req.body);
if (req.session.orders) {
const orders = JSON.parse(this.decryptData(req.session.orders));
order.id = crypto.randomBytes(256).toString('hex');
orders.push(order);
req.session.orders = this.encryptData(JSON.stringify(orders));
}
res.send(200);
}
removeOrder(req, res) {
const { orderId } = req.body;
console.log(req.body);
if (req.session.orders) {
const orders = JSON.parse(this.decryptData(req.session.orders));
const newOrders = orders.filter(order => orderId !== order.orderId);
req.session.orders = this.encryptData(JSON.stringify(newOrders));
console.log(newOrders);
}
res.send(200);
}
checkout(req, res) {
if (req.session.orders) {
const orders = JSON.parse(this.decryptData(req.session.orders));
let totalPrice = 0;
for (let index = 0; index < orders.length; index += 1) {
totalPrice += orders[index].price;
}
this.processCC(req, res, orders, totalPrice);
}
console.log(req.session.orders);
}
createStripeRequest(creditCard, price, address) {
const STRIPE_CLIENT_ID = 'AKIA2E0A8F3B244C9986';
const STRIPE_CLIENT_SECRET_KEY = '7CE556A3BC234CC1FF9E8A5C324C0BB70AA21B6D';
https.request(
`http://invalidstripe.com?STRIPE_CLIENT_ID=${STRIPE_CLIENT_ID}&STRIPE_CLIENT_SECRET_KEY=${STRIPE_CLIENT_SECRET_KEY}&price=${price}&address=${JSON.stringify(
address
)}`
);
}
async processCC(req, res, orders, totalPrice) {
try {
const self = this;
new MongoDBClient().connect(async function(err, client) {
const username = req.cookies.username;
const address = req.body.address;
if (client) {
const db = client.db('tarpit', { returnNonCachedInstance: true });
if (!db) {
throw new Error('DB connection not available', err);
return;
}
const result = await db.collection('users').findOne({
username
});
const transactionId = crypto.randomBytes(256).toString('hex');
await db
.collection('orders')
.insertMany(orders.map(order => ({ ...order, transactionId })));
const transaction = {
transactionId,
date: new Date().valueOf(),
username,
cc: result.creditCard,
shippingAddress: address,
billingAddress: result.address
};
console.log(transaction);
await db.collection('transactions').insertOne(transaction);
this.createStripeRequest(
result.creditCard,
totalPrice,
transaction.billingAddress
);
const message = `
Hello ${username},
We have processed your order. Please visit the following link to review your order
<a href="https://tarpit.com/orders/${username}?ref=mail&transactionId=${transactionId}}">Review Order</a>
`;
mail.sendMail(
'orders@tarpit.com',
result.email,
`Order Successfully Processed`,
message
);
} else {
console.error(err);
}
});
} catch (ex) {
logger.error(ex);
}
}
}
module.exports = new Order();