8000 GitHub - andreim2k/Mpesa-sdk: mpesa-sdk is a comprehensive and easy-to-use PHP library for integrating with the M-Pesa API. It provides a seamless way to handle mobile money transactions, including STK Push (Lipa Na M-Pesa Online), C2B (Customer-to-Business), B2C (Business-to-Customer), transaction status queries, and reversals.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mpesa-sdk is a comprehensive and easy-to-use PHP library for integrating with the M-Pesa API. It provides a seamless way to handle mobile money transactions, including STK Push (Lipa Na M-Pesa Online), C2B (Customer-to-Business), B2C (Business-to-Customer), transaction status queries, and reversals.

License

Notifications You must be signed in to change notification settings

andreim2k/Mpesa-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mpesa-sdk 📱💰

Mpesa-sdk PHP License

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!

Table of Contents

Features

  • 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.

Installation

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';

Usage

Initializing the SDK

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',
]);

STK Push Example

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',
]);

C2B Example

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
});

B2C Example

To send money to a customer, use the following method:

$response = $mpesa->b2c([
    'phone' => '2547XXXXXXXX',
    'amount' => 100,
    'remarks' => 'Payment for services',
]);

Transaction Status Query Example

To check the status of a transaction:

$response = $mpesa->transactionStatus([
    'transaction_id' => 'YOUR_TRANSACTION_ID',
]);

Reversal Example

To reverse a transaction, use:

$response = $mpesa->reverse([
    'transaction_id' => 'YOUR_TRANSACTION_ID',
    'amount' => 100,
]);

API Endpoints

The Mpesa SDK interacts with several key API endpoints:

  1. STK Push: https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest
  2. C2B: https://api.safaricom.co.ke/mpesa/c2b/v1/registerurl
  3. B2C: https://api.safaricom.co.ke/mpesa/b2c/v1/sendmoney
  4. Transaction Status: https://api.safaricom.co.ke/mpesa/transactionstatus/v1/query
  5. Reversals: https://api.safaricom.co.ke/mpesa/reversal/v1/request

Examples

STK Push Example

$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.";
}

C2B Listener Example

$mpesa->c2bListener(function($data) {
    // Log the incoming payment data
    file_put_contents('payments.log', json_encode($data) . PHP_EOL, FILE_APPEND);
});

B2C Example

$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.";
}

Contributing

We welcome contributions to improve the Mpesa-sdk. To contribute:

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. Submit a pull request.

Please ensure that your code adheres to the existing coding standards.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

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.

About

mpesa-sdk is a comprehensive and easy-to-use PHP library for integrating with the M-Pesa API. It provides a seamless way to handle mobile money transactions, including STK Push (Lipa Na M-Pesa Online), C2B (Customer-to-Business), B2C (Business-to-Customer), transaction status queries, and reversals.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0