-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (56 loc) · 1.69 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
require('dotenv').config();
const cors = require('cors');
const stripe = require('stripe')(process.env.REACT_APP_STRIPE_SECRET_KEY);
const express = require('express');
const app = express();
app.use(cors());
app.use(express.json());
const path = require('path');
const orderAmountArray = [];
const calculateOrderAmount = (items) => {
items.map((item) => {
const { price, cartTotalQuantity } = item;
const cartTotalProductsAmount = parseInt(price * cartTotalQuantity);
return orderAmountArray.push(cartTotalProductsAmount);
});
const totalProductsPriceAmount = orderAmountArray.reduce((a, b) => {
return a + b;
}, 0);
return totalProductsPriceAmount * 100;
};
if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
app.get('/', (req, res) => {
res.send('Lasso loja online');
});
app.post('/create-payment-intent', async (req, res) => {
const { items, shipping, description } = req.body;
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: 'brl',
automatic_payment_methods: {
enabled: true,
},
description,
shipping: {
address: {
line1: shipping.lineOne,
line2: shipping.lineTwo,
city: shipping.city,
country: shipping.country,
postal_code: shipping.portalCode,
},
name: shipping.name,
phone: shipping.phone,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
const port = process.env.PORT || 4242;
app.listen(port, () => console.log(`Servidor node funcionando na porta ${port}!`));