-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (78 loc) · 2.17 KB
/
index.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
const port = process.env.PORT || 8000;
const express = require('express');
const cors = require('cors');
const path = require('path');
const axios = require('axios');
require('dotenv').config();
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.use(
cors({
origin: 'http://localhost:8000',
credentials: true,
})
);
app.get('/api/checkout', (req, res) => {
const options = {
url: 'https://app.sandbox.midtrans.com/snap/v1/transactions',
method: 'post',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization:
'Basic ' +
Buffer.from(process.env.REACT_APP_MIDTRANS_SERVER_KEY).toString(
'base64'
),
},
data: {
transaction_details: {
order_id: req.query.order_id,
gross_amount: req.query.gross_amount,
},
item_details: req.query.item_details,
credit_card: {
secure: true,
},
customer_details: {
first_name: req.query.first_name,
last_name: req.query.last_name,
email: req.query.email,
phone: req.query.phone,
shipping_address: {
first_name: req.query.first_name,
last_name: req.query.last_name,
email: req.query.email,
phone: req.query.phone,
address: req.query.address,
city: req.query.city,
postal_code: req.query.postal_code,
},
},
},
};
axios(options).then(response => {
res.json(response.data.token);
});
});
app.get('/api/confirmation', (req, res) => {
const order_id = req.query.order_id;
const options = {
url: `https://api.sandbox.midtrans.com/v2/${order_id}/status`,
method: 'get',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization:
'Basic ' +
Buffer.from(process.env.MIDTRANS_SERVER_KEY + ':').toString('base64'),
},
};
axios(options).then(response => {
res.json(response.data);
});
});
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
app.listen(port, () => console.log('Server started on port ' + port));