Skip to content

Commit ab83d87

Browse files
authored
Add files via upload
1 parent 72bb976 commit ab83d87

File tree

3 files changed

+1949
-0
lines changed

3 files changed

+1949
-0
lines changed

app.js

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
const express = require('express');
2+
const request = require('request')
3+
const bodyParser = require('body-parser');
4+
5+
const app = express();
6+
app.use(bodyParser.json())
7+
8+
9+
const access = (req, resp, next) => {
10+
let url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials';
11+
let auth = new Buffer('6TGuDGENXHduT7IoQgcf8mvlkmCZnxlx:zUxivY71Pycj1qyW').toString('base64');
12+
13+
request({
14+
url: url,
15+
headers: {
16+
'Authorization': 'Basic ' + auth
17+
}
18+
}, (error, response, body) => {
19+
if(error){
20+
console.log(error);
21+
} else {
22+
req.access_token = JSON.parse(body).access_token;
23+
next();
24+
}
25+
});
26+
}
27+
28+
29+
//routes
30+
app.get('/', (req, resp) => {
31+
resp.send('Hello KC')
32+
});
33+
34+
app.get('/access_token', access, (req,resp) => {
35+
resp.status(200).json({access_token: req.access_token})
36+
});
37+
38+
app.get('/register', access, (req, resp) => {
39+
let url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl';
40+
let auth = 'Bearer ' + req.access_token;
41+
42+
request({
43+
url: url,
44+
method: 'POST',
45+
headers: {
46+
'Authorization': auth
47+
},
48+
json: {
49+
"ShortCode": "600610",
50+
"ResponseType": "Complete",
51+
"ConfirmationURL": "http://192.168.100.4:5007/confirmation",
52+
"ValidationURL": "http://192.168.100.4:5007/validation_url"
53+
}
54+
}, (error, response, body) => {
55+
if(error){
56+
console.log(error)
57+
} else {
58+
resp.status(200).json(body);
59+
}
60+
});
61+
});
62+
63+
app.post('/confirmation', (req,resp) => {
64+
console.log(' ...confirmation... ')
65+
console.log(resp.client)
66+
});
67+
68+
app.post('/validation_url', (req,resp) => {
69+
console.log(' ...validation... ')
70+
console.log(req.body)
71+
console.log(req)
72+
73+
74+
});
75+
76+
app.get('/simulate', access, (req, resp) => {
77+
let url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate';
78+
let auth = 'Bearer ' + req.access_token;
79+
80+
request({
81+
url: url,
82+
method: 'POST',
83+
headers: {
84+
'Authorization': auth
85+
},
86+
json: {
87+
"ShortCode":"600610",
88+
"CommandID":"CustomerPayBillOnline",
89+
"Amount":"2430",
90+
"Msisdn":"254708374149",
91+
"BillRefNumber":"TestAPI"
92+
}
93+
}, (error, response, body) => {
94+
if(error){
95+
console.log(error);
96+
} else {
97+
resp.status(200).json(body);
98+
}
99+
});
100+
});
101+
102+
app.get('/balance', access, (req, resp) => {
103+
let url = 'https://sandbox.safaricom.co.ke/mpesa/accountbalance/v1/query';
104+
let auth = 'Bearer ' + req.access_token;
105+
106+
request({
107+
url: url,
108+
method: 'POST',
109+
headers: {
110+
'Authorization': auth
111+
},
112+
json: {
113+
"Initiator":"test2",
114+
"SecurityCredential":"dG/Hkr6H/Yd3dtJEyHK3tUD8YeE96dfsQ27Uv43vFjnevp7YTvtMCrmdlBSN/zd4dGVAWsNT8pvXuK4/r6UaLATCUyE/hnJYP89mhgm/8O8E2r77VjOYOhSbJVAV9qPm+9v/ZoDGBHWJA6bfTgZoHbJ3WcFc+P1YXW34WAzbNssfWD46NU23x8Goy7wk/PsT8hn+Dx2JphbpYTCLbK20DHBk32l3K9AIwcry1afQowqS+HSVxLAU/ZfRvT82+aHUYlPJrTIvzltgRIj1PVSKFwL98dCDjvB+lGXtFdp1bDFWxbLAOBS5DJSu/wpfWPRMvfnYiWzoeGdOi34rkGltBA==",
115+
"CommandID":"AccountBalance",
116+
"PartyA":"600610",
117+
"IdentifierType":"4",
118+
"Remarks":"Remarks",
119+
"QueueTimeOutURL":"http://192.168.100.4:5007/timeout_url",
120+
"ResultURL":"http://192.168.100.4:5007/result_url"
121+
}
122+
}, (error, response, body) => {
123+
if(error){
124+
console.log(error);
125+
} else {
126+
resp.status(200).json(body);
127+
}
128+
});
129+
})
130+
131+
app.post('/timeout_url', (req, resp) => {
132+
console.log('...balance...');
133+
console.log(req.body);
134+
});
135+
136+
app.post('/result_url', (req, resp) => {
137+
console.log('...balance...');
138+
console.log(JSON.stringify(req.body.Result.ResultParameters));
139+
});
140+
141+
app.get('/stk', access, (req, resp) => {
142+
let url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
143+
let auth = 'Bearer ' + req.access_token
144+
145+
let dateNow = new Date();
146+
const timestamp = dateNow.getFullYear() + "" + "" + "0" + "" + "" + dateNow.getMonth() + "" + "" + dateNow.getDate() + "" + "" + dateNow.getHours() + "" + "" + dateNow.getMinutes() + "" + "" + dateNow.getMilliseconds();
147+
const password = new Buffer.from('174379' + 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919' + timestamp).toString('base64');
148+
console.log(timestamp)
149+
request({
150+
url: url,
151+
method: 'POST',
152+
headers: {
153+
'Authorization': auth
154+
},
155+
json: {
156+
"BusinessShortCode": "174379",
157+
"Password": password,
158+
"Timestamp": timestamp,
159+
"TransactionType": "CustomerPayBillOnline",
160+
"Amount": "1",
161+
"PartyA": "254701720503",
162+
"PartyB": "174379",
163+
"PhoneNumber": "254701720503",
164+
"CallBackURL": "http://192.168.100.4:5007/callback",
165+
"AccountReference": "account",
166+
"TransactionDesc": "test" ,
167+
}
168+
}, (error, response, body) => {
169+
if(error){
170+
console.log(error);
171+
} else {
172+
resp.status(200).json(body);
173+
}
174+
});
175+
});
176+
177+
app.post('/callback', (req,resp) => {
178+
console.log('....lipa na m-pesa....');
179+
console.log(JSON.stringify(req.body.Body.stkCallback));
180+
})
181+
182+
183+
184+
185+
//listen
186+
app.listen(5007, (err, live) => {
187+
if(err){
188+
console.log(err)
189+
}
190+
console.log('Server running on port 5007')
191+
});

0 commit comments

Comments
 (0)