edlugz / daraja
PHP/Laravel package for Mpesa Daraja
Requires
- php: ^8.3
- ext-openssl: *
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^9.0 || ^10.0
- phpunit/phpunit: ^11.0 || ^12.0
- dev-main
- 1.1.3
- v1.1.2
- v1.1.1
- 1.1.0
- 1.0.1
- 1.0.0.3
- 1.0.0.2
- 1.0.0.1
- 1.0.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.9
- v0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- v0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- v0.4.10
- 0.4.9
- v0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.9
- v0.3.8
- v0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- v0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-analysis-EPwMVm
- dev-analysis-ZndRka
- dev-analysis-PGKwyo
- dev-analysis-Znd0an
- dev-analysis-g6l45d
- dev-analysis-g6l4Kb
- dev-analysis-pr96l4
- dev-analysis-RP47lj
- dev-analysis-N453rg
- dev-analysis-O3eWj4
This package is auto-updated.
Last update: 2025-09-08 19:27:16 UTC
README
Laravel package for Safaricom M-Pesa Daraja: STK Push (C2B), B2C, B2B (BuyGoods/PayBill), Account Balance, Reversal, Transaction Status, Tax remittance, and MNO Lookup — with Laravel-12 friendly service bindings, logging, and callback helpers.
Requirements
- PHP 8.3+
- Laravel 12
ext-openssl
guzzlehttp/guzzle
^7.8
Installation
composer require edlugz/daraja
Quick start (one command)
php artisan daraja:install --force
This publishes the config and migrations (optional) and runs php artisan migrate
.
Manual publish (optional)
Publish everything from this provider:
php artisan vendor:publish --provider="EdLugz\Daraja\DarajaServiceProvider"
Or publish specific tags:
# Config php artisan vendor:publish --provider="EdLugz\Daraja\DarajaServiceProvider" --tag=daraja-config # Migrations php artisan vendor:publish --provider="EdLugz\Daraja\DarajaServiceProvider" --tag=daraja-migrations
Migrations are also auto-loaded from the package, so
php artisan migrate
will pick them up even without publishing.
Environment
Note: Consumer key/secret/shortcode can be injected dynamically per tenant via
ClientCredential
. Env vars below are optional defaults.
# Mode / endpoints DARAJA_MODE= #uat|live # Optional override (leave empty to auto-derive from mode) DARAJA_BASE_URL= # (Optional) Credentials if you prefer env-based defaults DARAJA_CONSUMER_KEY= DARAJA_CONSUMER_SECRET= DARAJA_INITIATOR_NAME= DARAJA_INITIATOR_PASSWORD= DARAJA_PASS_KEY= DARAJA_SHORTCODE= # Default callback URLs (you may also pass per call) DARAJA_STK_RESULT_URL= DARAJA_BALANCE_RESULT_URL= DARAJA_MOBILE_RESULT_URL= DARAJA_TILL_RESULT_URL= DARAJA_PAYBILL_RESULT_URL= DARAJA_REVERSAL_RESULT_URL= DARAJA_TRANSACTION_QUERY_RESULT_URL= DARAJA_REVERSAL_QUERY_RESULT_URL= DARAJA_FUNDS_TRANSFER_RESULT_URL= DARAJA_TIMEOUT_URL= # Logging DARAJA_LOG_ENABLED=true DARAJA_LOG_LEVEL=DEBUG
Usage
1) Build credentials dynamically
use EdLugz\Daraja\Data\ClientCredential; $cred = new ClientCredential( accountId: (string) $accountId, consumerKey: (string) $consumerKey, consumerSecret: (string) $consumerSecret, shortcode: (string) $shortcode, initiator: (string) $initiatorName, password: (string) $initiatorPassword, passkey: (string) $stkPasskey, use_b2c_validation: '0' // or '1' );
2) Make a request
STK Push (C2B)
use EdLugz\Daraja\Requests\C2B; $c2b = new C2B($cred); // result URL defaults to config('daraja.stk_result_url') $tx = $c2b->send('0712345678', '150', 'INV-12345');
B2C (Send to mobile)
use EdLugz\Daraja\Requests\B2C; $b2c = new B2C($cred, route('daraja.mobile.result')); $tx = $b2c->pay('0712345678', 100);
B2B (BuyGoods / PayBill)
use EdLugz\Daraja\Requests\B2B; $b2b = new B2B($cred); $buyGoods = $b2b->till(recipient: '123456', requester: '0712345678', amount: 100); $paybill = $b2b->paybill(recipient: '600000', requester: '0712345678', amount: 100, accountReference: 'ACC-1');
More flows: Balance, Reversal, Transaction Status, Tax remittance, and MNO Lookup are provided via their respective request classes.
Ensure these endpoints are publicly reachable and validate incoming payloads.
Change log
Please see the changelog for recent changes.
Contributing
See contributing.md for details and the todo list.
Security
If you discover any security-related issues, please email eddy.lugaye@gmail.com instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.