承接 breviam/mpesa-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

breviam/mpesa-sdk

Composer 安装命令:

composer require breviam/mpesa-sdk

包简介

Laravel SDK for M-Pesa Daraja API

README 文档

README

M-Pesa Laravel SDK

Latest Version on Packagist Total Downloads Tests

A comprehensive Laravel SDK for integrating with Safaricom's M-Pesa Daraja API. This package provides a clean, well-documented interface for all major M-Pesa services including STK Push, C2B, B2C, Transaction Status, and Account Balance.

Features

  • STK Push (Lipa na M-Pesa Online)
  • C2B (Customer to Business) payments
  • B2C (Business to Customer) payments
  • Transaction Status queries
  • Account Balance inquiries
  • Webhook handling with automatic route registration
  • Token management with automatic caching
  • Laravel Facade support
  • Artisan commands for testing and debugging
  • Comprehensive logging
  • Event-driven architecture
  • Full test coverage

Requirements

  • PHP 8.1+
  • Laravel 11.x (recommended)
  • Laravel 10.x (experimental support)

Note: For detailed Laravel version compatibility information, see LARAVEL_COMPATIBILITY.md

Installation

Install the package via Composer:

composer require breviam/mpesa-sdk

Publish the configuration file:

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

Configuration

Add your M-Pesa credentials to your .env file:

MPESA_ENV=sandbox
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=your_shortcode
MPESA_PASSKEY=your_passkey
MPESA_INITIATOR_NAME=your_initiator_name
MPESA_SECURITY_CREDENTIAL=your_security_credential
MPESA_CALLBACK_URL=https://yourdomain.com/mpesa/webhooks

# Optional: Withdrawal-specific credentials (if different)
MPESA_W_CONSUMER_KEY=your_withdrawal_consumer_key
MPESA_W_CONSUMER_SECRET=your_withdrawal_consumer_secret
MPESA_W_SHORTCODE=your_withdrawal_shortcode
MPESA_INITIATOR_W_NAME=your_withdrawal_initiator
MPESA_INITIATOR_W_PASS=your_withdrawal_security_credential

# Optional: Service-specific callback URLs
MPESA_STK_CALLBACK_URL=https://yourdomain.com/mpesa/stk
MPESA_C2B_CALLBACK_URL=https://yourdomain.com/mpesa/c2b
MPESA_B2C_CALLBACK_URL=https://yourdomain.com/mpesa/b2c

Basic Usage

STK Push (Lipa na M-Pesa Online)

use Breviam\MpesaSdk\Facades\Mpesa;

// Initiate STK Push
$response = Mpesa::stkPush(
    phone: '254712345678',
    amount: 100,
    reference: 'ORDER123',
    description: 'Payment for order #123'
);

// Query STK Push status
$status = Mpesa::stkQuery($response['CheckoutRequestID']);

C2B (Customer to Business)

// Register C2B URLs (usually done once)
$response = Mpesa::c2b()->registerUrls(
    confirmationUrl: 'https://yourdomain.com/mpesa/webhooks/c2b/confirmation',
    validationUrl: 'https://yourdomain.com/mpesa/webhooks/c2b/validation'
);

// Simulate C2B payment (sandbox only)
$response = Mpesa::c2b()->simulate(
    phone: '254712345678',
    amount: 100,
    reference: 'BILL123'
);

B2C (Business to Customer)

$response = Mpesa::sendMoney(
    phone: '254712345678',
    amount: 1000,
    commandId: 'BusinessPayment',
    remarks: 'Salary payment'
);

Account Balance

$response = Mpesa::checkBalance('Balance inquiry');

Transaction Status

$response = Mpesa::checkTransactionStatus(
    transactionId: 'ABC123XYZ',
    partyA: '254712345678',
    remarks: 'Transaction status check'
);

Advanced Usage

Using Individual Services

use Breviam\MpesaSdk\Contracts\StkInterface;
use Breviam\MpesaSdk\Contracts\AuthInterface;

class PaymentController extends Controller
{
    public function __construct(
        private StkInterface $stkService,
        private AuthInterface $authService
    ) {}

    public function initiatePayment()
    {
        $response = $this->stkService->push(
            '254712345678',
            100,
            'ORDER123',
            'Payment description'
        );
        
        return response()->json($response);
    }
}

Handling Webhooks

The package automatically registers webhook routes. You can listen to events:

use Illuminate\Support\Facades\Event;

// In your EventServiceProvider
Event::listen('mpesa.stk.callback', function ($data) {
    // Handle STK Push callback
    Log::info('STK Push callback received', $data);
});

Event::listen('mpesa.c2b.confirmation', function ($data) {
    // Handle C2B confirmation
    $payment = Payment::where('reference', $data['BillRefNumber'])->first();
    $payment->update(['status' => 'confirmed']);
});

Artisan Commands

Check token status:

php artisan mpesa:token

Clear token cache:

php artisan mpesa:token --clear

Simulate payment (sandbox only):

php artisan mpesa:simulate-payment 254712345678 100 --reference=TEST123

Testing

Run the tests:

composer test

Run tests with coverage:

composer test:coverage

Events

The package fires the following events:

Event Description
mpesa.stk.callback STK Push callback received
mpesa.c2b.validation C2B validation request
mpesa.c2b.confirmation C2B confirmation received
mpesa.b2c.result B2C transaction result
mpesa.b2c.timeout B2C transaction timeout
mpesa.balance.result Balance inquiry result
mpesa.balance.timeout Balance inquiry timeout
mpesa.transaction.result Transaction status result
mpesa.transaction.timeout Transaction status timeout

Configuration Options

The package supports extensive configuration through the config/mpesa.php file:

return [
    'env' => 'sandbox', // or 'production'
    'consumer_key' => env('MPESA_CONSUMER_KEY'),
    'consumer_secret' => env('MPESA_CONSUMER_SECRET'),
    'shortcode' => env('MPESA_SHORTCODE'),
    'passkey' => env('MPESA_PASSKEY'),
    'initiator' => env('MPESA_INITIATOR'),
    'security_credential' => env('MPESA_SECURITY_CREDENTIAL'),
    'callback_url' => env('MPESA_CALLBACK_URL'),
    
    'cache' => [
        'prefix' => 'mpesa_',
        'ttl' => 3300, // 55 minutes
    ],
    
    'timeout' => 30,
    
    'logging' => [
        'enabled' => true,
        'channel' => 'daily',
    ],
];

Security

  • All sensitive data is masked in logs
  • OAuth tokens are securely cached
  • HTTPS is enforced for all API calls
  • Webhook requests are logged with IP and user agent

Error Handling

The package throws specific exceptions:

use Breviam\MpesaSdk\Exceptions\MpesaException;
use Breviam\MpesaSdk\Exceptions\AuthenticationException;

try {
    $response = Mpesa::stkPush('254712345678', 100, 'REF123', 'Description');
} catch (AuthenticationException $e) {
    // Handle authentication errors
    Log::error('M-Pesa authentication failed: ' . $e->getMessage());
} catch (MpesaException $e) {
    // Handle general M-Pesa API errors
    Log::error('M-Pesa API error: ' . $e->getMessage());
    $context = $e->getContext(); // Get additional context
}

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email vikgachewa@hotmail.com instead of using the issue tracker.

Credits

License

The GNU General Public License v3.0. Please see License File for more information.

Changelog

Please see CHANGELOG for more information on what has changed recently.

breviam/mpesa-sdk 适用场景与选型建议

breviam/mpesa-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 breviam/mpesa-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 breviam/mpesa-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 breviam/mpesa-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 44
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-07-08