$curl_post_data = [
'InitiatorName' => 'testapi',
'InitiatorPassword' => 'Safaricom999!*!',
'BusinessShortCode' => '174379',
'Password' => $password,
'Timestamp' => $timestamp,
'TransactionType' => 'CustomerPayBillOnline',
'Amount' => $amount,
'PartyA' => '600584',
'PartyB' => 174379,
'PhoneNumber' => $phoneNumber,
'CallBackURL' => 'https://7db63514dbd1.ngrok.io/api/stk/push/callback/url',
'TransactionDesc' => "lipa Na M-PESA",
'TransactionDesc' => "lipa Na M-PESA",
];

My Response is:-

stdClass Object
(
[MerchantRequestID] => 9291-16554920-2
[CheckoutRequestID] => ws_CO_28072022090938484708374149
[ResponseCode] => 0
[ResponseDescription] => Success. Request accepted for processing
[CustomerMessage] => Success. Request accepted for processing
) 

Then I check with this API STKPushQuery

$curl_post_data = array(
'BusinessShortCode' => $businessShortCode,
'Password' => $password,
'Timestamp' => $timestamp,
'CheckoutRequestID' => $checkoutRequestID
);

Response is :-

{
"requestId":"ws_CO_28072022091256773708374149",
"errorCode": "500.001.1001",
"errorMessage": "The transaction is being processed"
}

and some times this

{
"ResponseCode": "0",
"ResponseDescription":"The service request has been accepted successsfully",
"MerchantRequestID":"1281-60777699-1",
"CheckoutRequestID":"ws_CO_28072022091306335708374149",
"ResultCode": "1037",
"ResultDesc":"DS timeout user cannot be reached"
}

My Register and callback url where i am not getting anything

public function mpesaConfirmation(Request $request)
{
$content=json_decode($request->getContent());         
$mpesa_transaction = new MpesaTrx();
$mpesa_transaction->TransactionType = $content->TransactionType;
$mpesa_transaction->TransID = $content->TransID;
$mpesa_transaction->TransTime = $content->TransTime; 
$mpesa_transaction->TransAmount = $content->TransAmount;
$mpesa_transaction->BusinessShortCode = $content->BusinessShortCode;
$mpesa_transaction->BillRefNumber = $content->BillRefNumber;
$mpesa_transaction->InvoiceNumber = $content->InvoiceNumber;
$mpesa_transaction->OrgAccountBalance = $content->OrgAccountBalance;
$mpesa_transaction->ThirdPartyTransID = $content->ThirdPartyTransID;
$mpesa_transaction->MSISDN = $content->MSISDN;
$mpesa_transaction->FirstName = $content->FirstName;
$mpesa_transaction->MiddleName = $content->MiddleName;
$mpesa_transaction->LastName = $content->LastName;
$mpesa_transaction->save();
// Responding to the confirmation request
$response = new Response();
$response->headers->set("Content-Type","text/xml; charset=utf-8");
$response->setContent(json_encode(["C2BPaymentConfirmationResult"=>"Success"]));
return $response;
}

public function mpesaRegisterUrls()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization: Bearer '. $this->generateAccessToken()));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
'ShortCode' => "600986",
'ResponseType' => 'Completed',
'ConfirmationURL' => "https://****.****.in/api/confirmation",
'ValidationURL' => "https://****.****.in/api/validation"
)));
    $curl_response = curl_exec($curl);
    echo $curl_response;
}

I am using this code for payment 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest' using this api. I am not getting in my confirmation url not working as I define my all usefull code here.

1

There are 1 best solutions below

0
On

The url should end with .json, ie 'https://7db63514dbd1.ngrok.io/api/stk/push/callback/url.json'