-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaraja.mjs
65 lines (52 loc) · 1.96 KB
/
daraja.mjs
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
import axios from 'axios'
// import express from 'express'
import { configDotenv } from 'dotenv'
configDotenv('./.env');
const baseURL = process.env.BASE_URL;
const consumerKey = process.env.CONSUMER_KEY;
const consumerSecret = process.env.CONSUMER_SECRET;
// function to get accestoken
const getAccessToken = async () => {
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString('base64');
const response = await axios.get(`${baseURL}/oauth/v1/generate?grant_type=client_credentials`, {
headers: {
Authorization: `Basic ${auth}`,
}
});
return response.data.access_token;
}
const initiateMpesaPayment = async (phoneNumber, amount) => {
const accessToken = await getAccessToken();
const timestamp = new Date().toISOString().replace(/[-T:.Z]/g, '').slice(0, 14);
const shortcode = process.env.SHORT_CODE;
const passkey = process.env.PASSKEY;
// the password is made up of the shortcode, passkey & timestamp
const password = Buffer.from(`${shortcode}${passkey}${timestamp}`).toString('base64');
// Payload which has all the require data for M-pesa api structure
const payload = {
BusinessShortCode: shortcode,
Password: password,
Timestamp: timestamp,
TransactionType: 'CustomerPayBillOnline',
Amount: amount,
PartyA: phoneNumber,
PartyB: shortcode,
PhoneNumber: phoneNumber,
CallBackURL: process.env.CALLBACK_URL,
AccountReference: 'Test123',
TransactionDesc: 'Payment for services',
};
const response = await axios.post(`${baseURL}/mpesa/stkpush/v1/processrequest`, payload, {
headers: {
Authorization: `Bearer ${accessToken}`,
}
});
console.log(response);
console.log(response.data);
return response.data;
};
// test number from daraja +254708374149
// set amount
initiateMpesaPayment(+254708374149, 10);
export { initiateMpesaPayment };
// TODO:error handling