{"id":17966215,"url":"https://github.com/safaricom/mpesa-php-sdk","last_synced_at":"2025-05-16T04:05:44.062Z","repository":{"id":45878681,"uuid":"107126277","full_name":"safaricom/mpesa-php-sdk","owner":"safaricom","description":"A PHP sdk for the new Mpesa RESTful APIs","archived":false,"fork":false,"pushed_at":"2024-06-02T14:01:12.000Z","size":62,"stargazers_count":192,"open_issues_count":29,"forks_count":223,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-05-09T13:23:40.942Z","etag":null,"topics":["laravel","mpesa","payment-gateway","payment-integration"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/safaricom.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2017-10-16T12:45:57.000Z","updated_at":"2025-05-07T23:24:57.000Z","dependencies_parsed_at":"2024-06-18T15:39:14.166Z","dependency_job_id":null,"html_url":"https://github.com/safaricom/mpesa-php-sdk","commit_stats":{"total_commits":26,"total_committers":10,"mean_commits":2.6,"dds":0.5769230769230769,"last_synced_commit":"5effb808e79ba6b3d8631ef83c2b9b31fa450152"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safaricom%2Fmpesa-php-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safaricom%2Fmpesa-php-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safaricom%2Fmpesa-php-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safaricom%2Fmpesa-php-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/safaricom","download_url":"https://codeload.github.com/safaricom/mpesa-php-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254464895,"owners_count":22075570,"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":["laravel","mpesa","payment-gateway","payment-integration"],"created_at":"2024-10-29T13:08:25.746Z","updated_at":"2025-05-16T04:05:39.038Z","avatar_url":"https://github.com/safaricom.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Introduction**\n\nThis package seeks to help php developers implement the various Mpesa APIs without much hustle. It is based on the REST API whose documentation is available on https://developer.safaricom.co.ke.\n \n **Installation using composer**\u003cbr\u003e\n `composer require safaricom/mpesa`\u003cbr\u003e\n \n \n **Configuration**\u003cbr\u003e\n At your project root, create a .env file and in it set the consumer key and consumer secret as follows   \n `MPESA_CONSUMER_KEY= [consumer key]` \u003cbr\u003e\n `MPESA_CONSUMER_SECRET=[consumer secret]`\u003cbr\u003e\n `MPESA_ENV=[live or sandbox]`\u003cbr\u003e\n For Laravel users, open the Config/App.php file and add `\\Safaricom\\Mpesa\\MpesaServiceProvider::class` under providers and ` 'Mpesa'=\u003e \\Safaricom\\Mpesa\\MpesaServiceProvider::class` under aliases.\n  \n  _Remember to edit the consumer_key and consumer_secret values appropriately when switching between sandbox and live_\n\n  \n **Usage**\n \n **Confirmation and validation urls** \n\n**B2C Payment Request**\n \n This creates transaction between an M-Pesa short code to a phone number registered on M-Pesa.\n \n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$b2cTransaction=$mpesa-\u003eb2c($InitiatorName, $SecurityCredential, $CommandID, $Amount, $PartyA, $PartyB, $Remarks, $QueueTimeOutURL, $ResultURL, $Occasion);`\n\n\n\n**Account Balance Request**\n \nThis is used to enquire the balance on an M-Pesa BuyGoods (Till Number)\n\n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$balanceInquiry=$mpesa-\u003eaccountBalance($CommandID, $Initiator, $SecurityCredential, $PartyA, $IdentifierType, $Remarks, $QueueTimeOutURL, $ResultURL);`\n\n\n\n**Transaction Status Request**\nThis is used to check the status of transaction. \n\n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$trasactionStatus=$mpesa-\u003etransactionStatus($Initiator, $SecurityCredential, $CommandID, $TransactionID, $PartyA, $IdentifierType, $ResultURL, $QueueTimeOutURL, $Remarks, $Occasion);`\n\n\n\n**B2B Payment Request**\n\nThis is used to transfer funds between two companies.\n\n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$b2bTransaction=$mpesa-\u003eb2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber );`\n\n\n\n**C2B Payment Request**\n\nThis is used to Simulate transfer of funds between a customer and business.\n\n\n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$b2bTransaction=$mpesa-\u003ec2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber );`\n\n_Also important to note is that you should have registered validation and confirmation urls where the callback responses will be sent._\n\n\n\n**STK Push Simulation**\n\nThis is used to initiate online payment on behalf of a customer.\n\n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$stkPushSimulation=$mpesa-\u003eSTKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remarks);`\n\n\n\n**STK Push Status Query**\n\n This is used to check the status of a Lipa Na M-Pesa Online Payment.\n \n`$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n\n`$STKPushRequestStatus=$mpesa-\u003eSTKPushQuery($checkoutRequestID,$businessShortCode,$password,$timestamp);`\n\n\n\n\n**Callback Routes**\nM-Pesa APIs are asynchronous. When a valid M-Pesa API request is received by the API Gateway, it is sent to M-Pesa where it is added to a queue. M-Pesa then processes the requests in the queue and sends a response to the API Gateway which then forwards the response to the URL registered in the CallBackURL or ResultURL request parameter. Whenever M-Pesa receives more requests than the queue can handle, M-Pesa responds by rejecting any more requests and the API Gateway sends a queue timeout response to the URL registered in the QueueTimeOutURL request parameter.\n\n**Obtaining post data from callbacks**\n This is used to get post data from callback in json format. The data can be decoded and stored in a database.\n \n `$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n \n `$callbackData=$mpesa-\u003egetDataFromCallback();`\n  \n  **Finishing a transaction**\n  After obtaining the Post data from the callbacks, use this at the end of your callback routes to complete the transaction\n  \n  `$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n  \n  `$callbackData=$mpesa-\u003efinishTransaction();`\n\n\n  If validation fails, pass `false` to `finishTransaction()`\n\n  `$mpesa= new \\Safaricom\\Mpesa\\Mpesa();`\n  \n  `$callbackData=$mpesa-\u003efinishTransaction(false);`\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafaricom%2Fmpesa-php-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsafaricom%2Fmpesa-php-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafaricom%2Fmpesa-php-sdk/lists"}