{"id":19356713,"url":"https://github.com/savannabits/daraja","last_synced_at":"2025-04-23T10:32:57.752Z","repository":{"id":62540645,"uuid":"264098226","full_name":"savannabits/daraja","owner":"savannabits","description":"Safaricom Mpesa API (Safaricom Daraja) for PHP Laravel . Simple integration with safaricom's MPESA API dubbed { DARAJA } and allow you to make requests in the nice Laravel chaining way that we all know and love.","archived":false,"fork":false,"pushed_at":"2022-11-28T05:24:07.000Z","size":36,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-07-12T21:56:33.851Z","etag":null,"topics":["b2b","c2b","daraja-api","daraja-mpesa","laravel","laravel-package","mpesa","mpesa-payments","mpesa-sdk","safaricom","safaricom-api","savannabits","stkpush"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/savannabits.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-15T04:47:31.000Z","updated_at":"2023-11-25T12:57:41.000Z","dependencies_parsed_at":"2023-01-23T18:31:25.050Z","dependency_job_id":null,"html_url":"https://github.com/savannabits/daraja","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/savannabits%2Fdaraja","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/savannabits%2Fdaraja/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/savannabits%2Fdaraja/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/savannabits%2Fdaraja/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/savannabits","download_url":"https://codeload.github.com/savannabits/daraja/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250416856,"owners_count":21427075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["b2b","c2b","daraja-api","daraja-mpesa","laravel","laravel-package","mpesa","mpesa-payments","mpesa-sdk","safaricom","safaricom-api","savannabits","stkpush"],"created_at":"2024-11-10T07:05:22.730Z","updated_at":"2025-04-23T10:32:52.743Z","avatar_url":"https://github.com/savannabits.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Description\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/savannabits/daraja.svg?style=flat-square)](https://packagist.org/packages/savannabits/daraja)\n[![Build Status](https://img.shields.io/travis/savannabits/daraja/master.svg?style=flat-square)](https://travis-ci.org/savannabits/daraja)\n[![Quality Score](https://img.shields.io/scrutinizer/g/savannabits/daraja.svg?style=flat-square)](https://scrutinizer-ci.com/g/savannabits/daraja)\n[![Total Downloads](https://img.shields.io/packagist/dt/savannabits/daraja.svg?style=flat-square)](https://packagist.org/packages/savannabits/daraja)\n\nThe savannabits/daraja package is a Laravel package that makes it easier to talk to the Safaricom MPESA integration RESTFul API dubbed Safaricom Daraja.\n### Motivation\nSo why Daraja and not other packages out there? \nI have looked at a number of packages that can accomplish the same, but the solution I was looking for was a package that can allow me do token generation and Callback registration on the fly from my code. That means I can store my credentials (Consumer Key and consumer secret) even in a database and retrieve them dynamically, using them to register my callbacks.\nWith this solution, you can even have a multiple shortcode gateway (Many apps each using a different paybill can use this gateway to talk to safaricom and register dynamic confirmation urls according to their needs).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require savannabits/daraja\n```\n\n## Usage\nUsage is very simple. If you are using C2B apis, the first step is to register the urls. Most of the work has been done for you under the hood.\n\n### Callback URLs Registration\n```php\n use Savannabits\\Daraja\\Daraja;\n \n $shortcode = \"600737\";//Your Paybill or till number here\n $confirmationURL = 'your dynamic validation url here';\n $validationURL = 'your dynamic validation url here'; // Optional. Leave null if you don't want validation\n $environment = \"sandbox\"; // or \"live\"\n $responseType = \"Cancelled\"; //Default Response type in case the validation URL is unreachable or is undefined. Either Cancelled or Completed as per the safaricom documentation\n $response = Daraja::getInstance()\n      -\u003esetCredentials(\"CONSUMER KEY\",\"CONSUMER SECRET\",$environment)\n      -\u003eregisterCallbacks($shortcode, $confirmationURL, $validationURL,$responseType);\n```\nThat's it. You can even put this code inside a Console Command class so that you run something like `php artisna mpesa:register-callbacks` from your terminal.\n\n### C2B simulation\nIf you are on Dev and would like to simulate a C2B transaction (without using any actual money) you can use the sandbox credentials and assuming you have registered the callbacks above, run the following:\n\n```php\n    use Savannabits\\Daraja\\Daraja;\n    /**\n     * Here is the c2b Function documentation to help you understand the params:\n     * Use this function to initiate a C2B transaction\n     * @param $ShortCode | 6 digit M-Pesa Till Number or PayBill Number\n     * @param $CommandID | Unique command for each transaction type. either \"CustomerPayBillOnline\" or \"CustomerBuyGoodsOnline\"\n     * @param $Amount | The amount been transacted.\n     * @param $Msisdn | MSISDN (phone number) sending the transaction, start with country code without the plus(+) sign.\n     * @param $BillRefNumber | \tBill Reference Number (Optional).\n     * @return mixed|string\n     */\n    $shortcode = \"YOUR SHORTCODE\";\n    $commandID = \"CustomerPayBillOnline\";\n    $amount = 100;\n    $msisdn = \"07xxxxxxxx\"; // See safaricom daraja documentation and check your credentials for the specific number given for testing.\n    $billRefNumber = \"THE PAYBILL ACCOUNT NO.\"; // e.g \"MAMA MBOGA 212\"\n    $response = Daraja::getInstance()\n                    -\u003esetCredentials(\"YOUR CONSUMER KEY\",\"YOUR CONSUMER SECRET\",\"sandbox\")\n                    -\u003ec2b($shortcode, $commandID, $amount, $msisdn, $billRefNumber);\n```\n\n### STK Simulation (Lipa na MPESA Online)\nHere is the method documentation\n```php\n\n/**\n * Use this function to initiate an STKPush Simulation\n * @param $BusinessShortCode | The organization shortcode used to receive the transaction.\n * @param $LipaNaMpesaPasskey | The password for encrypting the request. This is generated by base64 encoding BusinessShortcode, Passkey and Timestamp.\n * @param $TransactionType | The transaction type to be used for this request. Only CustomerPayBillOnline is supported.\n * @param $Amount | The amount to be transacted.\n * @param $PartyA | The MSISDN sending the funds.\n * @param $PartyB | The organization shortcode receiving the funds\n * @param $PhoneNumber | The MSISDN sending the funds.\n * @param $CallBackURL | The url to where responses from M-Pesa will be sent to.\n * @param $AccountReference | Used with M-Pesa PayBills.\n * @param $TransactionDesc | A description of the transaction.\n * @param $Remark | Remarks\n * @return mixed|string\n */\n\\Savannabits\\Daraja\\Daraja::getInstance()\n            -\u003esetCredentials(\"CONSUMER KEY\",\"CONSUMER SECRET\",\"sandbox\")\n            -\u003eSTKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remark);\n```\n```php\n    // Example\n   $json = \\Savannabits\\Daraja\\Daraja::getInstance()\n            -\u003esetCredentials(\"CONSUMER KEY\",\"CONSUMER SECRET\",\"sandbox\")\n            -\u003eSTKPushSimulation($app-\u003eshort_code,$app-\u003elnm_passkey,$app-\u003etransaction_type, $amount, $phone_number, $short_code, $phone_number, $confirm_callback,$account_ref, $transaction_desc, $comments);\n```\n### Validation and Confirmation Callback example, Finishing a Transaction\n```php\nuse Savannabits\\Daraja\\Daraja;\nclass MyController extends \\App\\Http\\Controllers\\Controller {\n    public function c2bValidationCallback(Request $request) {\n        // Perform your validations here and set the status\n        $status = true; // Or false based on whether you want to accept or reject the transaction.\n        Daraja::getInstance()-\u003efinishTransaction($status);\n    }\n    public function c2bConfirmationCallback(Request $request) {\n        //Get Response data\n        $response = Daraja::getInstance()-\u003egetDataFromCallback();\n        // $response = $request-\u003eall(); //Alternatively...\n        // Do what you want with the data\n        // ...\n        // Finish Transaction\n        Daraja::getInstance()-\u003efinishTransaction(true);\n    }\n    public function stkConfirmationCallback(Request $request) {\n       $mpesa = new Daraja();\n        // Get data from safaricom response.\n       $data = $mpesa-\u003egetDataFromCallback();\n       \\Log::info($data);\n        // Handle the data\n        //...\n        //Finish Transaction by sending acknowledgment to safaricom\n        $mpesa-\u003efinishTransaction(true);\n        \n    }\n}\n```\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email maosa.sam@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [Sam Maosa](https://github.com/savannabits)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Laravel Package Boilerplate\n\nThis package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsavannabits%2Fdaraja","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsavannabits%2Fdaraja","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsavannabits%2Fdaraja/lists"}