Download the PHP package levizwannah/mpesa-sdk-php without Composer
On this page you can find all versions of the php package levizwannah/mpesa-sdk-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download levizwannah/mpesa-sdk-php
More information about levizwannah/mpesa-sdk-php
Files in levizwannah/mpesa-sdk-php
Package mpesa-sdk-php
Short Description An independent Mpesa SDK for PHP. This package provides a clean implementation of the Safaricom Mpesa API for PHP applications.
License MIT
Informations about the package mpesa-sdk-php
Mpesa SDK for PHP
A Clean, Elegant, and Independent PHP SDK for interacting with Safaricom Daraja APIs. The SDK is purely object-oriented and easy to understand and use. It has no dependency and hence can be used in any PHP project: plain or framework-based.
Requirement
- PHP version >= 7.4
- You have read the documentation of the Daraja API: This SDK mainly abstracts everything for you and makes it easy to integrate M-Pesa. So, ensure you have looked at the Daraja documentation. Not to understand it, but just know what data are being sent and received. That makes it easy to understand this SDK.
Get it with Composer
And then start using it.
not using composer
Download the zip version of the code. Include the self-autoload.php
file located in the downloaded src
folder.
Documentation
Setting Up
Firstly create an Mpesa object with the necessary configurations.
The consumer key, consumer secret, and business short code are always required.
If you are using a till number, then the till
key is required, otherwise only the business short code is required.
Note: The business shortcode is the same as the Paybill number. For till numbers, it is different.
Method chaining vs configure($configure)
Every key in the $config
array is a setter method for the object. For example, the below code is equivalent to the one above.
Every object for interacting with a specific API extends the parent Mpesa object. Therefore, the same rule applies to them. You can use a config array and the configure method or just use the individual setter methods.
Warning: Do not directly create an object from a child class, always use the mpesa object to get the child object as you will see later in the doc.
Environment
You can use the 'env'
key in the $config
array or the env("env")
method to set the environment. The environment value can be live
or sandbox
. By default, the environment is "live"
. There is a Constant
class to save you from writing the literal strings.
Remember: Every setter method used below can be used as a key in the
$config
array and set using theconfigure($config)
method.
Phone Numbers
The SDK adds 254 to the phone number. The below formats are supported
Exceptions
The SDK throws an exception if a required data is missing during requests. For example, the initiator name and security credentials are required before making Reversal requests. If they are not found, an exception is thrown with a semantic message telling you what the problem is.
Handling Responses
The SDK provides a uniform way to check the immediate Mpesa responses. Use the below snippet when interacting with any API using the SDK.
Note: The data in the response object is the same as it is in the Daraja Documentation. There is no additional formatting done.
Security in Handling Callbacks
The SDK has two static helper methods to help you when handling mpesa responses in your callbacks.
verifyOrigin(): bool
: returns atrue
if the callback response is from Mpesa andfalse
otherwise.data($asArray = false): object|array
: gets the callback data from mpesa and returns it as an object or array based on its argument.
Handling Callback Payloads
Note:
Response
represents what you get from Mpesa when you make a request.Result
is the payload sent to your callback URLs.
Payload format
Please refer to the Daraja documentation at https://developer.safaricom.co.ke to see the expected payload. For forward compatibility, the SDK doesn't alter the responses or payload from Mpesa.
The confusing part
In every Response
, there will be unique keys. For example, the MerchantRequestID
and CheckoutRequestID
in the STK push response, and the OriginatorConversationID
and ConversationID
in the other APIs responses. These keys identify the transaction on Mpesa. Save these keys in your database or some storage alongside the pending transaction.
In the Result
payload that will be sent to your callbacks, these keys will be present. Therefore, you can use them to update the corresponding transactions in your storage or database.
Mpesa Express (STK Push) API
Used for initiating STK Push requests.
Requirements
Ensure these values were set as shown in the setup section:
- Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
); - Till Number (
till
) For till numbers - Passkey (
passkey
)
Usage
Notice that the difference between using Till number and paybill number is the use of paybill()
and buygoods()
methods before calling the push()
method. Also, ensure that till
value is set when using Till numbers.
Mpesa Express STK Query
Use for querying the status of STK push requests. This is different from normal transaction status queries.
Requirements
Ensure these values were set as shown in the setup section:
- Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
); - Passkey (
passkey
) - Till (
till
) for till accounts
Usage
See the code snippet below
Note: Use
paybill()
if you are querying an STK request made for a paybill number, otherwise usebuygoods()
. By default, it queries for STK requests made for paybill numbers.
C2B URLs Registration API
Enables you to register your C2B URLs. The SDK also provides an easy response method for your confirmation and validation scripts.
The
validation url
is not required unless you explicitly ask the Mpesa team to enable it for you.
Requirements
Ensure these values were set as shown in the setup section:
- Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Registration
Look at the code snippet below.
Callback helpers
When Mpesa sends a payload to your confirmation or validation URLs, you need to send a formatted confirmation or denial payload. You can use the SDKs static methods for that. See below.
Note: You can only use
Mpesa::deny()
in the validation handler.
Reversal API
Enables you to make Reversals of Mpesa Transactions.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
See the code snippet on how to use this SDK.
In your callback scripts, please ensure to follow the recommendation in the security section of this doc.
Transaction Query API
The Transaction query API enables you to check the statuses of transactions made to or by your business shortcode.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
See the code snippet below on how to use this SDK.
Balance Query API
The balance Query API allows you to query the balance in a business account.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
See the code snippet
B2B API
The B2B API allows you to make payments to paybill or till numbers from your business short code.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
Look at the code snippet below
B2C API
The B2C API allows you to make payments to mobile numbers from your business short code.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
See the code snippet below:
Tax Remittance API
The Tax Remittance API allows you to make Tax payment to KRA
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
See the code snippet below
Dynamic QR Code API
Enables you to generate QR codes for different transactions. Please see the Daraja documentation
Use this API to generate a Dynamic QR which enables Safaricom M-PESA customers who have My Safaricom App or M-PESA app, to scan and capture till number and amount and then authorize to pay for goods and services at select LIPA NA M-PESA (LNM) merchant outlets.
-- Daraja
Requirements
Ensure these values were set as shown in the setup section:
- Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
)
Usage
See the snippet below
Business To Bulk API
This API enables you to load funds from a working account directly to a utility account for B2C payments.
Requirements
Ensure these values were set as shown in the setup section:
- Initiator name (
initiator
) - Security credential (
credential
) - Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
Mpesa Ratiba (Subscription)
This API enables you to create Mpesa Standing Orders. Take it as a subscription API for Mpesa
Requirements
Ensure these values were set as shown in the setup section:
- Consumer Key (
key
) - Consumer Secret(
secret
) - Business Short Code (
code
);
Usage
Quick Note
If you are confused on how to handle the results in the callback, please read the earlier sections of this README file.
Reporting Errors
Please open an issue in case there is a bug found.
Show Love
- Star this repository
- if you love it, buy me coffee.