A PHP package for Mpesa API
Introduction, (*1)
This package seeks to help php developers implement the various Mpesa APIs without much hustle. It is based on the REST API whose documentation is available on https://developer.safaricom.co.ke., (*2)
Installation using composer
composer require safaricom/mpesa
, (*3)
Configuration
At your project root, create a .env file and in it set the consumer key and consumer secret as follows
MPESA_CONSUMER_KEY= [consumer key]
MPESA_CONSUMER_SECRET=[consumer secret]
MPESA_ENV=[live or sandbox]
For Laravel users, open the Config/App.php file and add \Safaricom\Mpesa\MpesaServiceProvider::class
under providers and 'Mpesa'=> \Safaricom\Mpesa\MpesaServiceProvider::class
under aliases., (*4)
Remember to edit the consumer_key and consumer_secret values appropriately when switching between sandbox and live, (*5)
Usage, (*6)
Confirmation and validation urls, (*7)
B2C Payment Request, (*8)
This creates transaction between an M-Pesa short code to a phone number registered on M-Pesa., (*9)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*10)
$b2cTransaction=$mpesa->b2c($InitiatorName, $SecurityCredential, $CommandID, $Amount, $PartyA, $PartyB, $Remarks, $QueueTimeOutURL, $ResultURL, $Occasion);
, (*11)
Account Balance Request, (*12)
This is used to enquire the balance on an M-Pesa BuyGoods (Till Number), (*13)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*14)
$balanceInquiry=$mpesa->accountBalance($CommandID, $Initiator, $SecurityCredential, $PartyA, $IdentifierType, $Remarks, $QueueTimeOutURL, $ResultURL);
, (*15)
Transaction Status Request This is used to check the status of transaction., (*16)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*17)
$trasactionStatus=$mpesa->transactionStatus($Initiator, $SecurityCredential, $CommandID, $TransactionID, $PartyA, $IdentifierType, $ResultURL, $QueueTimeOutURL, $Remarks, $Occasion);
, (*18)
B2B Payment Request, (*19)
This is used to transfer funds between two companies., (*20)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*21)
$b2bTransaction=$mpesa->b2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber );
, (*22)
C2B Payment Request, (*23)
This is used to Simulate transfer of funds between a customer and business., (*24)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*25)
$b2bTransaction=$mpesa->c2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber );
, (*26)
Also important to note is that you should have registered validation and confirmation urls where the callback responses will be sent., (*27)
STK Push Simulation, (*28)
This is used to initiate online payment on behalf of a customer., (*29)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*30)
$stkPushSimulation=$mpesa->STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remarks);
, (*31)
STK Push Status Query, (*32)
This is used to check the status of a Lipa Na M-Pesa Online Payment., (*33)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*34)
$STKPushRequestStatus=$mpesa->STKPushQuery($checkoutRequestID,$businessShortCode,$password,$timestamp);
, (*35)
Callback Routes M-Pesa APIs are asynchronous. When a valid M-Pesa API request is received by the API Gateway, it is sent to M-Pesa where it is added to a queue. M-Pesa then processes the requests in the queue and sends a response to the API Gateway which then forwards the response to the URL registered in the CallBackURL or ResultURL request parameter. Whenever M-Pesa receives more requests than the queue can handle, M-Pesa responds by rejecting any more requests and the API Gateway sends a queue timeout response to the URL registered in the QueueTimeOutURL request parameter., (*36)
Obtaining post data from callbacks This is used to get post data from callback in json format. The data can be decoded and stored in a database., (*37)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*38)
$callbackData=$mpesa->getDataFromCallback();
, (*39)
Finishing a transaction After obtaining the Post data from the callbacks, use this at the end of your callback routes to complete the transaction, (*40)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*41)
$callbackData=$mpesa->finishTransaction();
, (*42)
If validation fails, pass false
to finishTransaction()
, (*43)
$mpesa= new \Safaricom\Mpesa\Mpesa();
, (*44)
$callbackData=$mpesa->finishTransaction(false);
, (*45)