edlugz/daraja
最新稳定版本:2.0.1
Composer 安装命令:
composer require edlugz/daraja
包简介
PHP/Laravel package for Mpesa Daraja
README 文档
README
Laravel package for Safaricom M-Pesa Daraja: STK Push (C2B), B2C, B2B (BuyGoods/PayBill/Pochi), 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-opensslguzzlehttp/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 # Seeders php artisan vendor:publish --provider="EdLugz\Daraja\DarajaServiceProvider" --tag=daraja-seeders
Migrations are also auto-loaded from the package, so
php artisan migratewill 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.
统计信息
- 总下载量: 312
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-14