mpesa-php-sdk
mpesa-php-sdk copied to clipboard
feat: Enhance SDK usability for AI agents and programmatic use
This commit introduces several improvements to make the Mpesa SDK easier and more robust for AI agents and general programmatic interactions.
Key changes include:
-
Configuration Flexibility: The
Mpesaclass now accepts configuration (consumer key, secret, environment) via its constructor, reducing reliance on.envfiles for dynamic setups..envloading has been made safer, returning null instead of dying on missing variables. -
Improved Error Handling: Introduced custom exceptions:
MpesaConfigurationExceptionfor setup issues.MpesaNetworkExceptionfor cURL/network transport errors.MpesaApiExceptionfor errors from the M-Pesa API or malformed responses. API methods now throw these exceptions instead of returning error messages or dying. -
Standardized API Responses: All M-Pesa API call methods now return decoded PHP associative arrays for both successful responses and API errors, making them easier to work with.
getDataFromCallback()now also decodes JSON input to an associative array and throws an exception on failure.finishTransaction()now returns an array instead of directly outputting JSON, allowing frameworks to handle the response. -
Fluent B2C Transaction Builder:
- Added
B2CTransactionBuilder(accessible via\$mpesa->b2cTransaction()). - This provides a fluent, chainable interface for constructing B2C payment requests, improving readability and reducing parameter order errors.
- Added
-
Documentation Overhaul: The
readme.mdhas been extensively updated to cover all new features, including constructor configuration, error handling with exceptions, the B2C builder, and changes to callback methods. A new "Usage with AI Agents" section highlights the benefits of these changes. -
Unit Tests: Added PHPUnit tests covering the new configuration options, the B2C builder logic, and
finishTransactionbehavior. Test structure for API error handling and cURL interactions has been laid out but requires further mocking capabilities for full execution.composer.jsonupdated for PHPUnit and test autoloading.
These changes aim to provide a more developer-friendly, predictable, and robust experience when integrating with the M-Pesa API, especially in automated or AI-driven contexts.