-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
341 lines (295 loc) · 10.1 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
var express = require('express');
const cache = require('memory-cache');
const bodyParser = require('body-parser');
const cors = require('cors');
const axios = require('axios');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors());
app.options('*', cors());
app.get('/', (req, res) => res.status(200).send('Arkesel Rocks!!'));
app.post('/ussd', ((req, res) => {
const {
sessionID,
userID,
newSession,
msisdn,
userData,
network,
} = req.body;
if (newSession) {
const message = "Welcome to Arkesel Voting Portal. Please vote for your favourite service from Arkesel" +
"\n1. SMS" +
"\n2. Voice" +
"\n3. Email" +
"\n4. USSD" +
"\n5. Payments";
const continueSession = true;
// Keep track of the USSD state of the user and their session
const currentState = {
sessionID,
msisdn,
userData,
network,
newSession,
message,
level: 1,
page: 1,
};
let userResponseTracker = cache.get(sessionID);
!userResponseTracker
? userResponseTracker = [{ ...currentState }]
: userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
userID,
sessionID,
message,
continueSession,
msisdn
});
}
const userResponseTracker = cache.get(sessionID);
if (!userResponseTracker) {
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
userID,
sessionID,
message: 'Error! Please dial code again!',
continueSession: false,
msisdn
});
}
const lastResponse = userResponseTracker[userResponseTracker.length - 1];
let message = "Bad Option";
let continueSession = false;
if (lastResponse.level === 1) {
if (["2", "3", "4", "5"].includes(userData)) {
message = "Thank you for voting!";
continueSession = false;
} else if (userData === '1') {
message = "For SMS which of the features do you like best?" +
"\n1. From File" +
"\n2. Quick SMS" +
"\n\n #. Next Page";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 2,
msisdn,
message,
userData,
network,
newSession,
page: 1,
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
}
} else if (lastResponse.level === 2) {
if (lastResponse.page === 1 && userData === '#') {
message = "For SMS which of the features do you like best?" +
"\n3. Bulk SMS" +
"\n\n*. Go Back" +
"\n#. Next Page";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 2,
msisdn,
message,
userData,
network,
newSession,
page: 2
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
} else if (lastResponse.page === 2 && userData === '#') {
// Useful Resources
message = "For SMS which of the features do you like best?" +
"\n4. SMS To Contacts" +
"\n5. Enter your amount to vote with" +
"\n\n*. Go Back";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 2,
msisdn,
message,
userData,
network,
newSession,
page: 3,
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
} else if (lastResponse.page === 3 && userData === '*') {
message = "For SMS which of the features do you like best?" +
"\n3. Bulk SMS" +
"\n\n*. Go Back" +
"\n#. Next Page";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 2,
msisdn,
message,
userData,
network,
newSession,
page: 2
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
} else if (lastResponse.page === 2 && userData === '*') {
message = "For SMS which of the features do you like best?" +
"\n1. From File" +
"\n2. Quick SMS" +
"\n\n #. Next Page";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 2,
msisdn,
message,
userData,
network,
newSession,
page: 1,
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
} else if (["1", "2", "3", "4"].includes(userData)) {
message = "Thank you for voting!";
continueSession = false;
} else if (userData === "5") {
message = "Enter your amount to pay below: ";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 3,
msisdn,
message,
userData,
network,
newSession,
page: 1,
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
} else {
message = "Bad choice!";
continueSession = false;
}
} else if (lastResponse.level === 3) {
if (!isNaN(userData) && parseFloat(userData) > 0) {
const uniqueRef = `${Date.now() + (Math.random() * 100)}`;
const paymentRequest = {
account_number: msisdn,
merchant_reference: uniqueRef,
channel: "mobile-money",
provider: network.toLowerCase(),
transaction_type: "debit",
amount: userData,
purpose: "voting payment",
service_name: "arkesel voting",
currency: "GHS",
};
const apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxx=';
const url = 'https://payment.arkesel.com/api/v1/payment/charge/initiate';
axios({
method: 'post',
url,
data: { ...paymentRequest },
headers: {
'api-key': apiKey,
}
}).then(res => res.data).then(data => {
console.log({ data }, 'Initiate payment');
// Save into DB
// If it was successful then send message
message = `Kindly enter your pin for the approval of GHS ${userData} debiting of your Mobile money account. We are charging you for voting. Dial *170# to visit approvals if one doesn't pop up!`;
continueSession = false;
});
message = `Kindly enter your pin for the approval of GHS ${userData} debiting of your Mobile money account. We are charging you for voting. Dial *170# to visit approvals if one doesn't pop up!`;
continueSession = false;
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
userID,
sessionID,
message,
continueSession,
msisdn
});
} else {
message = "You entered an invalid amount: ";
continueSession = true;
const currentState = {
sessionID,
userID,
level: 3,
msisdn,
message,
userData,
network,
newSession,
page: 1,
};
userResponseTracker.push({ ...currentState });
cache.put(sessionID, userResponseTracker);
}
}
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
userID,
sessionID,
message,
continueSession,
msisdn
});
}));
// Callback URL for payment
app.get('/payments/arkesel/callback', ((req, res) => {
console.log({ query: res.query }, 'Callback for Arkesel payment');
// Verify the payment...
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
status: 'success',
message: 'arkesel payment callback called'
});
}));
// Verify payment
app.get('/payments/verify', ((req, res) => {
const apiKey = 'XXXXXXXXXXXXXXXXXXXXXXX=';
const transRef = 'T634E3e8cac8175';
const url = `https://payment.arkesel.com/api/v1/verify/transaction/${transRef}`;
axios({
method: 'get',
url,
headers: {
'api-key': apiKey,
}
}).then(res => res.data).then(data => {
console.log({ data }, 'Verify payment');
// Update payment status in DB
});
res.setHeader('Content-Type', 'application/json');
return res.status(200).json({
status: 'success',
message: 'payment verification called'
});
}));
app.listen(8000, function () {
console.log('Arkesel USSD app listening on 8000!');
});