Skip to content

Commit f42bc08

Browse files
committed
initial commit
0 parents  commit f42bc08

File tree

6,850 files changed

+790801
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,850 files changed

+790801
-0
lines changed

.env.example

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Application Environment
2+
APP_ENV=development # Options: development, staging, production
3+
4+
# Debug Mode (true for development, false for production)
5+
APP_DEBUG=false
6+
7+
# Logging Configuration
8+
LOG_LEVEL=debug # Options: debug, info, warning, error
9+
LOG_PATH=./logs
10+
11+
# M-Pesa API Version
12+
MPESA_API_VERSION=v1
13+
14+
# M-Pesa API Credentials
15+
MPESA_CONSUMER_KEY=consumer_key_here
16+
MPESA_CONSUMER_SECRET=consumer_secret_here
17+
MPESA_PASSKEY=passkey # For STK Push
18+
MPESA_SHORTCODE=yshortcode # Business Short Code
19+
20+
MPESA_INITIATOR_PASSWORD=your_initiator_password
21+
MPESA_CERTIFICATE_PATH=path/to/certificate.pem
22+
23+
# M-Pesa API Endpoints
24+
# Sandbox URLs (for development and testing)
25+
MPESA_BASE_URL_SANDBOX=https://sandbox.safaricom.co.ke
26+
MPESA_OAUTH_TOKEN_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/oauth/v1/generate
27+
MPESA_STK_PUSH_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/stkpush/v1/processrequest
28+
MPESA_STK_QUERY_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/stkpushquery/v1/query
29+
MPESA_C2B_REGISTER_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/c2b/v1/registerurl
30+
MPESA_C2B_SIMULATE_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/c2b/v1/simulate
31+
MPESA_B2C_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/b2c/v1/paymentrequest
32+
MPESA_B2B_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/b2b/v1/paymentrequest
33+
MPESA_TRANSACTION_STATUS_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/transactionstatus/v1/query
34+
MPESA_ACCOUNT_BALANCE_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/accountbalance/v1/query
35+
MPESA_REVERSAL_URL_SANDBOX=${MPESA_BASE_URL_SANDBOX}/mpesa/reversal/v1/request
36+
37+
# Production URLs (for live environment)
38+
MPESA_BASE_URL_PRODUCTION=https://api.safaricom.co.ke
39+
MPESA_OAUTH_TOKEN_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/oauth/v1/generate
40+
MPESA_STK_PUSH_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/stkpush/v1/processrequest
41+
MPESA_STK_QUERY_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/stkpushquery/v1/query
42+
MPESA_C2B_REGISTER_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/c2b/v1/registerurl
43+
MPESA_C2B_SIMULATE_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/c2b/v1/simulate
44+
MPESA_B2C_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/b2c/v1/paymentrequest
45+
MPESA_B2B_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/b2b/v1/paymentrequest
46+
MPESA_TRANSACTION_STATUS_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/transactionstatus/v1/query
47+
MPESA_ACCOUNT_BALANCE_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/accountbalance/v1/query
48+
MPESA_REVERSAL_URL_PRODUCTION=${MPESA_BASE_URL_PRODUCTION}/mpesa/reversal/v1/request
49+
50+
51+
# Callback URLs
52+
MPESA_STK_CALLBACK_URL=https://example.com/api/callbacks/stk
53+
MPESA_C2B_CONFIRMATION_URL=https://example.com/api/callbacks/c2b/confirmation
54+
MPESA_C2B_VALIDATION_URL=https://example.com/api/callbacks/c2b/validation
55+
MPESA_B2C_RESULT_URL=https://example.com/api/callbacks/b2c/result
56+
MPESA_B2C_TIMEOUT_URL=https://example.com/api/callbacks/b2c/timeout
57+
MPESA_B2B_RESULT_URL=https://example.com/api/callbacks/b2b/result
58+
MPESA_B2B_TIMEOUT_URL=https://example.com/api/callbacks/b2b/timeout
59+
MPESA_ACCOUNT_BALANCE_RESULT_URL=https://example.com/api/callbacks/accountbalance/result
60+
MPESA_ACCOUNT_BALANCE_TIMEOUT_URL=https://example.com/api/callbacks/accountbalance/timeout
61+
MPESA_REVERSAL_RESULT_URL=https://example.com/api/reversal/result
62+
MPESA_REVERSAL_TIMEOUT_URL=https://example.com/api/reversal/timeout
63+
MPESA_TRANSACTION_STATUS_RESULT_URL=https://example.com/api/status/result
64+
MPESA_TRANSACTION_STATUS_TIMEOUT_URL=https://example.com/api/status/timeout
65+
66+
67+
# Transaction Type Identifiers
68+
MPESA_COMMAND_ID_B2C_SALARY=SalaryPayment
69+
MPESA_COMMAND_ID_B2C_PAYMENT=BusinessPayment
70+
MPESA_COMMAND_ID_B2C_PROMOTION=PromotionPayment
71+
72+
# Security Settings
73+
MPESA_INITIATOR_NAME=your_initiator_name # For B2C, B2B, Transaction Status, etc.
74+
MPESA_SECURITY_CREDENTIAL=your_encrypted_security_credential # Base64 encoded
75+
76+
# Timeout settings (in seconds)
77+
HTTP_TIMEOUT=30
78+
79+
# Cache settings
80+
CACHE_ENABLED=true
81+
CACHE_DRIVER=file # Options: file, redis, memcached
82+
CACHE_PREFIX=mpesa_
83+
CACHE_TTL=3600 # Token caching time in seconds

