Welcome to the Mpesa-sdk repository! This PHP library simplifies the integration with the M-Pesa API, allowing you to handle mobile money transactions with ease. Whether you're looking to implement STK Push, C2B, B2C, transaction status queries, or reversals, this library has you covered.
Check out the latest releases here!
- STK Push: Initiate payments directly from the customer’s mobile phone.
- C2B: Accept payments from customers to your business seamlessly.
- B2C: Send money from your business to customers easily.
- Transaction Status Queries: Check the status of your transactions in real-time.
- Reversals: Handle transaction reversals with minimal hassle.
To install the Mpesa-sdk, you can use Composer. Run the following command in your terminal:
composer require andreim2k/mpesa-sdk
After installation, include the library in your project:
require 'vendor/autoload.php';
To start using the SDK, create an instance of the Mpesa
class. You will need your credentials from Safaricom:
use Mpesa\Mpesa;
$mpesa = new Mpesa([
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
'shortcode' => 'YOUR_SHORTCODE',
'lipa_na_mpesa_online_shortcode' => 'YOUR_LNMO_SHORTCODE',
'lipa_na_mpesa_online_shortcode' => 'YOUR_LNMO_SHORTCODE',
'lipa_na_mpesa_online_shortcode' => 'YOUR_LNMO_SHORTCODE',
]);
To initiate a STK Push, use the following method:
$response = $mpesa->stkPush([
'phone' => '2547XXXXXXXX',
'amount' => 100,
'callback_url' => 'https://yourcallbackurl.com',
'account_reference' => 'Test123',
'transaction_desc' => 'Payment for testing',
]);
To accept a C2B payment, you can set up a listener for incoming payments. Here's a simple example:
$mpesa->c2bListener(function($data) {
// Handle incoming payment data
});
To send money to a customer, use the following method:
$response = $mpesa->b2c([
'phone' => '2547XXXXXXXX',
'amount' => 100,
'remarks' => 'Payment for services',
]);
To check the status of a transaction:
$response = $mpesa->transactionStatus([
'transaction_id' => 'YOUR_TRANSACTION_ID',
]);
To reverse a transaction, use:
$response = $mpesa->reverse([
'transaction_id' => 'YOUR_TRANSACTION_ID',
'amount' => 100,
]);
The Mpesa SDK interacts with several key API endpoints:
- STK Push:
https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest
- C2B:
https://api.safaricom.co.ke/mpesa/c2b/v1/registerurl
- B2C:
https://api.safaricom.co.ke/mpesa/b2c/v1/sendmoney
- Transaction Status:
https://api.safaricom.co.ke/mpesa/transactionstatus/v1/query
- Reversals:
https://api.safaricom.co.ke/mpesa/reversal/v1/request
$response = $mpesa->stkPush([
'phone' => '2547XXXXXXXX',
'amount' => 100,
'callback_url' => 'https://yourcallbackurl.com',
'account_reference' => 'Test123',
'transaction_desc' => 'Payment for testing',
]);
if ($response['status'] == 'success') {
echo "Payment initiated successfully.";
} else {
echo "Failed to initiate payment.";
}
$mpesa->c2bListener(function($data) {
// Log the incoming payment data
file_put_contents('payments.log', json_encode($data) . PHP_EOL, FILE_APPEND);
});
$response = $mpesa->b2c([
'phone' => '2547XXXXXXXX',
'amount' => 100,
'remarks' => 'Payment for services',
]);
if ($response['status'] == 'success') {
echo "Money sent successfully.";
} else {
echo "Failed to send money.";
}
We welcome contributions to improve the Mpesa-sdk. To contribute:
- Fork the repository.
- Create a new branch.
- Make your changes.
- Submit a pull request.
Please ensure that your code adheres to the existing coding standards.
This project is licensed under the MIT License. See the LICENSE file for details.
For any issues or questions, please open an issue on the GitHub repository. You can also check the Releases section for updates and version changes.
Thank you for using Mpesa-sdk! We hope it makes your payment integration smooth and straightforward.