PHPackages                             ongudidan/mpesa-sdk - 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. ongudidan/mpesa-sdk

ActiveLibrary

ongudidan/mpesa-sdk
===================

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 http://developer.safaricom.co.ke.

v1.0.5(2mo ago)077↓100%PHP

Since Apr 21Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/ongudidan/mpesa-sdk)[ Packagist](https://packagist.org/packages/ongudidan/mpesa-sdk)[ RSS](/packages/ongudidan-mpesa-sdk/feed)WikiDiscussions main Synced today

READMEChangelog (6)DependenciesVersions (7)Used By (0)

M-Pesa SDK for PHP
==================

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

**Overview**

`mpesa-sdk` is a lightweight PHP SDK that simplifies integration with Safaricom's M-Pesa Daraja API. It supports B2C, B2B, C2B, STK Push, reversals, transaction status, account balance, and more.

This SDK is **framework-agnostic**, meaning you can use it with **Laravel, Symfony, Yii, CodeIgniter, or even plain/vanilla PHP**.

---

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

[](#-installation)

Install via Composer:

```
composer require ongudidan/mpesa-sdk
```

---

⚙️ Configuration
----------------

[](#️-configuration)

You'll need:

- Consumer Key &amp; Consumer Secret — from [Safaricom Developer Portal](https://developer.safaricom.co.ke/)
- An M-Pesa API User (Initiator) created from your Safaricom Business account
- Password for that Initiator

You can pass credentials directly in the data array or load them using environment variables/configuration.

---

🚀 Usage Examples
----------------

[](#-usage-examples)

---

### ✅ B2C (Business to Customer)

[](#-b2c-business-to-customer)

Send funds from your BusinessShortCode to a customer's phone.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'Initiator' => 'API user created on Safaricom Business dashboard',
    'InitiatorPassword' => 'Password of the API user',
    'CommandID' => 'BusinessPayment', // or SalaryPayment, PromotionPayment
    'Amount' => '100', // Amount to send
    'PartyA' => '600XXX', // Your BusinessShortCode
    'PartyB' => '2547XXXXXXXX', // Customer phone number
    'Remarks' => 'Salary payment',
    'QueueTimeOutURL' => 'https://yourdomain.com/timeout',
    'ResultURL' => 'https://yourdomain.com/result',
    'Occasion' => 'JunePayroll',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->b2c($data);
```

---

### 💼 Account Balance

[](#-account-balance)

Check your Paybill/Till balance.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'IdentifierType' => '4', // 1: BusinessShortCode, 2: Till, 4: Org
    'PartyA' => '600XXX', // Your BusinessShortCode or organization ID
    'Initiator' => 'API user created on dashboard',
    'InitiatorPassword' => 'Password of the API user',
    'QueueTimeOutURL' => 'https://yourdomain.com/timeout',
    'ResultURL' => 'https://yourdomain.com/result',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->accountBalance($data);
```

---

### 🔍 Transaction Status

[](#-transaction-status)

Check the status of a transaction.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'TransactionID' => 'OEI2AK4Q16', // M-Pesa transaction ID
    'PartyA' => '600XXX', // Your BusinessShortCode
    'IdentifierType' => '4', // 1: MSISDN (Phone number), 2: Till 4: Paybill, etc.
    'Initiator' => 'API user',
    'InitiatorPassword' => 'Password of the API user',
    'ResultURL' => 'https://yourdomain.com/result',
    'QueueTimeOutURL' => 'https://yourdomain.com/timeout',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->transactionStatus($data);
```

---

### 🏢 B2B (Business to Business)

[](#-b2b-business-to-business)

Send money from your organization to another.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'Initiator' => 'API user',
    'InitiatorPassword' => 'Password of the API user',
    'CommandID' => 'BusinessPayBill', // or MerchantToMerchantTransfer
    'SenderIdentifierType' => '4',
    'RecieverIdentifierType' => '4',
    'Amount' => '1000',
    'PartyA' => '600XXX', // Your BusinessShortCode
    'PartyB' => '600YYY', // Recipient BusinessShortCode
    'AccountReference' => 'Invoice#234',
    'Remarks' => 'B2B Payment',
    'QueueTimeOutURL' => 'https://yourdomain.com/timeout',
    'ResultURL' => 'https://yourdomain.com/result',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->b2b($data);
```

---

### 👥 C2B (Customer to Business Simulation)

[](#-c2b-customer-to-business-simulation)

Simulate a customer payment to your BusinessShortCode.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'BusinessShortCode' => '600XXX',
    'CommandID' => 'CustomerPayBillOnline', // or CustomerBuyGoodsOnline
    'Amount' => '500',
    'Msisdn' => '2547XXXXXXXX', // Customer phone
    'BillRefNumber' => 'INV2024',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->c2b($data);
```

---

### 📲 STK Push (Lipa Na M-Pesa)

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

Trigger a payment prompt on a customer's phone.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'BusinessShortCode' => '174379',
    'LipaNaMpesaPasskey' => 'your_lnm_passkey',
    'TransactionType' => 'CustomerPayBillOnline',
    'Amount' => '100',
    'PartyA' => '2547XXXXXXXX', // Customer phone
    'PartyB' => '174379', // Your BusinessShortCode
    'PhoneNumber' => '2547XXXXXXXX',
    'CallBackURL' => 'https://yourdomain.com/callback',
    'AccountReference' => 'Ref001',
    'TransactionDesc' => 'Payment for services',
    'Remarks' => 'Online Payment',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->STKPushSimulation($data);
```

---

### 📡 STK Push Query

[](#-stk-push-query)

Query the status of an STK Push request.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'CheckoutRequestID' => 'ws_CO_123456789',
    'BusinessShortCode' => '174379',
    'LipaNaMpesaPasskey' => 'your_lnm_passkey',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->STKPushQuery($data);
```

---

### 🔄 Reversal

[](#-reversal)

Reverse a transaction.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'CommandID' => 'TransactionReversal',
    'TransactionID' => 'OEI2AK4Q16',
    'Amount' => '100',
    'ReceiverParty' => '600XXX', //your BusinessShortCode
    'RecieverIdentifierType' => '11', //11: when using BusinessShortCode
    'ResultURL' => 'https://yourdomain.com/result',
    'QueueTimeOutURL' => 'https://yourdomain.com/timeout',
    'Remarks' => 'Refund for duplicate payment',
    'Occasion' => 'ErroneousPayment',
    'Initiator' => 'API user',
    'InitiatorPassword' => 'Password of the API user',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->reversal($data);
```

---

### 🔐 Register Validation &amp; Confirmation URLs

[](#-register-validation--confirmation-urls)

Register endpoints to handle C2B payments.

```
$mpesa = new \Mpesa\Mpesa();

$data = [
    'BusinessShortCode' => '600XXX',
    'ResponseType' => 'Completed',
    'ConfirmationURL' => 'https://yourdomain.com/confirmation',
    'ValidationURL' => 'https://yourdomain.com/validation',
    'environment' => 'sandbox or live',
    'consumer_key' => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
];

$response = $mpesa->registerUrl($data);
```

---

### 📥 Handle Callback Data

[](#-handle-callback-data)

#### Get Data from Callback:

[](#get-data-from-callback)

```
$mpesa = new \Mpesa\Mpesa();
$data = $mpesa->getDataFromCallback();
```

#### Send Callback Response:

[](#send-callback-response)

```
$mpesa = new \Mpesa\Mpesa();
$mpesa->finishTransaction(); // success
$mpesa->finishTransaction(false); // failure
```

---

### 📥 Handling M-PESA Daraja Callback Responses

[](#-handling-m-pesa-daraja-callback-responses)

To log the response from any M-PESA Daraja callback (e.g., `C2B Confirmation`, `C2B Validation`, `STK Push`, etc.), you can use the following snippet in your `callback.php` (or any relevant endpoint). This helps with debugging and record-keeping.

```
