-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (66 loc) · 1.93 KB
/
Copy pathserver.js
File metadata and controls
74 lines (66 loc) · 1.93 KB
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
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.json());
const cors = require('cors');
const { paymentDetails } = require('./services/PaymentDetails');
const { getPaymentIntent, getRefund } = require('./services/StripeFunctions');
app.use(cors({
origin: '*'
}));
const port = process.env.PORT || 3001
app.get('/', (req,res) => {
res.send(`App is live in ${port}`)
})
app.post('/create-payment-intent', async (req, res) => {
try {
const { email } = req.body;
const data = await paymentDetails(email);
const paymentIntent = await getPaymentIntent(data);
res.json({clientSecret: paymentIntent.client_secret});
} catch (error) {
console.error(error);
res.status(500).send(error);
}
});
app.post('/refund', async (req,res) => {
try {
const { payment_intent, amount } = req.body;
console.log(req.body);
const refund = await getRefund(payment_intent, amount);
res.json(refund);
} catch(error){
console.error(error);
res.status(500).send(error);
}
});
/*
let email = "user123@u.nus.edu"
paymentDetails(email).then(data => {
console.log(data)
getPaymentIntent(data).then(paymentint => {
console.log(paymentint.client_secret)
})
})
*/
app.post('/test', async (req,res) => {
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: 1999,
currency: 'sgd',
automatic_payment_methods: {
enabled: true,
},
});
res.send({clientSecret: paymentIntent.client_secret})
} catch (error) {
return res.status(400).send({
error: {
message: error.message,
},
});
}
})
app.listen(port, () => console.log(`app is running on ${port}`))