Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abdulmueid/mpesa-php-api
PHP library for M-Pesa API (Mozambique)
https://github.com/abdulmueid/mpesa-php-api
api library mpesa php php-library php7
Last synced: 3 months ago
JSON representation
PHP library for M-Pesa API (Mozambique)
- Host: GitHub
- URL: https://github.com/abdulmueid/mpesa-php-api
- Owner: abdulmueid
- License: mit
- Created: 2018-03-24T22:44:55.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-10T21:52:36.000Z (over 1 year ago)
- Last Synced: 2024-07-07T00:01:34.227Z (4 months ago)
- Topics: api, library, mpesa, php, php-library, php7
- Language: PHP
- Size: 838 KB
- Stars: 44
- Watchers: 9
- Forks: 25
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Welcome to M-Pesa PHP API
This project aims to provide an easy-to-use and up-to-date PHP wrapper for the M-Pesa Mozambique API.
Target version of M-Pesa API: **v1x**
## Installation
Install using composer:
```
composer require abdulmueid/mpesa
```## Usage
1. Load the configuration from file.
```php
$config = \abdulmueid\mpesa\Config::loadFromFile('/path/to/config.php');
```
See sample configuration file in examples folder.2. Create a Transaction using the configuration.
```php
$transaction = new \abdulmueid\mpesa\Transaction($config);
```
3. Execute API operations and pass appropriate parameters.1. Initiate a C2B payment collection.
```php
$c2b = $transaction->c2b(
float $amount,
string $msisdn,
string $reference,
string $third_party_reference
);
```
2. Initiate a B2C payment.
```php
$b2c = $transaction->b2c(
float $amount,
string $msisdn,
string $reference,
string $third_party_reference
);
```
3. Initiate a B2B payment.
```php
$b2b = $transaction->b2b(
float $amount,
string $receiver_party_code,
string $reference,
string $third_party_reference
);
```
2. Initiate a reversal.
```php
$reversal = $transaction->reversal(
float $amount,
string $transaction_id,
string $third_party_reference
);
```
3. Query a transaction.
```php
$query = $transaction->query(
string $query_reference,
string $third_party_reference
);
```
4. Check ResponseAll transactions return the `TransactionResponse` object. The object has the following public methods:
1. `getCode()` - Returns the response code i.e. `INS-0`
2. `getDescription()` - Returns the description.
3. `getTransactionID()` - Returns the transaction ID.
4. `getConversationID()` - Returns the conversation ID.
5. `getTransactionStatus()` - Returns the transaction status. Only populated when calling the `query()` transaction.
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.
In a typical scenario, code to check for successful transactions should be as follows:
```php
$c2b = $transaction->c2b(...);if($c2b->getCode() === 'INS-0') {
// Transaction Successful, Do something here
}
```## Testing
This repo provides Unit Tests to validate the objects and their interaction with M-Pesa.To run tests,
1. Open the `phpunit.xml` file and add the require credentials/parameters as supplied by M-Pesa.
2. Run `phpunit`
3. Check the handset for USSD prompts to approve test transactions.All tests use 1MT as the test amount.
## License
This library is release under the MIT License. See LICENSE file for details.