LICENSE

Whitespace-only changes.

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# M-Pesa PHP Library
2+
3+
This library provides a simple and flexible way to integrate M-Pesa API services into your PHP application. It supports STK Push, C2B, B2C, transaction status queries, and reversals.
4+
5+
---
6+
7+
## Features
8+
- **STK Push**: Initiate Lipa Na M-Pesa Online payments.
9+
- **C2B**: Handle customer-to-business transactions.
10+
- **B2C**: Process business-to-customer payments.
11+
- **Transaction Status**: Query the status of transactions.
12+
- **Reversal**: Reverse transactions.
13+
14+
---
15+
16+
## Installation
17+
18+
Install the library via Composer:
19+
20+
```bash
21+
composer require apelimpesa/mpesa
22+
```
23+
24+
---
25+
26+
## Configuration
27+
28+
### Step 1: Copy the Example Environment File
29+
30+
The library includes an `.env.example` file with all required configuration variables. Copy this file to your project root as `.env`:
31+
32+
```bash
33+
cp vendor/apelimpesa/mpesa/.env.example .env
34+
```
35+
36+
### Step 2: Configure Your Environment Variables
37+
38+
Edit the `.env` file with your M-Pesa API credentials and settings:
39+
40+
```ini
41+
# ==============================================
42+
# M-PESA API CONFIGURATION
43+
# ==============================================
44+
45+
# Application Environment (development/production)
46+
APP_ENV=development
47+
48+
# M-Pesa API Credentials
49+
MPESA_CONSUMER_KEY=your_consumer_key
50+
MPESA_CONSUMER_SECRET=your_consumer_secret
51+
MPESA_SHORTCODE=your_shortcode
52+
MPESA_PASSKEY=your_passkey
53+
54+
# Security Credentials
55+
MPESA_INITIATOR_NAME=your_initiator_name
56+
MPESA_INITIATOR_PASSWORD=your_initiator_password
57+
MPESA_CERTIFICATE_PATH=path/to/your/cert.pem
58+
59+
# ==============================================
60+
# CALLBACK URLS
61+
# ==============================================
62+
63+
# C2B URLs
64+
MPESA_C2B_VALIDATION_URL=https://yourdomain.com/api/c2b/validate
65+
MPESA_C2B_CONFIRMATION_URL=https://yourdomain.com/api/c2b/confirm
66+
67+
# B2C URLs
68+
MPESA_B2C_RESULT_URL=https://yourdomain.com/api/b2c/result
69+
MPESA_B2C_TIMEOUT_URL=https://yourdomain.com/api/b2c/timeout
70+
71+
# Transaction Status URLs
72+
MPESA_TRANSACTION_STATUS_RESULT_URL=https://yourdomain.com/api/status/result
73+
MPESA_TRANSACTION_STATUS_TIMEOUT_URL=https://yourdomain.com/api/status/timeout
74+
75+
# Reversal URLs
76+
MPESA_REVERSAL_RESULT_URL=https://yourdomain.com/api/reversal/result
77+
MPESA_REVERSAL_TIMEOUT_URL=https://yourdomain.com/api/reversal/timeout
78+
```
79+
80+
### Step 3: Required Variables
81+
82+
| Variable | Description | Required For |
83+
|------------------------------|--------------------------------------------------|-----------------------|
84+
| `APP_ENV` | Application environment (`development`/`production`) | All operations |
85+
| `MPESA_CONSUMER_KEY` | Your M-Pesa API consumer key | All operations |
86+
| `MPESA_CONSUMER_SECRET` | Your M-Pesa API consumer secret | All operations |
87+
| `MPESA_SHORTCODE` | Your business shortcode | All operations |
88+
| `MPESA_PASSKEY` | Your Lipa Na M-Pesa Online passkey | STK Push |
89+
| `MPESA_INITIATOR_NAME` | B2C/B2B transaction initiator name | B2C/B2B |
90+
| `MPESA_INITIATOR_PASSWORD` | Encrypted initiator password | B2C/B2B |
91+
| `MPESA_CERTIFICATE_PATH` | Path to your M-Pesa certificate (if required) | Optional for security |
92+
93+
---
94+
95+
## Usage
96+
97+
### Step 1: Load Environment Variables
98+
99+
Ensure your application loads the `.env` file. For non-Laravel projects, use `vlucas/phpdotenv`:
100+
101+
```php
102+
require_once __DIR__.'/vendor/autoload.php';
103+
104+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
105+
$dotenv->load();
106+
```
107+
108+
### Step 2: Initialize the Library
109+
110+
Create an instance of the M-Pesa service:
111+
112+
```php
113+
use ApeliMpesa\Mpesa\Mpesa;
114+
115+
$mpesa = new Mpesa([
116+
'consumer_key' => getenv('MPESA_CONSUMER_KEY'),
117+
'consumer_secret' => getenv('MPESA_CONSUMER_SECRET'),
118+
'shortcode' => getenv('MPESA_SHORTCODE'),
119+
'passkey' => getenv('MPESA_PASSKEY'),
120+
'environment' => getenv('APP_ENV'), // 'development' or 'production'
121+
]);
122+
```
123+
124+
### Step 3: Perform Transactions
125+
126+
#### 1. STK Push (Lipa Na M-Pesa Online)
127+
128+
```php
129+
$response = $mpesa->stkPush([
130+
'amount' => 100,
131+
'phone' => '+254712345678',
132+
'reference' => 'Order123',
133+
'description' => 'Payment for Order123',
134+
'callback_url' => 'https://yourdomain.com/api/stk/callback',
135+
]);
136+
137+
if ($response->isSuccessful()) {
138+
echo "STK Push initiated successfully. CheckoutRequestID: " . $response->getCheckoutRequestID();
139+
} else {
140+
echo "Error: " . $response->getErrorMessage();
141+
}
142+
```
143+
144+
#### 2. Query Transaction Status
145+
146+
```php
147+
$response = $mpesa->queryTransactionStatus('CheckoutRequestID123');
148+
149+
if ($response->isSuccessful()) {
150+
echo "Transaction completed successfully.";
151+
} else {
152+
echo "Error: " . $response->getErrorMessage();
153+
}
154+
```
155+
156+
#### 3. Reverse a Transaction
157+
158+
```php
159+
$response = $mpesa->reverseTransaction([
160+
'transaction_id' => 'TransactionID123',
161+
'amount' => 100,
162+
'receiver' => '600000',
163+
'receiver_type' => 'Paybill',
164+
'callback_url' => 'https://yourdomain.com/api/reversal/callback',
165+
]);
166+
167+
if ($response->isSuccessful()) {
168+
echo "Transaction reversed successfully.";
169+
} else {
170+
echo "Error: " . $response->getErrorMessage();
171+
}
172+
```
173+
174+
---
175+
176+
## Testing with Sandbox
177+
178+
When `APP_ENV=development`, the library uses M-Pesa's sandbox environment. No real money is transferred during testing.
179+
180+
For production, set:
181+
182+
```ini
183+
APP_ENV=production
184+
```
185+
186+
---
187+
188+
## Security Notes
189+
190+
- Use different credentials for development and production environments.
191+
- Keep your `.env` file secure and avoid committing it to version control.
192+
193+
---
194+
195+
## Requirements
196+
197+
- PHP 7.4 or higher
198+
- Composer
199+
- cURL extension enabled
200+
201+
---
202+
203+
## License
204+
205+
This library is open-source and available under the [MIT License](LICENSE).

