-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
79 lines (60 loc) · 1.99 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
const express = require('express')
const validateForm = require('./validateForm')
const keys = require('./config/keys')
const sgMail = require('@sendgrid/mail');
const stripe = require('stripe')(keys.stripe_secret_key)
const cors = require('cors')
const { v4: uuid } = require('uuid');
const app = express()
// app.use(cors)
app.use(express.json({ extended: false }))
app.post('/validateForm', (req, res) => {
const { errors, isValid } = validateForm(req.body)
if (!isValid) {
res.status(400).json(errors)
}
})
app.post('/checkout', (req, res) => {
const { fields, token } = req.body
const idempotencyKey = uuid()
stripe.customers.create({
email: token.email,
source: token.id
}).then(customer => {
stripe.charges.create({
amount: fields.cartTotal * 100, // cents
currency: 'AUD',
customer: customer.id,
receipt_email: token.email,
// description: product.name,
// shipping: {
// name: token.card.name,
// address: {
// country: token.card.address_country
// }
// }
}, { idempotencyKey })
})
.then(() => {
sgMail.setApiKey(keys.sendGridAPI)
const msg = {
to: token.email,
from: '[email protected]',
subject: 'Your order is being processed.',
text: `We've receieved your order. We will let you know when it's on its way.`
}
sgMail.send(msg)
res.json({ success: 'Success' })
})
.catch(err => console.log(err))
})
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})