{"id":17943345,"url":"https://github.com/smodav/mpesa","last_synced_at":"2025-04-10T06:19:03.225Z","repository":{"id":44669503,"uuid":"64550935","full_name":"SmoDav/mpesa","owner":"SmoDav","description":"M-Pesa API Package for PHP","archived":false,"fork":false,"pushed_at":"2025-02-27T06:41:42.000Z","size":150,"stargazers_count":157,"open_issues_count":1,"forks_count":128,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-03T03:58:23.546Z","etag":null,"topics":["mobile-money","mpesa","mpesa-api","php"],"latest_commit_sha":null,"homepage":"https://smodavproductions.com/blog","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/SmoDav.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-30T15:35:06.000Z","updated_at":"2025-02-27T06:41:46.000Z","dependencies_parsed_at":"2022-09-22T14:43:23.004Z","dependency_job_id":"ac78af80-bb8b-439e-bb94-b7450295a243","html_url":"https://github.com/SmoDav/mpesa","commit_stats":{"total_commits":92,"total_committers":11,"mean_commits":8.363636363636363,"dds":0.2065217391304348,"last_synced_commit":"ea97cc052935a5374a048adebacaa1e220313997"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmoDav%2Fmpesa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmoDav%2Fmpesa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmoDav%2Fmpesa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmoDav%2Fmpesa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SmoDav","download_url":"https://codeload.github.com/SmoDav/mpesa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166864,"owners_count":21058481,"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":["mobile-money","mpesa","mpesa-api","php"],"created_at":"2024-10-29T04:24:19.821Z","updated_at":"2025-04-10T06:19:03.197Z","avatar_url":"https://github.com/SmoDav.png","language":"PHP","readme":"# M-PESA API Package\n[![Build Status](https://travis-ci.org/SmoDav/mpesa.svg?branch=master)](https://travis-ci.org/SmoDav/mpesa)\n[![Total Downloads](https://poser.pugx.org/smodav/mpesa/d/total.svg)](https://packagist.org/packages/smodav/mpesa)\n[![Latest Stable Version](https://poser.pugx.org/smodav/mpesa/v/stable.svg)](https://packagist.org/packages/smodav/mpesa)\n[![Latest Unstable Version](https://poser.pugx.org/smodav/mpesa/v/unstable.svg)](https://packagist.org/packages/smodav/mpesa)\n[![License](https://poser.pugx.org/smodav/mpesa/license.svg)](https://packagist.org/packages/smodav/mpesa)\n\nThis is a PHP package for the Safaricom's M-Pesa API. \nThe API allows a merchant to initiate C2B online checkout (paybill via web) transactions.\nThe merchant submits authentication details, transaction details, callback url and callback method. \n\nAfter request submission, the merchant receives instant feedback with validity status of their requests. \nThe C2B API handles customer validation and authentication via USSD push. \nThe customer then confirms the transaction. If the validation of the customer fails or the customer declines the transaction, the API makes a callback to merchant. Otherwise the transaction is processed and its status is made through a callback.\n\nIf you enjoy using this package, please take a moment and [buy me some coffee.](https://rave.flutterwave.com/donate/fiqyumudlt6t)\n\n\n\n## Installation\n\nPull in the package through Composer.\n\n### Native Addon\n\nWhen using vanilla PHP, modify your `composer.json` file to include:\n\n```json\n  \"scripts\": {\n    \"post-update-cmd\": [\n        \"SmoDav\\\\Mpesa\\\\Support\\\\Installer::install\"\n    ]\n  },\n```\n\nThis script will copy the default configuration file to a config folder in the root directory of your project.\nNow proceed to require the package.\n\n### General Install\n\nRun `composer require smodav/mpesa` to get the latest stable version of the package.\n\n## Migration from previous versions\n\nv5 of the package changes the implementation and introduces some breaking changes. Please have a look at the [CHANGELOG](https://github.com/SmoDav/mpesa/blob/master/CHANGELOG.md).\n\nv4 of this package uses a new configuration setup. You will need to update your config file in order to upgrade v3 to v4. v2 is still incompatible since it uses the older API version.\n\n### Laravel\n\nWhen using Laravel 5.5+, the package will automatically register. For laravel 5.4 and below,\ninclude the service provider and its alias within your `config/app.php`.\n\n```php\n'providers' =\u003e [\n    SmoDav\\Mpesa\\Laravel\\ServiceProvider::class,\n],\n\n'aliases' =\u003e [\n    'STK'       =\u003e SmoDav\\Mpesa\\Laravel\\Facades\\STK::class,\n    'Simulate'  =\u003e SmoDav\\Mpesa\\Laravel\\Facades\\Simulate::class,\n    'Registrar' =\u003e SmoDav\\Mpesa\\Laravel\\Facades\\Registrar::class,\n    'Identity'  =\u003e SmoDav\\Mpesa\\Laravel\\Facades\\Identity::class,\n],\n```\n\nPublish the package specific config using:\n```bash\nphp artisan vendor:publish\n```\n\n### Other Frameworks\n\nTo implement this package, a configuration repository is needed, thus any other framework will need to create its own implementation of the `ConfigurationStore` and `CacheStore` interfaces.\n\n### Configuration\n\nThe package allows you to have multiple accounts. Each account will have its specific credentials and endpoints that are independent of the rest.\nYou will be required to set the default account to be used for all transactions, which you can override on each request you make. The package comes\nwith two default accounts that you can modify.\n\n```\n/*\n|--------------------------------------------------------------------------\n| Default Account\n|--------------------------------------------------------------------------\n|\n| This is the default account to be used when none is specified.\n*/\n\n'default' =\u003e 'staging',\n\n/*\n|--------------------------------------------------------------------------\n| File Cache Location\n|--------------------------------------------------------------------------\n|\n| When using the Native Cache driver, this will be the relative directory\n| where the cache information will be stored.\n*/\n\n'cache_location' =\u003e '../cache',\n\n/*\n|--------------------------------------------------------------------------\n| Accounts\n|--------------------------------------------------------------------------\n|\n| These are the accounts that can be used with the package. You can configure\n| as many as needed. Two have been setup for you.\n|\n| Sandbox: Determines whether to use the sandbox, Possible values: sandbox | production\n| Initiator: This is the username used to authenticate the transaction request\n| LNMO:\n|    shortcode: The till number\n|    passkey: The passkey for the till number\n|    callback: Endpoint that will be be queried on completion or failure of the transaction.\n|\n*/\n\n'accounts' =\u003e [\n    'staging' =\u003e [\n        'sandbox' =\u003e true,\n        'key' =\u003e 'your development consumer key',\n        'secret' =\u003e 'your development consumer secret',\n        'initiator' =\u003e 'your development username',\n        'id_validation_callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        'lnmo' =\u003e [\n            'paybill' =\u003e 'your development paybill number',\n            'shortcode' =\u003e 'your development business code',\n            'passkey' =\u003e 'your development passkey',\n            'callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        ]\n    ],\n\n    'paybill_1' =\u003e [\n        'sandbox' =\u003e false,\n        'key' =\u003e 'your production consumer key',\n        'secret' =\u003e 'your production consumer secret',\n        'initiator' =\u003e 'your production username',\n        'id_validation_callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        'lnmo' =\u003e [\n            'paybill' =\u003e 'your production paybill number',\n            'shortcode' =\u003e 'your production business code',\n            'passkey' =\u003e 'your production passkey',\n            'callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        ]\n    ],\n\n    'paybill_2' =\u003e [\n        'sandbox' =\u003e false,\n        'key' =\u003e 'your production consumer key',\n        'secret' =\u003e 'your production consumer secret',\n        'initiator' =\u003e 'your production username',\n        'id_validation_callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        'lnmo' =\u003e [\n            'paybill' =\u003e 'your production paybill number',\n            'shortcode' =\u003e 'your production business code',\n            'passkey' =\u003e 'your production passkey',\n            'callback' =\u003e 'http://example.com/callback?secret=some_secret_hash_key',\n        ]\n    ],\n],\n```\n\nYou can add as many accounts as required and switch the connection using the method `usingAccount` on `STK`, `Register` and `Simulate` as shown below.\n\nAlso, note the difference between the `business shortcode` and your `paybill number` in the configuration as getting them wrong will cost you a lot of time debugging.\n\n\n## Usage\n\nFor Vanilla PHP you will need to initialize the core engine before any requests as shown below. The package comes with a vanilla php implementation of the cache and configuration store, `NativeCache` and `NativeConfig`.\n\nThe `NativeConfig` receives the custom location for the configuration file to be used as the first constructor argument. If no value is passed when creating the instance, it will use the default configuration and look for a configuration file on the root of the project under `configs` directory.\n\nThe `NativeCache` receives a custom directory path as the first constructor argument. The path denotes where the cache should store its files. If no path is provided, the default cache location in the config will be used.\n\n```php\nuse GuzzleHttp\\Client;\nuse SmoDav\\Mpesa\\Engine\\Core;\nuse SmoDav\\Mpesa\\Native\\NativeCache;\nuse SmoDav\\Mpesa\\Native\\NativeConfig;\n\nrequire \"vendor/autoload.php\";\n\n$config = new NativeConfig();\n$cache = new NativeCache($config-\u003eget('cache_location'));\n// or\n$cache = new NativeCache(__DIR__ . '/../some/awesome/directory');\n\n$core = new Core(new Client, $config, $cache);\n\n```\n\n### URL Registration\n#### submit(shortCode = null, confirmationURL = null, validationURL = null, onTimeout = 'Completed|Cancelled', account = null)\n\nRegister callback URLs\n\n##### Vanilla\n\n```php\nuse SmoDav\\Mpesa\\C2B\\Registrar;\n\n$conf = 'http://example.com/mpesa/confirm?secret=some_secret_hash_key';\n$val = 'http://example.com/mpesa/validate?secret=some_secret_hash_key';\n\n\n$response = (new Registrar($core))-\u003eregister(600000)\n    -\u003eonConfirmation($conf)\n    -\u003eonValidation($val)\n    -\u003esubmit();\n\n/****** OR ********/\n$response = (new Registrar($core))-\u003esubmit(600000, $conf, $val);\n\n```\n\nWhen having multiple accounts, switch using the `usingAccount` method. We currently have `staging`, `paybill_1` and `paybill_2` with `staging` as the default:\n\n```php\n$response = (new Registrar($core))\n    -\u003eregister(600000)\n    -\u003eusingAccount('paybill_1')\n    -\u003eonConfirmation($conf)\n    -\u003eonValidation($val)\n    -\u003esubmit();\n\n/****** OR ********/\n$response = (new Registrar($core))-\u003esubmit(600000, $conf, $val, null, 'paybill_1');\n```\n\n##### Laravel\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\Registrar;\n\n$conf = 'http://example.com/mpesa/confirm?secret=some_secret_hash_key';\n$val = 'http://example.com/mpesa/validate?secret=some_secret_hash_key';\n\n$response = Registrar::register(600000)\n    -\u003eonConfirmation($conf)\n    -\u003eonValidation($val)\n    -\u003esubmit();\n\n/****** OR ********/\n$response = Registrar::submit(600000, $conf, $val);\n```\n\nUsing the `paybill_1` account:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\Registrar;\n\n$response = Registrar::register(600000)\n    -\u003eusingAccount('paybill_1')\n    -\u003eonConfirmation($conf)\n    -\u003eonValidation($val)\n    -\u003esubmit();\n\n/****** OR ********/\n$response = Registrar::submit(600000, $conf, $val, null, 'paybill_1');\n```\n\n### Simulate Transaction\n#### push(amount = null, number = null, reference = null, account = null, command = null)\n\nInitiate a C2B simulation transaction request.\n\nNote that when initiating a C2B simulation, setting the command type is optional and by default `CustomerPaybillOnline`\nwill be used.\n\n##### Vanilla\n\n```php\nuse SmoDav\\Mpesa\\C2B\\Simulate;\n\n$simulate = new Simulate($core)\n\n$response = $simulate-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003epush();\n\n/****** OR ********/\n$response = $simulate-\u003epush(10, 254722000000, 'Some Reference');\n```\n\nUsing the `paybill_1` account:\n\n```php\n$response = $simulate-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003eusingAccount('paybill_1')\n    -\u003epush();\n\n/****** OR ********/\n$response = $simulate-\u003epush(10, 254722000000, 'Some Reference', 'paybill_1');\n```\n\nUsing the `CustomerBuyGoodsOnline` command:\n\n```php\n$response = $simulate-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003esetCommand(CUSTOMER_BUYGOODS_ONLINE)\n    -\u003epush();\n\n/****** OR ********/\n$response = $simulate-\u003epush(10, 254722000000, 'Some Reference', null, CUSTOMER_BUYGOODS_ONLINE);\n```\n\n\n##### Laravel\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\Simulate;\n\n$response = Simulate::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003epush();\n\n/****** OR ********/\n$response = Simulate::push(10, 254722000000, 'Some Reference');\n```\n\nUsing the `paybill_1` account:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\Simulate;\n\n$response = Simulate::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003eusingAccount('paybill_1')\n    -\u003epush();\n\n/****** OR ********/\n$response = Simulate::push(10, 254722000000, 'Some Reference', 'paybill_1');\n```\n\nUsing the `CustomerBuyGoodsOnline` command:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\Simulate;\n\n$response = Simulate::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference')\n    -\u003esetCommand(CUSTOMER_BUYGOODS_ONLINE) \n    -\u003epush();\n\n/****** OR ********/\n$response = Simulate::push(10, 254722000000, 'Some Reference', null, CUSTOMER_BUYGOODS_ONLINE);\n```\n\n### STK PUSH\n#### push(amount = null, number = null, reference = null, description = null, account = null, command = null)\n\nInitiate a C2B STK Push request.\n\nNote that when initiating an STK Push, setting the command type is optional and by default `CustomerPaybillOnline`\nwill be used.\n\n##### Vanilla\n\n```php\nuse SmoDav\\Mpesa\\C2B\\STK;\n\n$stk = new STK($core);\n\n$response = $stk-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003epush();\n\n/****** OR ********/\n$response = $stk-\u003epush(10, 254722000000, 'Some Reference', 'Test Payment');\n```\n\nUsing the `paybill_2` account:\n\n```php\n\n$response = $stk-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingAccount('paybill_2')\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003epush();\n\n/****** OR ********/\n$response = $stk-\u003epush(10, 254722000000, 'Some Reference', 'Test Payment', 'paybill_2');\n```\n\nUsing `CustomerBuyGoodsOnline` command:\n\n```php\n\n$response = $stk-\u003erequest(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003esetCommand(CUSTOMER_BUYGOODS_ONLINE)\n    -\u003epush();\n\n/****** OR ********/\n$response = $stk-\u003epush(10, 254722000000, 'Some Reference', 'Test Payment', null, CUSTOMER_BUYGOODS_ONLINE);\n```\n\n\n##### Laravel\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\STK;\n\n$response = STK::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003epush();\n\n/****** OR ********/\n$response = STK::push(10, 254722000000, 'Some Reference', 'Test Payment');\n```\n\nUsing the `paybill_2` account:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\STK;\n\n$response = STK::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingAccount('paybill_2')\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003epush();\n\n$response = STK::push(10, 254722000000, 'Some Reference', 'Test Payment', 'paybill_2');\n```\n\nUsing the `CustomerGoodsOnline` command:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\STK;\n\n$response = STK::request(10)\n    -\u003efrom(254722000000)\n    -\u003eusingReference('Some Reference', 'Test Payment')\n    -\u003esetCommand(CUSTOMER_BUYGOODS_ONLINE) \n    -\u003epush();\n\n$response = STK::push(10, 254722000000, 'Some Reference', 'Test Payment', null, CUSTOMER_BUYGOODS_ONLINE);\n```\n\n### STK PUSH Transaction Validation\n#### validate(merchantReferenceId, account = null)\n\nValidate a C2B STK Push transaction.\n\n##### Vanilla\n\n```php\nuse SmoDav\\Mpesa\\C2B\\STK;\n\n$stk = new STK($core);\n    \n$response = $stk-\u003evalidate('ws_CO_16022018125');\n```\n\nUsing the `paybill_2` account:\n\n```php\n$response = $stk-\u003evalidate('ws_CO_16022018125', 'paybill_2');\n```\n\n##### Laravel\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\STK;\n\n$response = STK::validate('ws_CO_16022018125');\n```\n\nUsing the `paybill_1` account:\n\n```php\nuse SmoDav\\Mpesa\\Laravel\\Facades\\STK;\n\n$response = STK::validate('ws_CO_16022018125', 'paybill_2');\n```\n\n##### When going live, you should change the `default` value of the config file to the production account.\n\n## License\n\nThe M-Pesa Package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmodav%2Fmpesa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmodav%2Fmpesa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmodav%2Fmpesa/lists"}