-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (63 loc) · 1.63 KB
/
Copy pathindex.js
File metadata and controls
70 lines (63 loc) · 1.63 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
require('dotenv').config();
const express = require('express');
const app = express();
const rabbit = require('./rabbitmq');
const logger = require('./logger');
const { numberConvert, formatter } = require('./util');
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
const validator = (req, res, next) => {
if (req.headers['x-api-key'] === process.env.API_KEY) {
next();
} else {
res.status(401).send('unauthorized');
}
}
const paymentHandler = (req, res) => {
try {
const {
paymentChannel:channelName,
phoneNumber,
message
} = req.body;
let amountString = formatter[channelName](message);
if (amountString) {
amountString = amountString.join();
const amount = numberConvert(amountString);
const notificationDetails = {
channelName,
phoneNumber,
amount
}
logger.info(notificationDetails);
rabbit.sendToQueue(channelName, Buffer.from(JSON.stringify(req.body)));
}
} catch (e) {
console.error(e);
logger.error(e);
}
res.status(204).send();
}
app.get('/', (req, res) => {
res.send('Jeketibot payment service');
})
app.post('/', validator, paymentHandler);
app.use( (req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
})
app.use( (err, req, res, next) => {
console.log(err);
logger.error(err);
res.status(err.status || 500);
res.send({
error: err
});
})
app.listen(process.env.NODE_PORT, () => {
console.log(`payment service is running in port ${process.env.NODE_PORT}`);
logger.info(`payment service is running in port ${process.env.NODE_PORT}`);
})