-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (99 loc) · 3.68 KB
/
server.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
const express = require('express');
const cors = require('cors');
const path = require('path');
const compression = require('compression');
const enforce = require('express-sslify');
const admin = require('firebase-admin');
const ServiceAccount = require('./serviceAccountInfo.json');
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app = express();
const port = process.env.PORT || 5000;
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// app.use(enforce.HTTPS({ trustProtoHeader: true }));
app.use(cors());
admin.initializeApp({
credential: admin.credential.cert(ServiceAccount),
});
const firestore = admin.firestore();
app.listen(port, (error) => {
if (error) throw error;
console.log('Server running on port ' + port);
});
app.get('/service-worker.js', (req, res) => {
res.sendFile(
path.resolve(__dirname, '..', 'build', 'service-worker.js'),
);
});
app.post('/payment', (req, res) => {
const body = {
source: req.body.token.id,
amount: req.body.amount,
currency: 'usd',
};
stripe.charges.create(body, async (stripeErr, stripeRes) => {
if (stripeErr) {
res.status(500).send({ error: stripeErr });
} else {
const {
userId,
isOnTrial,
hasPaid,
paidAmount,
employeesAllowed,
} = req.body.currentUser;
let currentUser = req.body.currentUser;
const userRef = firestore.collection('users').doc(userId);
let updatedCurrentUser = {};
// user was on trial
if (isOnTrial && !hasPaid) {
updatedCurrentUser.isOnTrial = false;
updatedCurrentUser.hasPaid = true;
updatedCurrentUser.paidAmount = req.body.price;
updatedCurrentUser.workspacesLimit = 15;
updatedCurrentUser.employeesAllowed = req.body.employeesNum;
updatedCurrentUser.trialEndsAt = new Date();
currentUser.isOnTrial = false;
currentUser.hasPaid = true;
currentUser.paidAmount = req.body.price;
currentUser.workspacesLimit = 15;
currentUser.employeesAllowed = req.body.employeesNum;
currentUser.trialEndsAt = new Date();
// user has paid before
} else if (!isOnTrial && hasPaid) {
updatedCurrentUser.paidAmount =
Number(paidAmount) + Number(req.body.price);
updatedCurrentUser.workspacesLimit = 20;
updatedCurrentUser.employeesAllowed =
Number(employeesAllowed) + Number(req.body.employeesNum);
currentUser.paidAmount =
Number(paidAmount) + Number(req.body.price);
currentUser.workspacesLimit = 20;
currentUser.employeesAllowed =
Number(employeesAllowed) + Number(req.body.employeesNum);
// user trial has ended
} else if (!isOnTrial && !hasPaid) {
// @user when trial will also end if the user is invited by another user
// will have to creata a popover of inivitation
updatedCurrentUser.paidAmount = req.body.price;
updatedCurrentUser.workspacesLimit = 15;
updatedCurrentUser.employeesAllowed = req.body.employeesNum;
currentUser.paidAmount = req.body.price;
currentUser.workspacesLimit = 15;
currentUser.employeesAllowed = req.body.employeesNum;
}
await userRef.update(updatedCurrentUser);
res
.status(200)
.send({ success: stripeRes, userRes: currentUser });
}
});
});
app.get('/check', (req, res) => {
res.status(200).json({
success: true,
message: "working fine"
})
});