clickpesa-python-sdk 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
clickpesa/_version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.2.0"
1
+ __version__ = "0.2.1"
clickpesa/security.py CHANGED
@@ -56,26 +56,50 @@ class SecurityManager:
56
56
  ).hexdigest()
57
57
 
58
58
  @staticmethod
59
- def verify_webhook(checksum_key: str, payload: dict, signature: str) -> bool:
59
+ def webhook_checksum(checksum_key: str, payload: dict) -> str:
60
60
  """
61
- Verify an incoming ClickPesa webhook signature.
61
+ The checksum ClickPesa puts in a webhook body.
62
62
 
63
- Uses ``hmac.compare_digest`` for constant-time comparison to prevent
64
- timing-based side-channel attacks.
63
+ ClickPesa webhooks look like ``{"event": "...", "data": {..., "checksum":
64
+ "...", "checksumMethod": "canonical"}}``. The checksum is
65
+ HMAC-SHA256(checksum_key, canonical compact JSON of ``data`` WITHOUT the
66
+ ``checksum`` and ``checksumMethod`` fields). The ``event`` envelope is
67
+ not signed.
68
+
69
+ Returns ``""`` when there is no key or no ``data`` object.
70
+ """
71
+ data = payload.get("data") if isinstance(payload, dict) else None
72
+ if not checksum_key or not isinstance(data, dict):
73
+ return ""
74
+ signed = {k: v for k, v in data.items() if k not in ("checksum", "checksumMethod")}
75
+ return SecurityManager.create_checksum(checksum_key, signed)
76
+
77
+ @staticmethod
78
+ def verify_webhook(checksum_key: str, payload: dict, signature: str | None = None) -> bool:
79
+ """
80
+ Verify an incoming ClickPesa webhook.
81
+
82
+ ClickPesa sends no signature header: the checksum is ``data.checksum``
83
+ in the body. ``signature`` is optional and, when given, is compared
84
+ instead of ``data.checksum`` (kept for callers that pass it).
85
+
86
+ Uses ``hmac.compare_digest`` for constant-time comparison.
65
87
 
66
88
  Args:
67
89
  checksum_key: Your application's checksum secret key.
68
90
  payload: The parsed webhook body dict.
69
- signature: The ``X-ClickPesa-Signature`` header value.
91
+ signature: Optional; defaults to ``payload["data"]["checksum"]``.
70
92
 
71
93
  Returns:
72
- ``True`` if the signature is valid, ``False`` otherwise.
94
+ ``True`` if the checksum is valid, ``False`` otherwise.
73
95
  """
74
- if not signature:
96
+ if signature is None:
97
+ data = payload.get("data") if isinstance(payload, dict) else None
98
+ signature = data.get("checksum") if isinstance(data, dict) else None
99
+ if not signature or not isinstance(signature, str):
75
100
  return False
76
-
77
- computed = SecurityManager.create_checksum(checksum_key, payload)
78
- return hmac.compare_digest(computed, signature)
101
+ computed = SecurityManager.webhook_checksum(checksum_key, payload)
102
+ return bool(computed) and hmac.compare_digest(computed, signature)
79
103
 
80
104
 
81
105
  __all__ = ["SecurityManager"]
clickpesa/webhooks.py CHANGED
@@ -5,9 +5,9 @@ Usage::
5
5
 
6
6
  from clickpesa import WebhookValidator
7
7
 
8
+ # The checksum travels inside the body (data.checksum) - no header.
8
9
  is_valid = WebhookValidator.verify(
9
10
  payload=request.json(),
10
- signature=request.headers["X-ClickPesa-Signature"],
11
11
  checksum_key="your-checksum-secret",
12
12
  )
13
13
  """
@@ -21,7 +21,7 @@ class WebhookValidator:
21
21
  """Static helper for validating ClickPesa webhook payloads."""
22
22
 
23
23
  @staticmethod
24
- def verify(payload: dict, signature: str, checksum_key: str) -> bool:
24
+ def verify(payload: dict, signature: str | None = None, checksum_key: str = "") -> bool:
25
25
  """
26
26
  Verify that an incoming webhook was genuinely sent by ClickPesa.
27
27
 
@@ -30,7 +30,8 @@ class WebhookValidator:
30
30
 
31
31
  Args:
32
32
  payload: Parsed JSON body of the webhook request.
33
- signature: Value of the ``X-ClickPesa-Signature`` header.
33
+ signature: Optional. ClickPesa sends the checksum in the body
34
+ (``data.checksum``); that is used when this is None.
34
35
  checksum_key: Your application's checksum secret key.
35
36
 
36
37
  Returns:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clickpesa-python-sdk
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Production-grade Python SDK for the ClickPesa API — sync & async, collections, payouts, BillPay and more
5
5
  Author-email: Jackson Linus <jacksonlinus95@gmail.com>
6
6
  License-Expression: MIT
@@ -466,14 +466,16 @@ asyncio.run(run_payments())
466
466
  from clickpesa import WebhookValidator
467
467
 
468
468
  # In your webhook endpoint (Flask / FastAPI / Django etc.)
469
+ # ClickPesa sends the checksum inside the body (data.checksum), not in a header.
469
470
  def webhook_handler(request):
470
471
  is_valid = WebhookValidator.verify(
471
472
  payload=request.json,
472
- signature=request.headers["X-ClickPesa-Signature"],
473
473
  checksum_key="YOUR_CHECKSUM_KEY",
474
474
  )
475
475
  if not is_valid:
476
- return {"error": "Invalid signature"}, 401
476
+ # Don't trust the payload. Either answer 401, or (safer for payments)
477
+ # confirm the status with the API before acting on it.
478
+ return {"error": "Invalid checksum"}, 401
477
479
 
478
480
  # Process event ...
479
481
  ```
@@ -1,11 +1,11 @@
1
1
  clickpesa/__init__.py,sha256=qKPzSjA9V5hMx27qHulRhAq5gJd56TwCBnxaH3hJfbs,4504
2
- clickpesa/_version.py,sha256=Zn1KFblwuFHiDRdRAiRnDBRkbPttWh44jKa5zG2ov0E,22
2
+ clickpesa/_version.py,sha256=HfjVOrpTnmZ-xVFCYSVmX50EXaBQeJteUHG-PD6iQs8,22
3
3
  clickpesa/async_client.py,sha256=7eK6YZ6BvZnQEZDawQtti1vLQyyRxW0eGy25pAC2eS8,11718
4
4
  clickpesa/client.py,sha256=4RMErEzeZXS0hL7x62pJmjuUCRrEB_62kuTHer4dkk4,11584
5
5
  clickpesa/exceptions.py,sha256=PJyEmOkP7UdsYJLQ5csZJG4k3WFTdZGTczgssSiFeSs,2311
6
6
  clickpesa/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- clickpesa/security.py,sha256=JXWZXZj9Ap9G_BPfjUSznzO2FIsxYylMdG7YW2g3ow8,2578
8
- clickpesa/webhooks.py,sha256=C7u3JKK6_B0tLp3W5U-FYHXF4mMMBDwdfuc_Ox8cZJw,1170
7
+ clickpesa/security.py,sha256=0f1w6t90MpZ_C1loAf9Rjl5PQFaTIqm9yMJejATnIvI,3885
8
+ clickpesa/webhooks.py,sha256=kxC2ng201NX6Sv9P1xuXNb9YaNGC2uFfnDlTpsfSkpA,1284
9
9
  clickpesa/services/__init__.py,sha256=s0gzgj-E9dAiW5FLZAe-KM67wfFDq6UrxvEUMrguFN8,639
10
10
  clickpesa/services/account.py,sha256=IFN6CRTpJY5XF5D-lERaF9YRFyhD-mLIFQpMsfwTq5c,2907
11
11
  clickpesa/services/billpay.py,sha256=3qIcQjcZHFSzlJpUTEboPtzv_75P4ijL9C_mLyVBFRA,15286
@@ -13,8 +13,8 @@ clickpesa/services/exchange.py,sha256=jv4PfT1svaKQrfRbUpxCkMwZdo5EWz5YevylNOvF03
13
13
  clickpesa/services/links.py,sha256=A3ZNU7Z-haiazqOX4VHApsdq8FOqsBIL8C_97mtw7W0,6113
14
14
  clickpesa/services/payments.py,sha256=zUPSraoxzGPFcvsdCfbNHnwgeNTZdlADozHO4mwkR0M,8579
15
15
  clickpesa/services/payouts.py,sha256=5FhfI9mJjZJbhN9502RwYZFLZ9BNpL7-ZNI-vBxBvWQ,17931
16
- clickpesa_python_sdk-0.2.0.dist-info/licenses/LICENSE,sha256=3JOYdzl7RSKg6-b0-yGz-LwssM-qVjYEAJ_GGWfVxb8,1070
17
- clickpesa_python_sdk-0.2.0.dist-info/METADATA,sha256=9bqzoKwueHenEpJ0RN1zLh_htf4-fAtrIFhxEng9UBE,16668
18
- clickpesa_python_sdk-0.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
19
- clickpesa_python_sdk-0.2.0.dist-info/top_level.txt,sha256=4ijtPYCQzYuF0upkzfKlku_Pf1C3ym-77M6Rg8MkJPY,10
20
- clickpesa_python_sdk-0.2.0.dist-info/RECORD,,
16
+ clickpesa_python_sdk-0.2.1.dist-info/licenses/LICENSE,sha256=3JOYdzl7RSKg6-b0-yGz-LwssM-qVjYEAJ_GGWfVxb8,1070
17
+ clickpesa_python_sdk-0.2.1.dist-info/METADATA,sha256=_2KrYuZduFiPMbJursAhZkMqFKNTAAuwjSd9vpKU56Y,16829
18
+ clickpesa_python_sdk-0.2.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
19
+ clickpesa_python_sdk-0.2.1.dist-info/top_level.txt,sha256=4ijtPYCQzYuF0upkzfKlku_Pf1C3ym-77M6Rg8MkJPY,10
20
+ clickpesa_python_sdk-0.2.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.0.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5