composer.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"name": "apelimpesa/mpesa-php",
3+
"description": "A comprehensive PHP library for integrating with the M-Pesa API",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"mpesa",
8+
"safaricom",
9+
"payment",
10+
"api",
11+
"mobile-money",
12+
"kenya",
13+
"africa"
14+
],
15+
"authors": [
16+
{
17+
"name": "Livingstone Apeli",
18+
"email": "[email protected]",
19+
"homepage": "https://apelisolutions.com",
20+
"role": "Software Developer"
21+
}
22+
23+
],
24+
"support": {
25+
"email": "[email protected]",
26+
"issues": "https://github.com/ApeliDev/mpesa-php/issues",
27+
"source": "https://github.com/ApeliDev/mpesa-php",
28+
"docs": "https://ApeliDev.github.io/mpesa-php"
29+
},
30+
"funding": [
31+
{
32+
"type": "github",
33+
"url": "https://github.com/sponsors/ApeliDev"
34+
},
35+
{
36+
"type": "patreon",
37+
"url": "https://www.patreon.com/ApeliDev"
38+
}
39+
],
40+
"minimum-stability": "stable",
41+
"prefer-stable": true,
42+
"require": {
43+
"php": ">=7.4",
44+
"guzzlehttp/guzzle": "^7.0",
45+
"monolog/monolog": "^2.9",
46+
"nesbot/carbon": "^2.0",
47+
"vlucas/phpdotenv": "^5.3"
48+
},
49+
"require-dev": {
50+
"phpunit/phpunit": "^9.5",
51+
"mockery/mockery": "^1.4",
52+
"squizlabs/php_codesniffer": "^3.6",
53+
"phpstan/phpstan": "^1.0",
54+
"friendsofphp/php-cs-fixer": "^3.0",
55+
"phpmd/phpmd": "^2.10"
56+
},
57+
"autoload": {
58+
"psr-4": {
59+
"ApeliMpesa\\Mpesa\\": "src/"
60+
}
61+
},
62+
"autoload-dev": {
63+
"psr-4": {
64+
"ApeliMpesa\\Mpesa\\Tests\\": "tests/"
65+
}
66+
},
67+
"scripts": {
68+
"test": "phpunit",
69+
"check-style": "phpcs -p --standard=PSR12 src tests",
70+
"fix-style": "phpcbf -p --standard=PSR12 src tests",
71+
"analyse": "phpstan analyse --level=7 src",
72+
"md": "phpmd src text cleancode,codesize,controversial,design,naming,unusedcode",
73+
"cs-fix": "php-cs-fixer fix --using-cache=no",
74+
"check-all": [
75+
"@check-style",
76+
"@analyse",
77+
"@md",
78+
"@test"
79+
]
80+
},
81+
"config": {
82+
"sort-packages": true,
83+
"optimize-autoloader": true,
84+
"allow-plugins": {
85+
"composer/package-versions-deprecated": true
86+
}
87+
},
88+
"extra": {
89+
"branch-alias": {
90+
"dev-main": "1.0-dev"
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)