Last active
May 8, 2023 23:16
-
-
Save NjiruClinton/b1bc6eab8fa35d9279fb1bd4c9fa17e5 to your computer and use it in GitHub Desktop.
mpesa stk-push for lipaNaMpesa online sandbox only, expressjs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const app = express(); | |
| const bodyParser = require('body-parser'); | |
| const request = require('request'); | |
| app.use(bodyParser.json()); | |
| app.listen(3000, () => { | |
| console.log('Server started on port 3000'); | |
| }); | |
| let accessToken = ''; | |
| const getAccessToken = () => { | |
| const username = 'your_consumer_key'; | |
| const password = 'your_consumer_secret'; | |
| const url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; | |
| const auth = 'Basic ' + new Buffer.from(username + ':' + password).toString('base64'); | |
| request( | |
| { | |
| method: 'GET', | |
| url: url, | |
| headers: { | |
| Authorization: auth | |
| } | |
| }, | |
| (error, response, body) => { | |
| if (error) { | |
| console.error(error); | |
| res.status(500).send('An error occurred while processing the payment.'); | |
| } else { | |
| console.log(body); | |
| accessToken = JSON.parse(body).access_token; | |
| console.log(accessToken); | |
| } | |
| } | |
| ); | |
| }; | |
| const pay = async () => { | |
| console.log('accessToken', accessToken); | |
| const payload = { | |
| BusinessShortCode: '174379', | |
| Password: | |
| 'MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTkxMjEzMTA1NzEz', | |
| Timestamp: '20191213105713', | |
| TransactionType: 'CustomerPayBillOnline', // or CustomerBuyGoodsOnline | |
| Amount: '1', | |
| PartyA: '2547...', // mobile number that will pay | |
| PartyB: '174379', | |
| PhoneNumber: '2547...', // mobile number that will pay | |
| CallBackURL: 'http://arbaaz.herokuapp.com/log.txt', // condider your own firebase functions callBackURL to monitor logs, can still work without it | |
| AccountReference: 'Clinton', | |
| TransactionDesc: 'Test' | |
| }; | |
| request( | |
| { | |
| method: 'POST', | |
| url: 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest', | |
| headers: { | |
| Authorization: `Bearer ${accessToken}`, | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify(payload) | |
| }, | |
| (error, response, body) => { | |
| if (error) { | |
| console.error(error); | |
| res.status(500).send('An error occurred while processing the payment.'); | |
| } else { | |
| console.log(body); | |
| // res.send(body); res is not defined | |
| } | |
| } | |
| ); | |
| }; | |
| function initializePayment (){ | |
| getAccessToken(); | |
| setTimeout(pay, 5000); | |
| } | |
| // you can export function here | |
| module.exports = { | |
| initializePayment | |
| } | |
| // uncomment this to execute with nodemon on the expressjs server | |
| // call getAccessToken to generate access_token | |
| // getAccessToken(); | |
| // call pay function after 5 seconds to use access_token generated by getAccessToken | |
| // setTimeout(pay, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment