Skip to content

Feat webhook#139

Open
watersRand wants to merge 3 commits into
Byte-Barn:masterfrom
watersRand:feat_webhook
Open

Feat webhook#139
watersRand wants to merge 3 commits into
Byte-Barn:masterfrom
watersRand:feat_webhook

Conversation

@watersRand

Copy link
Copy Markdown
Contributor

Description

This PR introduces a unified callback routing feature (MpesaClient.process_callback(payload)) that dynamically detects the signature and structure of incoming M-PESA webhook payloads (e.g., STK Push, Ratiba, B2C, Bill Manager) and automatically forwards them to their respective strongly-typed validation and parsing models.

Closes #133

Motivation:
Reduces cognitive load significantly for developers integrating the SDK. Instead of manually inspecting the payload structure in their web controllers to decide whether to call .process_stk_callback() or .process_ratiba_service_callback(), they can now pass any raw M-PESA JSON payload to a single, intuitive entry point.

Additionally, this PR fixes a documentation example in the Dynamic QR Code Callback section. In alignment with official Safaricom Daraja documentation, the RequestID field is omitted as it is not part of the standard Dynamic QR response schema.

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Refactor (code structure improvements, no new functionality)
  • Tests (addition or improvement of tests)
  • This change requires documentation update

How Has This Been Tested?

  • Unit Tests: Added a new test suite class TestUnifiedCallbackRouting inside tests/unit/test_mpesa_client.py to verify accurate structural sniffing and correct downstream schema generation across distinct payloads (STK Push, Ratiba, B2B, etc.).
  • Regression Testing: Executed all 25+ existing test cases locally to confirm zero breaking changes to explicit parser methods.
  • Bug Fix Verification: Verified the test_process_ratiba_service_callback list-traversal structure passes cleanly without throwing AttributeError.

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

Additional Context

The sniffing router handles nuanced payload definitions (such as differentiating structural commonalities like Result parameters between Account Balance, Tax, and B2C transactions) by inspect-matching signature keys natively embedded within Safaricom’s standard response parameters.

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.

[Feature] Add Webhook response validator inside the facade

1 participant