Am trying to save MPesa Callback data, when a user has paid with Lipa Na Mpesa, ineed to save the CheckoutRequestID, with a decoded username from token, then if the payment is successful the cllback URL needs to update the associated CheckoutRequestID from mysql with the amount, from there it needs to update the user's wallet with the new value. Currently the code works well, but I cannot save, when I try to access the username or any value the callback doesn't return any data. Here is the LipaNaMpesa code and the STK Callback code.
LipaNaMpesa code
router.get('/stk', middleware.access, middleware.checkToken, (req, res) => {
let endpoint = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
let auth = "Bearer " + req.access_token
let datenow = new Date()
//console.log(datenow.getUTCDate())
const timestamp = datenow.getFullYear() +"" + addZero((datenow.getMonth()+1))+"" +addZero(datenow.getUTCDate())+ ""+ addZero(datenow.getHours())+"" + addZero(datenow.getMinutes())+"" + addZero(datenow.getSeconds())
// console.log(timestamp)
const password = (new Buffer.from('174379' + 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919' + timestamp).toString('base64'))
request(
{
url: endpoint,
method: "POST",
headers: {
"Authorization": auth
},
json: {
"BusinessShortCode": 174379,
"Password": password,
//"MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjExMjExMDcyODAw"
"Timestamp": timestamp,
"TransactionType": "CustomerPayBillOnline",
"Amount": 1,
"PartyA": 254792482180,//254792482180,
"PartyB": 174379,
"PhoneNumber": 254792482180,//254704148972,
"CallBackURL": "https://9b68-41-80-113-243.ngrok.io/mrequests/stk_confirm",
"AccountReference": "GoChama",
"TransactionDesc": "Wallet Deposit"
}
},
function (error, response, body) {
if (error) {
console.log(error)
}
else {
// console.log(body.CheckoutRequestID)
//console.log(req.decoded.username)
res.status(200).json(body)
}
}
)
})
STK CallBAck Code
router.post('/stk_confirm', (req, res) => {
console.log('....................... stk_confirm .............')
console.log("Payload Received", req.body.Body.stkCallback)
/* const callbackData = req.body.Body.stkCallback
console.log("Payload Received", callbackData)
var resultCode = callbackData.ResultCode;
var checkoutId = callbackData.CheckoutRequestID
var username = req.decoded.username
if(resultCode === 0){
const details = callbackData.CallbackMetadata.Item
var mReceipt;
var mPhoneNumber;
var mAmount;
await details.forEach(entry =>{
switch (entry.Name){
case "MpesaReceiptNumber":
mReceipt = entry.Value
break;
case "PhoneNumber":
mPhoneNumber = entry.Value
break;
case "Amount":
mAmount = entry.Value
break;
default:
break;
}
})
}*/
res.status(200).json(req.body)
})
The commented part f the code doesnt return any value when added. Is there something I am doing wrong?
This is how the Daraja API will work in reference to your problem. When you call the LipaNaMpesa Code and it executes the following response is returned.
You will save the CheckoutRequestID to the database at this point together with the decoded username from the token and anything else you would like to save. The LipaNaMpesa Code sends a prompt to the customer's phone and returns the above response.
If the customer does not enter their PIN or has an insufficient balance or for whatever reason, the transaction does not go through, the Callback URL does not return anything.
If the customer enters the PIN and pays, the Callback URL returns the following response
At this point now, you can update the DB row where you inserted CheckoutRequestID from LipaNaMpesa Code response. The CheckoutRequestID from both responses is the same.