The KNet package was experiencing fatal errors when processing payment responses from the KNet gateway due to malformed or corrupted hexadecimal data being passed to PHP's hex2bin() function.
KNet Gateway → /knet/response → ResponseController → KnetResponseService::decryptAndParse() → KPayClient::decryptAES() → hex2ByteArray() → hex2bin()
- KNet sending corrupted/incomplete response data
- Network issues truncating the response
- Invalid characters in the encrypted payload
- Empty or null response data
- Character encoding issues
Created a specialized exception class that extends KnetException to handle hex validation errors with detailed debugging information.
Location: src/Exceptions/InvalidHexDataException.php
Features:
- Specific factory methods for different error types
- Detailed error information for debugging
- Hex data preservation for analysis
- Structured error details for logging
Completely rewrote the hex2ByteArray() method with robust validation:
Key Improvements:
- ✅ Input validation (null, empty, whitespace)
- ✅ Hex string length validation (must be even)
- ✅ Character validation (only valid hex characters)
- ✅ Minimum length validation for AES decryption
- ✅ Comprehensive error handling with try-catch
- ✅ Debug logging when enabled
- ✅ Detailed error analysis and reporting
New Methods:
generateDebugInfo()- Creates detailed debug informationanalyzeHexString()- Analyzes hex string for common issuesdebugHexData()- Utility method for debugging malformed datatestHexConversion()- Tests conversion on small samples
Improved error handling in the decryptAndParse() method:
Key Improvements:
- ✅ Enhanced request validation
- ✅ Specific handling for
InvalidHexDataException - ✅ Detailed logging with request context
- ✅ Debug mode support for raw data logging
- ✅ Graceful exception chaining
Updated the response controller to handle different exception types gracefully:
Key Improvements:
- ✅ Specific handling for
InvalidHexDataException - ✅ Specific handling for
AccessDeniedHttpException - ✅ User-friendly error messages
- ✅ Detailed logging with request context
- ✅ Proper error code categorization
Added comprehensive debugging options to config/knet.php:
New Configuration Options:
// Debugging Configuration
'debug_hex_conversion' => env('KNET_DEBUG_HEX_CONVERSION', false),
'debug_response_data' => env('KNET_DEBUG_RESPONSE_DATA', false),
'debug_transactions' => env('KNET_DEBUG_TRANSACTIONS', false),
'debug_encryption' => env('KNET_DEBUG_ENCRYPTION', false),
'debug_validation_failures' => env('KNET_DEBUG_VALIDATION_FAILURES', true),
'debug_hex_preview_length' => env('KNET_DEBUG_HEX_PREVIEW_LENGTH', 200),
'debug_auto_hex_analysis' => env('KNET_DEBUG_AUTO_HEX_ANALYSIS', false),
// Error Handling Configuration
'retry_hex_conversion' => env('KNET_RETRY_HEX_CONVERSION', true),
'max_hex_retry_attempts' => env('KNET_MAX_HEX_RETRY_ATTEMPTS', 2),
'store_failed_hex_data' => env('KNET_STORE_FAILED_HEX_DATA', false),
'hex_validation_mode' => env('KNET_HEX_VALIDATION_MODE', 'strict'),Add these to your .env file for debugging:
# Enable hex conversion debugging
KNET_DEBUG_HEX_CONVERSION=true
# Enable response data debugging
KNET_DEBUG_RESPONSE_DATA=true
# Enable validation failure logging
KNET_DEBUG_VALIDATION_FAILURES=true
# Set hex validation mode (strict, lenient, fallback)
KNET_HEX_VALIDATION_MODE=strictuse Asciisd\Knet\KPayClient;
// Debug malformed hex data
$debugInfo = KPayClient::debugHexData($suspiciousHexString);
logger()->info('Hex Debug Analysis:', $debugInfo);
// Generate debug information for errors
$debugInfo = KPayClient::generateDebugInfo($hexString, 'Manual analysis');use Asciisd\Knet\Exceptions\InvalidHexDataException;
use Asciisd\Knet\Services\KnetResponseService;
try {
$payload = KnetResponseService::decryptAndParse($request);
} catch (InvalidHexDataException $e) {
// Handle hex validation errors
logger()->error('Hex validation failed:', $e->getErrorDetails());
// Get detailed debug information
$debugInfo = $e->getDebugInfo();
$hexData = $e->getHexData();
// Handle gracefully...
}- No more fatal errors from
hex2bin() - Proper exception handling with detailed context
- User-friendly error messages
- Detailed logging helps identify root causes
- Hex data analysis for troubleshooting
- Request context preservation
- Prevents invalid data from causing crashes
- Multiple validation layers
- Character-by-character analysis
- Existing functionality remains unchanged
- Non-breaking changes
- Graceful degradation
- Handles edge cases effectively
- Meaningful error messages
- Comprehensive logging
Comprehensive unit tests have been created to verify the solution:
Test Coverage:
- ✅ Valid hex string conversion
- ✅ Empty/null input handling
- ✅ Odd length validation
- ✅ Invalid character detection
- ✅ Minimum length validation
- ✅ Whitespace trimming
- ✅ Debug utility functions
- ✅ Exception detail verification
- ✅ Case sensitivity handling
Run Tests:
php artisan test tests/Unit/KPayClientHexValidationTest.php- Update package version in
composer.json - Update
CHANGELOG.mdwith new features - Test with actual KNet responses
- Verify logging configuration
- Review error handling flows
- Monitor logs for hex validation issues
- Verify error handling works as expected
- Check user experience with error messages
- Analyze debugging data if issues occur
| Error Type | Description | Exception | User Message |
|---|---|---|---|
| Empty Data | Null or empty hex string | InvalidHexDataException::emptyData() |
No response from gateway |
| Invalid Characters | Non-hex characters in string | InvalidHexDataException::invalidCharacters() |
Corrupted response data |
| Odd Length | Hex string has odd number of chars | InvalidHexDataException::oddLength() |
Malformed response format |
| Too Short | Hex string shorter than minimum | InvalidHexDataException::corruptedData() |
Incomplete response data |
| Conversion Failed | hex2bin() returned false | InvalidHexDataException::invalidCharacters() |
Data processing failed |
-
Still getting hex2bin errors?
- Check if the new code is deployed
- Verify exception handling is working
- Enable debug logging to see detailed errors
-
Too much logging?
- Disable debug options in production
- Use
KNET_DEBUG_VALIDATION_FAILURES=falseto reduce noise - Adjust
KNET_DEBUG_HEX_PREVIEW_LENGTHto limit log size
-
Need to analyze failed hex data?
- Enable
KNET_STORE_FAILED_HEX_DATA=true - Use
KPayClient::debugHexData()for analysis - Check logs for detailed hex analysis
- Enable
- Debug logging can impact performance in high-traffic scenarios
- Disable unnecessary debug options in production
- The hex validation adds minimal overhead to normal operations
- Exception handling is only triggered on actual errors
This solution transforms a fatal hex2bin() error into a manageable exception with proper logging and user feedback, making your KNet package much more robust and production-ready.