PHPackages                             kahsaygt/mpesa-sdk-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. kahsaygt/mpesa-sdk-php

ActiveLibrary[API Development](/categories/api)

kahsaygt/mpesa-sdk-php
======================

A PHP SDK for M-PESA API integration

10PHP

Since May 9Pushed 1y ago2 watchersCompare

[ Source](https://github.com/kahsay-GT/mpesa-sdk-php)[ Packagist](https://packagist.org/packages/kahsaygt/mpesa-sdk-php)[ RSS](/packages/kahsaygt-mpesa-sdk-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

M-PESA PHP SDK Integration 🚀
============================

[](#m-pesa-php-sdk-integration-)

Easily integrate M-PESA APIs into your PHP or Laravel applications using the `kahsaygt/mpesa-sdk-php` package.🌟

Features ✨
==========

[](#features-)

- 🔑 **Easy Authentication**: Generate access tokens effortlessly.
- 📲 **STK Push (NI Push)**: Initiate customer-to-business payments with USSD prompts.
- 🏪 **C2B Support**: Register URLs and handle customer payments.
- 💸 **B2C Payouts**: Process business-to-customer disbursements.
- 🧩 **Modular Design**: Well-structured and extensible codebase.
- 🚨 **Error Handling**: Robust, developer-friendly error management.

---

Installation 📦
--------------

[](#installation-)

Install the SDK via Composer:

```
composer require kahsaygt/mpesa-sdk-php:dev-main
```

### Using a Custom Repository 🌍

[](#using-a-custom-repository-)

Since this package is hosted on GitHub, add the repository to your `composer.json`:

```
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/kahsay-GT/mpesa-sdk-php.git"
    }
],
"require": {
    "kahsaygt/mpesa-sdk-php": "dev-main"
},

"minimum-stability": "dev"
```

Then run:

```
composer update
```

### Laravel (Optional)

[](#laravel-optional)

If using Laravel, publish the configuration file (if supported by your SDK):

```
php artisan vendor:publish --tag=mpesa-config
```

---

Requirements ✅
--------------

[](#requirements-)

- 🐘 PHP 7.4 or higher
- 🌐 cURL extension enabled
- 📥 Composer

---

Usage 🛠️
--------

[](#usage-️)

This SDK provides a `Client` class for API interactions and service classes for each M-PESA API endpoint. Below are usage examples for authentication and various API operations.

### 1. Authentication 🔐

[](#1-authentication-)

All API requests require an access token. Use `Authentication.php` to authenticate a `Client` instance:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php'; // Adjust path as needed

use Mpesa\Sdk\Client;

$client = getClient();
authenticate($client, __DIR__ . '/logs/auth.log');

// The client is now ready for API requests
```

### 2. STK Push (Lipa Na M-PESA Online) 📱

[](#2-stk-push-lipa-na-m-pesa-online-)

Initiate an STK Push payment request using `StkPushService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/StkPushService.php';

use Ramsey\Uuid\Uuid;

$service = new StkPushService();
$service->run();
```

### 3. Account Balance Query 📊

[](#3-account-balance-query-)

Check your account balance using `AccountBalanceService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/AccountBalanceService.php';

$service = new AccountBalanceService();
$service->run();
```

### 4. B2C Payout 💸

[](#4-b2c-payout-)

Initiate a B2C payout using `B2CPayOutService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/B2CPayOutService.php';

$service = new B2CPayOutService();
$service->run();
```

### 5. C2B URL Registration 🌐

[](#5-c2b-url-registration-)

Register C2B callback URLs using `C2BRegisterService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/C2BRegisterService.php';

$service = new C2BRegisterService();
$service->run();
```

### 6. Transaction Status Query 🔍

[](#6-transaction-status-query-)

Query transaction status using `TransactionStatusService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/TransactionStatusService.php';

$service = new TransactionStatusService();
$service->run();
```

### 7. Transaction Reversal 🔄

[](#7-transaction-reversal-)

Reverse a transaction using `TransactionReversalService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/TransactionReversalService.php';

$service = new TransactionReversalService();
$service->run();
```

### 8. B2C Simulation 🧪

[](#8-b2c-simulation-)

Simulate a B2C transaction using `SimulateTransactionService`:

```
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Authentication.php';
require __DIR__ . '/SimulateTransactionService.php';

$service = new SimulateTransactionService();
$service->run();
```

---

Configuration in pure PHP ⚙️
----------------------------

[](#configuration-in-pure-php-️)

The `Authentication.php` file centralizes configuration. Customize it with:

```
$config = [
    'base_url' => 'https://apisandbox.safaricom.et/mpesa/',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];
$securityCredential = 'your_security_credential';
```

Configuration in Laravel ⚙️
---------------------------

[](#configuration-in-laravel-️)

Configuration is managed via `config/mpesa.php` in Laravel projects. After publishing the config file with `php artisan vendor:publish --tag=mpesa-config`, customize it with your credentials. Use environment variables in your `.env` file:

```
APP_ENV=development #production
DEV_MPESA_BASE_URL=https://apisandbox.safaricom.et/
DEV_MPESA_CONSUMER_KEY=your_consumer_key
DEV_MPESA_CONSUMER_SECRET=your_consumer_secret
DEV_SECURITY_CREDENTIAL=your_security_credential

PROD_MPESA_BASE_URL=https://apisandbox.safaricom.et/
PROD_MPESA_CONSUMER_KEY=your_consumer_key
PROD_MPESA_CONSUMER_SECRET=your_consumer_secret
PROD_SECURITY_CREDENTIAL=your_security_credential

---

## Laravel Integration ⚙️

In a Laravel controller:

```php
