mbongo 0.1.0__tar.gz
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.
- mbongo-0.1.0/.gitignore +8 -0
- mbongo-0.1.0/CHANGELOG.md +18 -0
- mbongo-0.1.0/LICENSE +21 -0
- mbongo-0.1.0/PKG-INFO +362 -0
- mbongo-0.1.0/README.md +333 -0
- mbongo-0.1.0/pyproject.toml +53 -0
- mbongo-0.1.0/src/mbongo/__init__.py +81 -0
- mbongo-0.1.0/src/mbongo/_airtel.py +617 -0
- mbongo-0.1.0/src/mbongo/_core.py +121 -0
- mbongo-0.1.0/src/mbongo/_mtn.py +319 -0
- mbongo-0.1.0/src/mbongo/airtel.py +203 -0
- mbongo-0.1.0/src/mbongo/errors.py +105 -0
- mbongo-0.1.0/src/mbongo/mtn.py +563 -0
- mbongo-0.1.0/src/mbongo/py.typed +0 -0
- mbongo-0.1.0/src/mbongo/pytest_plugin.py +22 -0
- mbongo-0.1.0/src/mbongo/testing.py +718 -0
- mbongo-0.1.0/tests/conftest.py +57 -0
- mbongo-0.1.0/tests/test_airtel_async_unit.py +99 -0
- mbongo-0.1.0/tests/test_airtel_mbongo.py +225 -0
- mbongo-0.1.0/tests/test_airtel_unit.py +619 -0
- mbongo-0.1.0/tests/test_mtn_async_unit.py +81 -0
- mbongo-0.1.0/tests/test_mtn_mbongo.py +220 -0
- mbongo-0.1.0/tests/test_mtn_unit.py +355 -0
mbongo-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
The `mbongo` Python package. Versions follow [Semantic Versioning](https://semver.org), on their own: the
|
|
4
|
+
Mbongo server has its own [changelog](https://github.com/chrismoussounda/mbongo/blob/main/CHANGELOG.md).
|
|
5
|
+
|
|
6
|
+
## 0.1.0 (2026-12-06)
|
|
7
|
+
|
|
8
|
+
First version, for Python 3.10 or later.
|
|
9
|
+
|
|
10
|
+
- `Mtn` and `AsyncMtn` for MTN MoMo: collections, disbursements and sandbox API users, with
|
|
11
|
+
`parse_mtn_callback`. The same code runs against Mbongo, MTN's sandbox and MTN Congo: only the mode changes.
|
|
12
|
+
- `Airtel` and `AsyncAirtel` for Airtel Money Congo: collections, refunds, disbursements, cash-in, cash-out, user
|
|
13
|
+
and balance, with `parse_airtel_callback`, which checks the callback hash. Sending the PIN needs
|
|
14
|
+
`pip install 'mbongo[airtel]'`.
|
|
15
|
+
- A lost answer raises `MtnNoAnswerError` or `AirtelNoAnswerError`, and an error whose `outcome_unknown` is true
|
|
16
|
+
means the payment may exist: ask its status with the same id before sending it again.
|
|
17
|
+
- `mbongo.testing` drives Mbongo from your tests (keys, apps, wallets, the payer's answer, fault rules, the
|
|
18
|
+
report), and the `mbongo` pytest fixture reads `MBONGO_URL` and `MBONGO_ADMIN_TOKEN`.
|
mbongo-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Christoffert Moussounda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mbongo-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mbongo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MTN MoMo and Airtel Money Congo clients that run the same code against the Mbongo mock, the operators' sandboxes and production
|
|
5
|
+
Project-URL: Homepage, https://github.com/chrismoussounda/mbongo/tree/main/sdks/python#readme
|
|
6
|
+
Project-URL: Repository, https://github.com/chrismoussounda/mbongo
|
|
7
|
+
Project-URL: Changelog, https://github.com/chrismoussounda/mbongo/blob/main/sdks/python/CHANGELOG.md
|
|
8
|
+
Author: Christoffert Moussounda
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: airtel-money,congo,mobile-money,mock,momo,mtn,payments,testing
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: Pytest
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: anyio>=4
|
|
20
|
+
Requires-Dist: httpx>=0.27
|
|
21
|
+
Provides-Extra: airtel
|
|
22
|
+
Requires-Dist: cryptography>=42; extra == 'airtel'
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: build==1.6.1; extra == 'dev'
|
|
25
|
+
Requires-Dist: mypy==2.4.1; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest==9.2.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff==0.17.6; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# mbongo (Python SDK)
|
|
31
|
+
|
|
32
|
+
MTN MoMo and Airtel Money Congo clients for Python that run the same code against three targets:
|
|
33
|
+
|
|
34
|
+
| mode | MTN | Airtel |
|
|
35
|
+
| --- | --- | --- |
|
|
36
|
+
| `mock` | [Mbongo](https://github.com/chrismoussounda/mbongo), the MTN and Airtel mock (your URL); XAF, or EUR for a `sandbox` API user | Mbongo (your URL) |
|
|
37
|
+
| `sandbox` | `https://sandbox.momodeveloper.mtn.com`, environment `sandbox`, EUR | Airtel's staging, `https://openapiuat.airtel.cg` |
|
|
38
|
+
| `live` | MTN Congo, `https://momoapi.momo.africa`, environment `mtncongo`, XAF | `https://openapi.airtel.cg` |
|
|
39
|
+
|
|
40
|
+
Only the options change between modes. An app built on the mock goes live with new keys, not a new integration.
|
|
41
|
+
|
|
42
|
+
Python 3.10 or later. Sync (`Mtn`, `Airtel`) and async (`AsyncMtn`, `AsyncAirtel`) clients on
|
|
43
|
+
[httpx](https://www.python-httpx.org), typed.
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
pip install mbongo
|
|
47
|
+
pip install 'mbongo[airtel]' # to send Airtel's PIN, for disbursements and cash-in
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import os
|
|
54
|
+
from mbongo import Mtn, MtnKeys
|
|
55
|
+
|
|
56
|
+
mtn = Mtn(
|
|
57
|
+
"mock", # "sandbox" or "live" later
|
|
58
|
+
base_url="http://localhost:8080", # mock mode only
|
|
59
|
+
collection=MtnKeys(
|
|
60
|
+
subscription_key=os.environ["MTN_COLLECTION_KEY"],
|
|
61
|
+
api_user=os.environ["MTN_API_USER"],
|
|
62
|
+
api_key=os.environ["MTN_API_KEY"],
|
|
63
|
+
),
|
|
64
|
+
callback_url="https://example.com/momo/callback",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
reference_id = mtn.collection.request_to_pay(1500, "242061234567", "order-42")
|
|
68
|
+
# Save reference_id with the order before anything else.
|
|
69
|
+
|
|
70
|
+
status = mtn.collection.wait_for(reference_id) # or wait for your callback
|
|
71
|
+
if status.status == "SUCCESSFUL":
|
|
72
|
+
... # deliver
|
|
73
|
+
elif status.status == "FAILED":
|
|
74
|
+
print(status.reason) # APPROVAL_REJECTED, EXPIRED, NOT_ENOUGH_FUNDS...
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`request_to_pay` returns once MTN has accepted the request (HTTP 202), not once the customer has paid. The customer
|
|
78
|
+
approves on their phone; follow the payment with `get_status`, `wait_for` or your callback.
|
|
79
|
+
|
|
80
|
+
The async client takes the same options and has the same calls, awaited:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from mbongo import AsyncMtn
|
|
84
|
+
|
|
85
|
+
async with AsyncMtn("live", collection=keys) as mtn:
|
|
86
|
+
reference_id = await mtn.collection.request_to_pay(1500, "242061234567", "order-42")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Options
|
|
90
|
+
|
|
91
|
+
| option | default |
|
|
92
|
+
| --- | --- |
|
|
93
|
+
| `mode` | required: `"mock"`, `"sandbox"` or `"live"` |
|
|
94
|
+
| `base_url` | required in mock mode; MTN's URL for the mode otherwise |
|
|
95
|
+
| `target_environment` | `sandbox` in sandbox mode, `mtncongo` otherwise. In mock mode, pass `sandbox` for an API user made in Mbongo's sandbox environment |
|
|
96
|
+
| `currency` | `EUR` for the `sandbox` environment, `XAF` otherwise |
|
|
97
|
+
| `collection`, `disbursement` | `MtnKeys(subscription_key, api_user, api_key)` for each product you use |
|
|
98
|
+
| `callback_url` | sent as `X-Callback-Url`; its host must be the API user's `providerCallbackHost` |
|
|
99
|
+
| `timeout` | 30 seconds for each step of a call (connect, send, read), as httpx counts it |
|
|
100
|
+
| `http_client` | your own `httpx.Client` (`httpx.AsyncClient` for `AsyncMtn`); you close it. Otherwise close the client, or use it as a context manager |
|
|
101
|
+
|
|
102
|
+
### Calls
|
|
103
|
+
|
|
104
|
+
Collection (asking customers to pay):
|
|
105
|
+
|
|
106
|
+
- `request_to_pay(amount, phone, external_id, *, currency=None, payer_message="", payee_note="", reference_id=None, callback_url=None)`:
|
|
107
|
+
returns the reference id
|
|
108
|
+
- `get_status(reference_id)`, `wait_for(reference_id, *, timeout=300, interval=5)`: an `MtnStatus`
|
|
109
|
+
(`status`, `amount`, `currency`, `external_id`, `payer`, `financial_transaction_id`, `reason`, ...; `raw` is
|
|
110
|
+
MTN's body). `wait_for` asks again after a 5xx, a 429 or no answer, and raises only once the time is up
|
|
111
|
+
- `get_balance()`, `is_active(id, type="msisdn")`: whether the account holder is active; `type` is `"msisdn"`
|
|
112
|
+
(`242061234567`), `"email"` or `"party_code"`. MTN's sandbox environment does not answer it; Mbongo answers
|
|
113
|
+
numbers only
|
|
114
|
+
|
|
115
|
+
Disbursement (payouts; nobody approves them):
|
|
116
|
+
|
|
117
|
+
- `transfer(...)`, `deposit(...)`: same arguments as `request_to_pay`, `phone` is the payee
|
|
118
|
+
- `refund(amount, reference_id_to_refund, external_id, ...)`: pays back a successful request to pay
|
|
119
|
+
- `get_status(kind, reference_id)`, `wait_for(kind, reference_id)` with `kind` `"transfer"`, `"deposit"` or `"refund"`
|
|
120
|
+
- `get_balance()`, `is_active(id, type="msisdn")` as for collection
|
|
121
|
+
|
|
122
|
+
Tokens are asked for you, kept until a minute before they expire, and asked again once if MTN refuses one.
|
|
123
|
+
|
|
124
|
+
Sandbox API users (MTN's sandbox, or Mbongo's `sandbox` environment; in production MTN's partner portal makes
|
|
125
|
+
them), with a subscription key only:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
mtn = Mtn("sandbox")
|
|
129
|
+
api_user = mtn.create_api_user(subscription_key, "example.com")
|
|
130
|
+
api_key = mtn.create_api_key(subscription_key, api_user)
|
|
131
|
+
mtn.get_api_user(subscription_key, api_user) # MtnApiUser(provider_callback_host, target_environment)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Retries: keep the reference
|
|
135
|
+
|
|
136
|
+
A request to pay or a payout can reach MTN even when its answer does not reach you. Sending it again under a
|
|
137
|
+
new reference can charge or pay twice. The SDK makes a UUID v4 reference for each call unless you give
|
|
138
|
+
`reference_id`:
|
|
139
|
+
|
|
140
|
+
- `MtnNoAnswerError` (timeout, network error or an answer that could not be read) or an `MtnError` with `outcome_unknown` (a 5xx): ask
|
|
141
|
+
`get_status` with the same reference. If MTN does not know it (404), send again **with the same
|
|
142
|
+
`reference_id`**, never a new one. If that gets the same error again, stop: MTN refused the call. It answers
|
|
143
|
+
some refusals with a 500, such as `INVALID_CURRENCY`.
|
|
144
|
+
- `MtnDuplicateError` (409): the reference was used before, so the payment exists. Ask its status.
|
|
145
|
+
- Any other `MtnError` (4xx): MTN refused the call. `status`, `code` and `body` say why.
|
|
146
|
+
- A failed token call comes before the payment is sent: an `MtnError` with `outcome_unknown` false, or the
|
|
147
|
+
httpx error when no answer came. Sending again is safe.
|
|
148
|
+
|
|
149
|
+
`external_id` is your own id. MTN does not refuse a repeat of it, so it does not protect you against double payments.
|
|
150
|
+
|
|
151
|
+
## Callbacks
|
|
152
|
+
|
|
153
|
+
MTN sends the callback as a PUT to the `callback_url` of the call. The body may not hold the reference, so put it
|
|
154
|
+
in the URL. MTN does not sign callbacks: anyone can send one, so confirm with `get_status` before delivering:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
import uuid
|
|
158
|
+
from mbongo import parse_mtn_callback
|
|
159
|
+
|
|
160
|
+
reference_id = str(uuid.uuid4()) # save it with the order before the call
|
|
161
|
+
mtn.collection.request_to_pay(
|
|
162
|
+
1500,
|
|
163
|
+
"242061234567",
|
|
164
|
+
"order-42",
|
|
165
|
+
reference_id=reference_id,
|
|
166
|
+
callback_url=f"https://example.com/momo/callback/{reference_id}",
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# In your web framework, for PUT /momo/callback/<ref>: answer 200 first, then
|
|
170
|
+
cb = parse_mtn_callback(request_body, ref) # raises ValueError on a body that is not a callback
|
|
171
|
+
confirmed = mtn.collection.get_status(cb.reference_id)
|
|
172
|
+
# act on confirmed.status, once per reference
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Callbacks can arrive late, twice or not at all, so keep polling for payments still pending.
|
|
176
|
+
|
|
177
|
+
## Airtel Money Congo
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
import os
|
|
181
|
+
from mbongo import Airtel
|
|
182
|
+
|
|
183
|
+
airtel = Airtel(
|
|
184
|
+
"mock", # "sandbox" or "live" later
|
|
185
|
+
base_url="http://localhost:8080", # mock mode only
|
|
186
|
+
client_id=os.environ["AIRTEL_CLIENT_ID"],
|
|
187
|
+
client_secret=os.environ["AIRTEL_CLIENT_SECRET"],
|
|
188
|
+
pin=os.environ.get("AIRTEL_PIN"), # only for disbursements and cash-in
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
id = airtel.collect(1500, "051234567", "order 42")
|
|
192
|
+
# Save id with the order before anything else.
|
|
193
|
+
|
|
194
|
+
s = airtel.wait_for_payment(id) # or wait for your callback
|
|
195
|
+
if s.status == "TS":
|
|
196
|
+
... # deliver; keep s.airtel_money_id to refund
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`collect` sends the USSD prompt and returns once Airtel has taken it, not once paid. Statuses: `TIP` in progress,
|
|
200
|
+
`TS` paid, `TF` failed, `TE` expired, `TA` ambiguous. `TA` is not a failure: ask again later. `wait_for_payment`
|
|
201
|
+
keeps asking through `TIP`, `TA` and lost or 5xx answers, until a final status or the time is up. `AsyncAirtel`
|
|
202
|
+
takes the same options and has the same calls, awaited.
|
|
203
|
+
|
|
204
|
+
| option | default |
|
|
205
|
+
| --- | --- |
|
|
206
|
+
| `mode`, `base_url`, `timeout`, `http_client` | as for MTN |
|
|
207
|
+
| `client_id`, `client_secret` | required: the app's keys |
|
|
208
|
+
| `pin` | the app's PIN, for disbursements and cash-in; encrypted before it is sent, which needs `mbongo[airtel]` |
|
|
209
|
+
| `public_key` | Airtel's RSA key in Base64, as the portal shows it; asked from Airtel when not given |
|
|
210
|
+
| `country`, `currency` | `CG`, `XAF` |
|
|
211
|
+
|
|
212
|
+
Calls: `collect(amount, phone, reference, id=None)`, `get_payment(id)`, `wait_for_payment(id, timeout=300,
|
|
213
|
+
interval=5)`, `refund(airtel_money_id)` (full amount, once), `disburse(amount, phone, reference, id=None)`
|
|
214
|
+
(returns the status, like `cash_in`; give its `id` to `get_disbursement`), `get_disbursement(id)`, `wait_for_disbursement(id)`, `get_balance()` (`amount` is a `Decimal`), `get_user(phone)`
|
|
215
|
+
(KYC: names, grade, `is_barred`, `registration_status`; an unknown number raises `AirtelError`). Amounts are an
|
|
216
|
+
`int`, `float`, `str` or `Decimal`. Numbers are sent national, without 242 or 00242: `+242 05 123 4567` becomes
|
|
217
|
+
`051234567` (`airtel_number`); a number with no digits raises `ValueError` before any call. Tokens last three
|
|
218
|
+
minutes; they are renewed for you.
|
|
219
|
+
|
|
220
|
+
`cash_in(amount, phone, reference, id=None)` pays the customer from the app's wallet (needs the PIN);
|
|
221
|
+
`cash_out(...)`, same arguments, takes from the customer's wallet with no prompt documented. Both return the
|
|
222
|
+
status: `TS` once Airtel's code says paid, otherwise the status as Airtel gives it. Ask it later with
|
|
223
|
+
`get_payment(id)`, like a collection's. A short customer raises `AirtelError` with code `DP01000001007`.
|
|
224
|
+
|
|
225
|
+
Retries work as for MTN, with your transaction `id` in place of the reference: on `AirtelNoAnswerError` or an
|
|
226
|
+
`AirtelError` with `outcome_unknown` (5xx, 408, an ambiguous `…00` code, `ROUTER117`, a 200 with no code), ask
|
|
227
|
+
the status of the same `id`. `AirtelDuplicateError` means the id was used before (disbursements, cash-in,
|
|
228
|
+
cash-out). Airtel's docs give no duplicate code for collections: a reused collection id comes back as a plain
|
|
229
|
+
`AirtelError`, so ask `get_payment` for that id before anything else. Refusals come as HTTP 200 with Airtel's code
|
|
230
|
+
in `code`, such as `DP00800001010` (unknown number) or `ROUTER115` (wrong PIN). On `ROUTER116` (PIN encrypted with
|
|
231
|
+
another key), the SDK fetches Airtel's key again and sends once more with the same id, unless you gave
|
|
232
|
+
`public_key`.
|
|
233
|
+
|
|
234
|
+
Refunds have no status call. On `AirtelNoAnswerError` from `refund`, check your balance before sending it again:
|
|
235
|
+
Mbongo refuses a second refund of one payment, but Airtel's docs don't say whether Airtel does.
|
|
236
|
+
|
|
237
|
+
Airtel posts callbacks to the one URL set in the app's settings. With a callback key there, check the `hash`.
|
|
238
|
+
Give the body as the text or bytes received, so the hash is checked on the bytes Airtel sent:
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
from mbongo import parse_airtel_callback
|
|
242
|
+
|
|
243
|
+
# In your web framework, for POST /airtel/callback: answer 200 first, then
|
|
244
|
+
try:
|
|
245
|
+
# Raises ValueError if the hash is wrong, or if the body has a hash and the key is missing or empty.
|
|
246
|
+
cb = parse_airtel_callback(request_body, os.environ.get("AIRTEL_CALLBACK_KEY"))
|
|
247
|
+
confirmed = airtel.get_payment(cb.id)
|
|
248
|
+
# act on confirmed.status, once per id
|
|
249
|
+
except ValueError:
|
|
250
|
+
pass # log it: the status poll still catches the payment
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Airtel's docs don't say which bytes the hash covers; the SDK follows Mbongo (the `transaction` object). Without a
|
|
254
|
+
key and a hash, anyone can send a callback: confirm with `get_payment` before delivering.
|
|
255
|
+
|
|
256
|
+
## Testing with Mbongo: `mbongo.testing`
|
|
257
|
+
|
|
258
|
+
`mbongo.testing` drives Mbongo itself: keys, customer wallets, the customer's answer, faults and the report of
|
|
259
|
+
your integration's mistakes. It works in mock mode only. Start Mbongo with an admin token:
|
|
260
|
+
|
|
261
|
+
```sh
|
|
262
|
+
docker run --rm -p 8080:8080 -e MBONGO_ADMIN_TOKEN=dev-admin ghcr.io/chrismoussounda/mbongo:latest
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
from mbongo import Mtn
|
|
267
|
+
from mbongo.testing import Mbongo
|
|
268
|
+
|
|
269
|
+
mb = Mbongo("http://localhost:8080", "dev-admin")
|
|
270
|
+
|
|
271
|
+
keys = mb.mtn.create_keys("collection", provider_callback_host="localhost")
|
|
272
|
+
mb.create_customer("mtn", "242061234567", "XAF", balance=50_000)
|
|
273
|
+
mb.report.start_run(operator="mtn", client=keys.api_user)
|
|
274
|
+
|
|
275
|
+
mtn = Mtn("mock", base_url=mb.url, collection=keys)
|
|
276
|
+
reference_id = mtn.collection.request_to_pay(1500, "242061234567", "o-1")
|
|
277
|
+
|
|
278
|
+
mb.mtn.approve(keys.api_user, reference_id) # the customer approves; or mb.mtn.refuse(...)
|
|
279
|
+
mtn.collection.wait_for(reference_id) # SUCCESSFUL
|
|
280
|
+
|
|
281
|
+
# Raises a ReportError (an AssertionError) listing your integration's mistakes, such as a
|
|
282
|
+
# payment whose status was never asked or a retry under a new reference.
|
|
283
|
+
mb.report.expect_clean(operator="mtn", client=keys.api_user)
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
For Airtel, the same with an app:
|
|
287
|
+
|
|
288
|
+
```python
|
|
289
|
+
from mbongo import Airtel
|
|
290
|
+
|
|
291
|
+
app = mb.airtel.create_app("my app", callback_url="http://localhost:3000/airtel/callback")
|
|
292
|
+
mb.create_customer("airtel", "051234567", "XAF", balance=50_000)
|
|
293
|
+
airtel = Airtel("mock", base_url=mb.url, client_id=app.client_id, client_secret=app.client_secret, pin=app.pin)
|
|
294
|
+
id = airtel.collect(1500, "051234567", "o-1")
|
|
295
|
+
mb.airtel.approve(app.client_id, id) # or mb.airtel.refuse(...)
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
Payouts, cash-in and refunds pay from the app's own wallet, which starts empty in Mbongo: fund it first (with
|
|
299
|
+
MTN and Airtel, the account is topped up outside the API). An MTN refund finds the payment only when the
|
|
300
|
+
disbursement keys were made with the same `partner` as the collection keys:
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
keys = mb.mtn.create_keys("collection", partner="shop")
|
|
304
|
+
payout_keys = mb.mtn.create_keys("disbursement", partner="shop")
|
|
305
|
+
mb.mtn.fund(payout_keys.api_user, 100_000)
|
|
306
|
+
mb.airtel.fund(app.client_id, 100_000)
|
|
307
|
+
|
|
308
|
+
mtn = Mtn("mock", base_url=mb.url, collection=keys, disbursement=payout_keys)
|
|
309
|
+
mtn.disbursement.refund(1500, reference_id, "refund-1") # reference_id: a successful request to pay
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
`AsyncMbongo` is the same, awaited.
|
|
313
|
+
|
|
314
|
+
### pytest
|
|
315
|
+
|
|
316
|
+
Installing the package adds a `mbongo` fixture: an `Mbongo` built from `MBONGO_URL` and `MBONGO_ADMIN_TOKEN`.
|
|
317
|
+
A test that asks for it is skipped when either is unset.
|
|
318
|
+
|
|
319
|
+
```python
|
|
320
|
+
def test_checkout(mbongo):
|
|
321
|
+
keys = mbongo.mtn.create_keys("collection")
|
|
322
|
+
...
|
|
323
|
+
mbongo.report.expect_clean(operator="mtn", client=keys.api_user)
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Faults
|
|
327
|
+
|
|
328
|
+
```python
|
|
329
|
+
# The next request to pay is made, but its answer is lost: request_to_pay raises MtnNoAnswerError.
|
|
330
|
+
mb.faults.add("mtn", "answer_lost", client=keys.api_user, operation="requesttopay")
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Faults: `answer_lost`, `error_after_money`, `slow`, `rate_limited`, `maintenance`, `token_revoked`, `stuck`,
|
|
334
|
+
`ambiguous`, `callback_never`, `callback_twice`, `callback_late`, `callback_before_answer`, described in
|
|
335
|
+
[docs/faults.md](https://github.com/chrismoussounda/mbongo/blob/main/docs/faults.md). The full admin API is in
|
|
336
|
+
[docs/openapi.json](https://github.com/chrismoussounda/mbongo/blob/main/docs/openapi.json).
|
|
337
|
+
|
|
338
|
+
## Going to sandbox, then live
|
|
339
|
+
|
|
340
|
+
1. `Mtn("sandbox", ...)`, with the keys from [MTN's developer portal](https://momodeveloper.mtn.com) (an API user
|
|
341
|
+
made through `/v1_0/apiuser`). Amounts are in EUR. Test numbers: `46733123450` fails with
|
|
342
|
+
`INTERNAL_PROCESSING_ERROR`, `46733123451` `APPROVAL_REJECTED`, `46733123452` `EXPIRED`, `46733123453` stays
|
|
343
|
+
`PENDING`, `46733123454` stays `CREATED`, `56733123453` succeeds. Other numbers fail with other reasons, such
|
|
344
|
+
as `46733123459` `INVALID_CALLBACK_URL_HOST`. The sandbox does not answer `is_active`, and sometimes not
|
|
345
|
+
`get_balance` (500 or 503).
|
|
346
|
+
2. `Mtn("live", ...)`, with the keys MTN Congo gives you. Nothing else changes.
|
|
347
|
+
|
|
348
|
+
For Airtel, `Airtel("sandbox", ...)` calls Airtel's staging with the app's staging keys, and `Airtel("live", ...)`
|
|
349
|
+
production. Not yet checked against Airtel's staging: only against Mbongo and Airtel's docs.
|
|
350
|
+
|
|
351
|
+
## Develop
|
|
352
|
+
|
|
353
|
+
```sh
|
|
354
|
+
pip install -e ".[dev,airtel]"
|
|
355
|
+
ruff check . && ruff format --check .
|
|
356
|
+
mypy
|
|
357
|
+
pytest # unit tests; set MBONGO_URL and MBONGO_ADMIN_TOKEN to run the tests against a running Mbongo too
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## License
|
|
361
|
+
|
|
362
|
+
MIT
|