Skip to content

Conversation

@RafaelJohn9
Copy link
Member

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

  • Bug fix (non-breaking change that fixes an issue)
  • Tests (addition or improvement of tests)

How Has This Been Tested?

  • Added comprehensive unit tests for all affected schemas (B2BExpressCheckout, B2C, Reversal, TransactionStatus)
  • Test cases cover:
    • Single zero ("0")
    • Multiple zeros ("00000000")
    • Non-zero codes ("1", "-1")
    • Mixed codes ("00001")
    • Empty strings
    • Both string and integer code inputs
  • Verified that existing functionality remains unchanged for standard "0" success codes
  • All tests pass locally with both Python 3.9 and 3.12

Checklist

  • My code follows the project's coding style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

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.

…ess codes

- Update all `is_successful()` methods across B2B, B2C, Reversal, and Transaction Status schemas
- Implement robust success detection that treats any string of zeros (e.g., '0', '00000000') as success
- Add comprehensive unit tests covering zero, all-zeros, non-zero, mixed, negative, and empty codes
- Standardize import ordering in schema files
- Fix URL in B2C documentation
- Minor formatting improvements
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 18, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@RafaelJohn9 RafaelJohn9 merged commit 79dbd12 into develop Jan 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants