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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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

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 the configure($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.

  1. verifyOrigin(): bool: returns a true if the callback response is from Mpesa and false otherwise.
  2. 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:

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:

Usage

See the code snippet below

Note: Use paybill() if you are querying an STK request made for a paybill number, otherwise use buygoods(). 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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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


All versions of mpesa-sdk-php with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package levizwannah/mpesa-sdk-php contains the following files

Loading the files please wait ....