-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (61 loc) · 2.07 KB
/
app.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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const app = express();
// Convenience middleware to parse JSON body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// These credentials should be received from the onboarding team.
// These are account specific credentials
const { API_KEY, IDENTITY_API_URL, GATEWAY_API_URL, PORT, REALM } = process.env;
if (!API_KEY || !IDENTITY_API_URL || !GATEWAY_API_URL) {
console.error("Error!!! Env configs not found");
process.exit();
}
app.get('/health', (_, res) => res.send('App working'));
/*
Example route that handles order creation, a similar route should be implemented
on your server to create orders and the response should be sent to the client
*/
app.post('/api/createOrder', async (req, res) => {
// Authenticate and receive bearer token
try {
const { data } = await axios({
method: 'post',
url: IDENTITY_API_URL,
headers: {
'Content-Type': 'application/vnd.ni-identity.v1+json',
'Authorization': `Basic ${API_KEY}`
},
data: {
grant_type: 'client_credentials',
realm: REALM,
}
});
const { access_token } = data;
// Create the order using the bearer token received from previous step and the order data received from client
// Refer docs for the possible fields available for order creation
const { data: orderData } = await axios.post(GATEWAY_API_URL, {
...req.body
}, {
headers: {
'Authorization': `Bearer ${access_token}`, // Note the access_token received in the previous step is passed here
'Content-Type': 'application/vnd.ni-payment.v2+json',
'Accept': 'application/vnd.ni-payment.v2+json'
},
},
);
res
.status(200)
.send(orderData)
} catch (error) {
console.error(error);
res
.status(400)
.send({
error,
message: 'An error occured',
});
}
});
app.listen(PORT || 3000, () => console.log(`Example app listening on port ${PORT}!`));