FAQ
General Questions
What is the M-Pesa SDK?
The M-Pesa SDK is a PHP library that provides a simple and efficient way to integrate with the M-Pesa payment system. It handles all the complexities of API communication, authentication, and data formatting.
What are the requirements for using the SDK?
- PHP 7.4 or higher
- Composer
- M-Pesa API credentials (Consumer Key and Secret)
- SSL certificate for production use
How do I get started with the SDK?
- Install the SDK via Composer:
composer require mesa/php-mpesa
- Configure your environment:
$config = new Config();
$config->setBaseUrl("https://apisandbox.safaricom.et")
->setConsumerKey("your_consumer_key")
->setConsumerSecret("your_consumer_secret")
->setEnvironment('sandbox');
- Initialize the SDK:
$mpesa = new Mpesa($config);
Authentication
How do I get my API credentials?
- Register on the M-Pesa Developer Portal
- Create a new application
- Generate your Consumer Key and Secret
- Request access to the sandbox environment
How do I handle token expiration?
The SDK automatically handles token expiration and refresh. You don't need to manage tokens manually.
What should I do if authentication fails?
- Verify your credentials
- Check your internet connection
- Ensure you're using the correct environment (sandbox/production)
- Check the error logs for detailed information
Transactions
What types of transactions are supported?
- STK Push (Customer to Business)
- B2C (Business to Customer)
- C2B (Customer to Business)
- Transaction Status Query
- Account Balance Query
How do I handle failed transactions?
try {
$result = $mpesa->ussdPush();
if (!$result->isSuccessful()) {
// Handle failure
echo "Error: " . $result->getErrorMessage();
}
} catch (MpesaException $e) {
// Handle exception
echo "Error: " . $e->getMessage();
}
How do I check transaction status?
$result = $mpesa->checkTransactionStatus();
if ($result->isSuccessful()) {
echo "Status: " . $result->getTransactionStatus();
}
Callbacks
How do I set up callbacks?
- Configure callback URLs in your application:
$config->setCallbackUrl("https://your-domain.com/callback")
->setResultUrl("https://your-domain.com/result")
->setQueueTimeOutUrl("https://your-domain.com/timeout");
- Create callback endpoints:
// callback.php
$callbackData = json_decode(file_get_contents('php://input'), true);
$mpesa->processCallback($callbackData);
How do I secure my callback endpoints?
- Use IP whitelisting
- Validate callback data
- Verify signatures
- Implement proper error handling
What should I do if callbacks are not received?
- Check your server logs
- Verify your callback URLs are accessible
- Ensure your server can receive POST requests
- Check your firewall settings
Error Handling
What types of errors should I handle?
- Authentication errors
- Network errors
- Validation errors
- Transaction errors
- Callback errors
How do I implement proper error handling?
try {
$mpesa->authenticate();
} catch (AuthenticationException $e) {
// Handle authentication errors
} catch (NetworkException $e) {
// Handle network errors
} catch (ValidationException $e) {
// Handle validation errors
} catch (MpesaException $e) {
// Handle M-Pesa specific errors
} catch (Exception $e) {
// Handle unexpected errors
}
Logging
How do I configure logging?
$logger = new MpesaLogger();
$logger->setLogPath('/path/to/logs')
->setLogLevel('debug');
$config->setLogger($logger);
What information should I log?
- API requests and responses
- Authentication attempts
- Transaction details
- Error messages
- Callback data
How do I manage log files?
- Implement log rotation
- Set appropriate retention periods
- Monitor log size
- Regular log analysis
Testing
How do I test my integration?
- Use the sandbox environment
- Write unit tests
- Write integration tests
- Test error scenarios
How do I mock API responses?
$mockClient = new MockHttpClient();
$mockClient->setResponse(
'https://apisandbox.safaricom.et/oauth/v1/generate',
['access_token' => 'test_token']
);
$config->setHttpClient($mockClient);
Production
What should I do before going live?
- Test thoroughly in sandbox
- Enable SSL verification
- Use environment variables
- Implement proper error handling
- Set up monitoring
- Configure logging
How do I monitor my integration?
- Set up log aggregation
- Implement error alerts
- Monitor transaction success rates
- Track API response times
What security measures should I implement?
- Use HTTPS
- Validate all input
- Implement proper authentication
- Secure sensitive data
- Regular security audits
Support
Where can I get help?
- Check the documentation
- Review the examples
- Open an issue on GitHub
- Contact support
How do I report bugs?
- Check if it's a known issue
- Provide detailed information
- Include error logs
- Describe steps to reproduce
How do I contribute to the SDK?
- Fork the repository
- Create a feature branch
- Write tests
- Submit a pull request