├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── config.example.php ├── phpdoc.xml ├── phpunit.xml ├── src ├── Config.php ├── Transaction.php ├── TransactionResponse.php ├── helpers │ └── ValidationHelper.php └── interfaces │ ├── ConfigInterface.php │ ├── TransactionInterface.php │ └── TransactionResponseInterface.php └── tests ├── MPesaTest.php ├── ValidationTest.php └── config.test.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /docs/cache/ 3 | /vendor/ 4 | /tests/coverage/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Abdul Mueid Akhtar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to M-Pesa PHP API 2 | 3 | This project aims to provide an easy-to-use and up-to-date PHP wrapper for the M-Pesa Mozambique API. 4 | 5 | Target version of M-Pesa API: **v1x** 6 | 7 | ## Installation 8 | 9 | Install using composer: 10 | ``` 11 | composer require abdulmueid/mpesa 12 | ``` 13 | 14 | ## Usage 15 | 16 | 1. Load the configuration from file. 17 | ```php 18 | $config = \abdulmueid\mpesa\Config::loadFromFile('/path/to/config.php'); 19 | ``` 20 | See sample configuration file in examples folder. 21 | 22 | 2. Create a Transaction using the configuration. 23 | ```php 24 | $transaction = new \abdulmueid\mpesa\Transaction($config); 25 | ``` 26 | 27 | 3. Execute API operations and pass appropriate parameters. 28 | 29 | 1. Initiate a C2B payment collection. 30 | ```php 31 | $c2b = $transaction->c2b( 32 | float $amount, 33 | string $msisdn, 34 | string $reference, 35 | string $third_party_reference 36 | ); 37 | ``` 38 | 39 | 2. Initiate a B2C payment. 40 | ```php 41 | $b2c = $transaction->b2c( 42 | float $amount, 43 | string $msisdn, 44 | string $reference, 45 | string $third_party_reference 46 | ); 47 | ``` 48 | 49 | 3. Initiate a B2B payment. 50 | ```php 51 | $b2b = $transaction->b2b( 52 | float $amount, 53 | string $receiver_party_code, 54 | string $reference, 55 | string $third_party_reference 56 | ); 57 | ``` 58 | 59 | 2. Initiate a reversal. 60 | ```php 61 | $reversal = $transaction->reversal( 62 | float $amount, 63 | string $transaction_id, 64 | string $third_party_reference 65 | ); 66 | ``` 67 | 68 | 3. Query a transaction. 69 | ```php 70 | $query = $transaction->query( 71 | string $query_reference, 72 | string $third_party_reference 73 | ); 74 | ``` 75 | 4. Check Response 76 | 77 | All transactions return the `TransactionResponse` object. The object has the following public methods: 78 | 79 | 1. `getCode()` - Returns the response code i.e. `INS-0` 80 | 81 | 2. `getDescription()` - Returns the description. 82 | 83 | 3. `getTransactionID()` - Returns the transaction ID. 84 | 85 | 4. `getConversationID()` - Returns the conversation ID. 86 | 87 | 5. `getTransactionStatus()` - Returns the transaction status. Only populated when calling the `query()` transaction. 88 | 89 | 6. `getResponse()` - Returns the full response JSON object as received from M-Pesa servers. Good for debugging any issues or undocumented behaviors of the M-Pesa API. 90 | 91 | In a typical scenario, code to check for successful transactions should be as follows: 92 | 93 | ```php 94 | $c2b = $transaction->c2b(...); 95 | 96 | if($c2b->getCode() === 'INS-0') { 97 | // Transaction Successful, Do something here 98 | } 99 | ``` 100 | 101 | ## Testing 102 | This repo provides Unit Tests to validate the objects and their interaction with M-Pesa. 103 | 104 | To run tests, 105 | 1. Open the `phpunit.xml` file and add the require credentials/parameters as supplied by M-Pesa. 106 | 2. Run `phpunit` 107 | 3. Check the handset for USSD prompts to approve test transactions. 108 | 109 | All tests use 1MT as the test amount. 110 | 111 | 112 | ## License 113 | 114 | This library is release under the MIT License. See LICENSE file for details. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abdulmueid/mpesa", 3 | "description": "PHP SDK for M-Pesa Mozambique API", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Abdul Mueid Akhtar", 9 | "email": "abdul.mueid@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.2", 14 | "ext-curl": "*", 15 | "ext-json": "*", 16 | "ext-openssl": "*" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "abdulmueid\\mpesa\\": "src/" 21 | } 22 | }, 23 | "require-dev": { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "9e0f53be4f5aa060409c788af2039970", 8 | "packages": [], 9 | "packages-dev": [], 10 | "aliases": [], 11 | "minimum-stability": "stable", 12 | "stability-flags": [], 13 | "prefer-stable": false, 14 | "prefer-lowest": false, 15 | "platform": { 16 | "php": "^7.2", 17 | "ext-curl": "*", 18 | "ext-json": "*", 19 | "ext-openssl": "*" 20 | }, 21 | "platform-dev": [] 22 | } 23 | -------------------------------------------------------------------------------- /config.example.php: -------------------------------------------------------------------------------- 1 | 4 | * @copyright Copyright (c) Abdul Mueid akhtar 5 | * @license http://mit-license.org/ 6 | * 7 | * @link https://github.com/abdulmueid/mpesa-php-api 8 | */ 9 | 10 | return [ 11 | 'public_key' => '', 12 | 'api_host' => 'api.sandbox.vm.co.mz', 13 | 'api_key' => '', 14 | 'origin' => '', 15 | 'service_provider_code' => '', 16 | 'initiator_identifier' => '', 17 | 'security_credential' => '' 18 | ]; -------------------------------------------------------------------------------- /phpdoc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ./docs/cache 5 | 6 | 7 | ./docs 8 | 9 | 10 |