承接 drh/mpesa 相关项目开发

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

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

drh/mpesa

Composer 安装命令:

composer require drh/mpesa

包简介

Mpesa Payments Library

关键字:

README 文档

README

GitHub TestCI Workflow Github StyleCI Workflow codecov

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

A Laravel package for integrating with Safaricom's M-Pesa payment APIs. Supports STK Push, C2B, B2C, B2B payments, transaction status queries, and identity verification.

Inspired by Mobile Money Library by Agweria: https://mobile-money.agweria.com

Requirements

  • PHP 8.2+
  • Laravel 8.x - 13.x

Installation

composer require drh/mpesa

The package auto-discovers its service provider and facades. Publish the configuration file:

php artisan vendor:publish --provider="DrH\Mpesa\MpesaServiceProvider"

Run migrations to create the payment tracking tables:

php artisan migrate

Configuration

Add these environment variables to your .env file:

# General
MPESA_SANDBOX=true

# C2B / STK Push
MPESA_KEY=your-consumer-key
MPESA_SECRET=your-consumer-secret
MPESA_C2B_SHORTCODE=174379
MPESA_C2B_PASS_KEY=your-passkey
MPESA_C2B_TRANSACTION_TYPE=CustomerPayBillOnline

# B2C (Business to Customer)
MPESA_B2C_KEY=your-b2c-key
MPESA_B2C_SECRET=your-b2c-secret
MPESA_B2C_SHORTCODE=603021
MPESA_B2C_INITIATOR=apiop37

# B2B (Business to Business)
MPESA_B2B_KEY=your-b2b-key
MPESA_B2B_SECRET=your-b2b-secret
MPESA_B2B_SHORTCODE=600584
MPESA_B2B_INITIATOR=apiop37

See the published config/drh.mpesa.php for all available options including callback URLs, retry settings, multi-tenancy, and logging.

Usage

STK Push (Lipa Na M-Pesa Online)

use DrH\Mpesa\Facades\STK;

// Simple push
$result = STK::push(amount: 100, phone: '254712345678', ref: 'Order001', description: 'Payment for order');

// Fluent API
$result = STK::amount(100)
    ->from('254712345678')
    ->usingReference('Order001', 'Payment for order')
    ->push();

// Check STK transaction status
$status = STK::status($checkoutRequestId);

C2B (Customer to Business)

Register your validation and confirmation URLs with M-Pesa:

use DrH\Mpesa\Facades\Registrar;

Registrar::shortcode(174379)
    ->onConfirmation('https://example.com/payments/callbacks/c2b-confirmation')
    ->onValidation('https://example.com/payments/callbacks/c2b-validation')
    ->submit();

B2C (Business to Customer)

use DrH\Mpesa\Facades\B2C;

// Send payment
$result = B2C::send(number: '254712345678', amount: 500, remarks: 'Salary payment');

// Fluent API
$result = B2C::to('254712345678')
    ->amount(500)
    ->withRemarks('Salary payment')
    ->send();

// Check balance
$balance = B2C::balance();

// Check transaction status
$status = B2C::status($transactionId);

B2B (Business to Business)

use DrH\Mpesa\Facades\B2B;

// Send payment
$result = B2B::pay(
    type: 'BusinessPayBill',
    shortcode: '600000',
    amount: 1000,
    reference: 'INV001',
    phone: '254712345678'
);

// Check transaction status
$status = B2B::status($request);

Identity Verification

use DrH\Mpesa\Facades\Identity;

$result = Identity::validate('254712345678');

Events

The package dispatches events for payment lifecycle hooks. Listen for these in your EventServiceProvider or via Event::listen():

Event Description
StkPushRequestedEvent STK push request initiated
StkPushPaymentSuccessEvent STK push payment completed successfully
StkPushPaymentFailedEvent STK push payment failed
C2bConfirmationEvent C2B payment confirmed
B2cPaymentSuccessEvent B2C payment completed successfully
B2cPaymentFailedEvent B2C payment failed
B2bPaymentSuccessEvent B2B payment completed successfully
B2bPaymentFailedEvent B2B payment failed
TransactionStatusSuccessEvent Transaction status query succeeded
TransactionStatusFailedEvent Transaction status query failed
QueueTimeoutEvent Queue timeout occurred

All events are in the DrH\Mpesa\Events namespace.

Artisan Commands

# Register C2B validation and confirmation URLs
php artisan mpesa:register_c2b_urls

# Check status of pending STK transactions
php artisan mpesa:query_stk_status

# Check status of pending B2C transactions
php artisan mpesa:b2c_transaction_status

# Check status of pending B2B transactions
php artisan mpesa:b2b_transaction_status

Callback Routes

The package automatically registers callback routes under /payments/callbacks/:

  • POST /payments/callbacks/stk-callback - STK push callback
  • POST /payments/callbacks/c2b-validation - C2B validation
  • POST /payments/callbacks/c2b-confirmation - C2B confirmation
  • POST /payments/callbacks/result/ - B2C/B2B result
  • POST /payments/callbacks/timeout/ - B2C/B2B timeout

A pesa.cors middleware alias is available for CORS handling on callback routes.

Testing

composer run-script test

License

MIT

drh/mpesa 适用场景与选型建议

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

它主要适用于以下技术方向: 「library」 「mpesa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.27k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-06