Download the PHP package codexoft/mpesa-sdk without Composer
On this page you can find all versions of the php package codexoft/mpesa-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codexoft/mpesa-sdk
More information about codexoft/mpesa-sdk
Files in codexoft/mpesa-sdk
Package mpesa-sdk
Short Description A PHP SDK for seamless integration with M-Pesa payment gateway, providing easy-to-use methods for transactions and payment processing
License MIT
Informations about the package mpesa-sdk
M-Pesa PHP SDK
A comprehensive PHP SDK for integrating with Safaricom's M-Pesa payment services. This package provides a simple and elegant way to interact with various M-Pesa APIs, including STK Push, B2B, B2C, C2B, QR Code generation, tax remittance, and more.
Table of Contents
- Features
- Installation
- Configuration
- Available Methods
- STK Push
- STK Push Query
- QR Code Generation
- C2B URL Registration
- B2C Payments
- B2B Payments
- B2B Express Checkout
- Transaction Status
- Account Balance
- Transaction Reversal
- Tax Remittance
- Standing Orders (M-Pesa Ratiba)
- Error Handling
- Webhook Integration
- Advanced Usage
- Requirements
- Security Best Practices
- Testing
- Contributing
- License
- Support
- Credits
- Changelog
Features
- Complete M-Pesa API integration
- Environment support for both sandbox and production
- Automatic access token management
- Built-in phone number formatting
- Comprehensive error handling and validation
- Webhook handling support
- Certificate management for security credentials
- Support for all M-Pesa transaction types:
- Customer-to-Business (C2B)
- Business-to-Customer (B2C)
- Business-to-Business (B2B)
- STK Push (M-Pesa Express)
- QR Code Payments
- Tax Remittance
- Standing Orders
Installation
Install the package via Composer:
Configuration
Basic Configuration
Using Environment Variables (Recommended)
Create a .env file:
Load configuration from environment:
Basic Usage
Initialize the SDK:
Available Methods
STK Push
Initiate an STK push request (M-Pesa Express):
STK Push Query
Check the status of an STK push request:
QR Code Generation
Generate a dynamic QR code for payments:
C2B URL Registration
Register URLs for C2B payment notifications:
B2C Payments
Send money to customers (Business to Customer):
B2B Payments
Make payments to other businesses:
B2B Express Checkout
Initiate a B2B Express Checkout (USSD Push):
Transaction Status
Query the status of any transaction:
Account Balance
Check your business account balance:
Transaction Reversal
Reverse a completed transaction:
Tax Remittance
Process tax payments to KRA:
Standing Orders (M-Pesa Ratiba)
Create recurring payment orders:
Frequency options:
- 1: One Off
- 2: Daily
- 3: Weekly
- 4: Monthly
- 5: Bi-Monthly
- 6: Quarterly
- 7: Half Year
- 8: Yearly
Error Handling
The SDK implements comprehensive error handling:
Webhook Integration
Example webhook handler for callbacks:
Advanced Usage
Certificate Management
The SDK automatically handles certificates for different environments:
- Production:
/Certificates/ProductionCertificate.cer
- Sandbox:
/Certificates/SandboxCertificate.cer
Phone Number Formatting
The SDK automatically formats phone numbers to the required format:
Access Token Management
Access tokens are automatically generated and managed by the SDK. You don't need to handle token generation or refreshing manually.
Requirements
- PHP 8.0 or higher
- Extensions:
- curl
- json
- openssl
- Composer
- HTTPS enabled web server
- Valid M-Pesa API credentials
- Valid SSL certificate for production
Security Best Practices
-
Environment Configuration
- Use environment variables for sensitive data
- Never commit credentials to version control
- Use different credentials for sandbox and production
-
SSL/TLS
- Always use HTTPS for callbacks
- Keep certificates up to date
- Validate SSL certificates
-
Error Handling
- Log errors securely
- Don't expose sensitive information in error messages
- Implement proper error recovery
-
Data Validation
- Validate all input data
- Sanitize callback data
- Implement request signing if needed
- Access Control
- Implement IP whitelisting for callbacks
- Use strong passwords
- Rotate credentials regularly
Testing
Sandbox Testing
Production Testing
Test Phone Numbers
For sandbox testing, use these test phone numbers:
- 254708374149
- 254708374150
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin feature/my-new-feature
) - Create a new Pull Request
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
- For bugs and issues, create an issue in the GitHub repository
- For security issues, email [[email protected]]
- For more documentation, visit [https://developer.safaricom.co.ke/Documentation]
Community Support
- GitHub Issues
- Stack Overflow tag:
mpesa-sdk
Credits
- codexoft
- Martin Wainaina
- Safaricom M-Pesa API Team
Changelog
[1.0.0/1] - 2024-12-30
- Initial release
- Basic M-Pesa integration features
- Comprehensive documentation
[1.0.2] - 2024-12-31
- Added Mpesa Exception
- Some bugs fixes
[Unreleased]
- Future features and improvements