starnerz/laravel-daraja

A Laravel package for the Safaricom M-Pesa Daraja APIs.

Maintainers

Package info

github.com/starnerz/laravel-daraja

Homepage

Documentation

pkg:composer/starnerz/laravel-daraja

Transparency log

Statistics

Installs: 778

Dependents: 0

Suggesters: 0

Stars: 7

Open Issues: 2

v5.0.0 2026-08-18 10:53 UTC

This package is auto-updated.

Last update: 2026-08-20 22:51:26 UTC


README

Every Safaricom M-Pesa Daraja API, as ordinary Laravel code. Seventeen APIs โ€” STK Push, C2B, B2C, B2B, Dynamic QR, M-Pesa Ratiba, Bill Manager and the rest โ€” with typed responses, cached tokens, and callbacks you can actually test.

๐Ÿ“– Documentation ยท Install ยท STK Push ยท Going live

Daraja::stk()->push(phone: '0712345678', amount: 1500, accountReference: 'INV-001');

Tests Static analysis Latest version Downloads License

Requirements

  • PHP 8.3+
  • Laravel 12 or 13

Installation

composer require starnerz/laravel-daraja
DARAJA_MODE=sandbox
DARAJA_CONSUMER_KEY=your-consumer-key
DARAJA_CONSUMER_SECRET=your-consumer-secret
DARAJA_STK_SHORTCODE=174379
DARAJA_STK_PASS_KEY=your-passkey
DARAJA_STK_CALLBACK_URL=https://your-domain/daraja/stk

Verify the credentials:

php artisan daraja:token

Prompt a customer to pay

use Starnerz\LaravelDaraja\Facades\Daraja;

$response = Daraja::stk()->push(
    phone: '0712345678',       // any Kenyan format
    amount: 1500,
    accountReference: 'INV-001',
);

$response->accepted();          // Safaricom sent the prompt
$response->checkoutRequestId;   // identifies this attempt

The payment result arrives later on your callback URL:

use Starnerz\LaravelDaraja\Events\StkCallbackReceived;

Event::listen(function (StkCallbackReceived $event) {
    if ($event->callback->successful()) {
        Order::markPaid(
            $event->callback->checkoutRequestId,
            $event->callback->receipt(),
        );
    }
});

Supported APIs

M-Pesa Express Daraja::stk()->push() / ->query()
Customer to Business Daraja::c2b()->registerUrls() / ->simulatePayBill()
Business to Customer Daraja::b2c()->business() / ->salary() / ->promotion()
Business to Pochi Daraja::b2c()->pochi()
Business to Business Daraja::b2b()->payBill() / ->buyGoods() / ->accountTopUp()
B2B Express Checkout Daraja::b2bExpress()->push()
Account Balance Daraja::balance()->query()
Transaction Status Daraja::transaction()->query()
Reversal Daraja::reversal()->reverse()
Dynamic QR Daraja::qr()->generate()
M-Pesa Ratiba Daraja::standingOrder()->create()
Bill Manager Daraja::billManager()->invoice()
Pull Transactions Daraja::pull()->query()
Lipa na Bonga Daraja::bonga()->redeem()

Testing

Built on Laravel's HTTP client, so Http::fake() drives the whole package โ€” no sandbox credentials, network or handset required.

Http::fake([
    '*/oauth/*' => Http::response(['access_token' => 'test']),
    '*/mpesa/stkpush/*' => Http::response(['ResponseCode' => '0']),
]);

See the testing guide.

Upgrading from 4.x

v5 is a rewrite: the facade is now Daraja, responses are typed objects, and several endpoints moved (C2B to v2, B2C to v3). See the upgrade guide.

Laravel 10 and 11 are not supported โ€” both are past end of life and carry unpatched advisories. Applications on those versions should stay on ^4.0.

Contributing

See CONTRIBUTING.md.

Security

Report vulnerabilities to stanleykimathi@gmail.com rather than the issue tracker. See SECURITY.md.

Licence

MIT. See LICENSE.