Closed Alecckie closed 5 years ago
You can do a simple print_r($data)
to see all the elements in that response array
Then I'm constantly getting this error whenever I call the
$data['response_body'] = $this->mpesa_callbacks->processC2BRequestValidation();
...
It makes the whole validation function fail
You can do a simple
print_r($data)
to see all the elements in that response array
print_r($data); returns an empty array
Mpesa response with ResultCode: 1
means Insufficient Funds.
Mpesa response with
ResultCode: 1
means Insufficient Funds.
Check a detailed description of all possible responses here https://developer.safaricom.co.ke/docs?php#m-pesa-result-and-response-codes
In this case, the resulcode is hardored for this testing. I think the main error is in the callBack function
$data['response_body'] = $this->mpesa_callbacks->processC2BRequestValidation();
print_r($data);
$amount= $data['0']["transAmount"];
if($amount == '1030.00'){
echo '{"ResultCode":0, "ResultDesc":"Payment validated successfully", "ThirdPartyTransID": 0}';
}
else{
echo '{"ResultCode":1, "ResultDesc":"Reject payment", "ThirdPartyTransID": 0}';
}
Is this now working for you? Let me know or just create a pull request and I'll review it then merge.
This is what worked for me. I opted not to use the callback but you can use this to see how to make the callback work `function validate_customer_to_business(){ header("Content-Type:application/json");
function validate_customer_to_business(){
header("Content-Type:application/json");
$callbackJSONData = file_get_contents('php://input');
$callbackData = json_decode($callbackJSONData,true);
$transactionType = $callbackData["TransactionType"];
$transID = $callbackData["TransID"];
$transTime = $callbackData["TransTime"];
$transAmount = $callbackData["TransAmount"];
$businessShortCode = $callbackData["BusinessShortCode"];
$billRefNumber = $callbackData["BillRefNumber"];
$invoiceNumber = $callbackData["InvoiceNumber"];
$orgAccountBalance = $callbackData["OrgAccountBalance"];
$thirdPartyTransID = $callbackData["ThirdPartyTransID"];
$MSISDN = $callbackData["MSISDN"];
$firstName = $callbackData["FirstName"];
$middleName = $callbackData["MiddleName"];
$lastName = $callbackData["LastName"];
//The log (Very optional...used it just to see the actual response from saf)
$logFile= 'mpesa_validation_c2b.txt';
//Write to file
$log=fopen($logFile,"a+");
fwrite($log,$callbackJSONData);
fclose($log);
if($transAmount == "1200.00"){ //For testing purposes...in production, we will perform actual validation
echo '{"ResultCode":0, "ResultDesc":"Payment validated successfully", "ThirdPartyTransID": 0}';
}
else{
echo '{"ResultCode":1, "ResultDesc":"Reject payment", "ThirdPartyTransID": 0}';
}
}`
This was the response of the c2b simulation on validation using the above function
Hello Alex, the callback function works as expected. Yours also works as expected. After viewing your code, I noticed that the error came from how the json_decode
function was being invoked. The callback decodes the response body into an object but you tried to access it as an associative array. To decode the array into an associative array, pass a second argument to the function call i.e json_decode($response_data, true)
Following your tutorial, this is how you get data from the mpesa_callbacks to our controller n then to model
public function c2b() { $data['response_body'] = $this->mpesa_callbacks->processSTKPushRequestCallback(); $this->test_model->insert($data); }
Now, if I wanted to use one of the variables to actually perform my validation before sending a response to confirm or reject payment, how would I extract that info from the variable $data
Like maybe $sent_pay = $data->$transAmount;