This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
116 lines (98 loc) · 3.19 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* M-Pesa Mwitu
* Mohammed Sohail <sohail@forfuture.tech>
*/
/* eslint-disable no-console */
// npm-installed modules
const bodyParser = require("body-parser");
const express = require("express");
const mongoose = require("mongoose");
// own modules
const config = require("./config");
const message = require("./lib/message");
const package = require("./package");
const Payment = require("./schemas/payments");
// module variables
mongoose.connect(config.db);
const db = mongoose.connection;
const app = express();
app.disable("x-powered-by");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/api/transactions/receipt/:receipt", (req, res) => {
return Payment.findOne({
receipt: req.params.receipt
}, {
__v: 0,
_id: 0
}, (error, doc) => {
if (doc === null) {
return res.status(404).json({ ok: false, message: "Could not find the specified transaction" });
}
if (error) {
return res.status(500).json({ ok: false, message: "Internal server error" });
}
return res.status(200).json({ ok: true, message: "Transaction found", payload: doc });
});
});
app.get("/api/transactions/phone/:phone", (req, res) => {
return Payment.find({
phone: req.params.phone
}, {
__v: 0,
_id: 0
}, (error, doc) => {
if (doc.length === 0) {
return res.status(404).json({ ok: false, message: "Could not find the specified transaction" });
}
if (error) {
return res.status(500).json({ ok: false, message: "Internal server error" });
}
return res.status(200).json({ ok: true, message: "Transaction(s) found", payload: doc });
});
});
app.get("/api/transactions/validate/:receipt", (req, res) => {
return Payment.findOneAndUpdate({
receipt: req.params.receipt
}, {
$set: {
used: true
}
}, (error, doc) => {
if (error) {
return res.status(500).json({ ok: false, message: "Could not update transaction status" });
}
if (doc === null) {
return res.status(404).json({ ok: false, message: "Transaction does not exist" });
}
return res.status(200).json({ ok: true, message: "Transaction status updated" });
});
});
app.get("*", (req, res) => {
return res.send({ name: "M-Pesa Mwitu", version: package.version });
});
app.post("/", (req, res) => {
if (req.body.secret === config.secret && req.body.from === "MPESA") {
return message.parse(req.body.message, (ctx) => {
if (ctx.ok === false) {
return res.status(500).json(ctx);
}
if (ctx.type === "DONT_PROCESS") {
return res.status(200).json(ctx);
}
return res.status(200).json(ctx);
});
}
});
db.once("open", () => {
console.log("Successfully connected to DB");
app.listen(config.server.port, () => {
console.log(`Server started on port ${config.server.port}`);
});
});
db.on("error", () => {
console.error("Failed to connect to DB");
});
db.on("disconnected", () => {
console.log("Connection to DB disconnected");
});