feat(schemas): enhance is_successful logic to handle zero-padded success codes #120
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses inconsistent handling of M-Pesa success codes across different API schemas. Previously,
is_successful()methods only checked for exact string equality with"0", but M-Pesa APIs sometimes return success codes as"00000000"or other zero-padded variations. This caused false negatives in transaction status checks.The changes implement a robust success detection logic that treats any string consisting entirely of zeros (e.g.,
'0','00000000') as successful, while properly rejecting mixed codes like'00001'or empty strings.Fixes #88
Type of Change
How Has This Been Tested?
B2BExpressCheckout,B2C,Reversal,TransactionStatus)"0")"00000000")"1","-1")"00001")"0"success codesChecklist
Additional Context
This fix ensures consistent behavior across all M-Pesa API integrations in the SDK. The implementation uses
code.strip("0") == "" and code != ""which efficiently handles all zero-only strings while rejecting edge cases like empty strings or whitespace-only inputs.The change is backward compatible since
"0"continues to work exactly as before, and it aligns with M-Pesa's documented behavior where success is indicated by any variation of zero codes.