Skip to main content

mtn-momo-collection

SDK Python non officiel pour l'API MTN Mobile Money (MoMo). Ce projet n'est ni affilié à MTN, ni approuvé par MTN.

  • Collections, Disbursements et Remittances
  • Jeton mis en cache et renouvelé automatiquement
  • Nouvelles tentatives avec le même X-Reference-Id, pour ne pas créer de second paiement
  • Callbacks traités comme non fiables et vérifiés auprès de MTN
  • Réconciliation des transactions en attente

Installation

Python 3.9 ou plus :

pip install mtn-momo-collection

Le paquet PyPI s'appelle mtn-momo-collection, mais le module Python reste mtn_momo.

Configuration

Variables d'environnement (voir .env.example, que le SDK ne charge pas lui-même) :

Variable Rôle
MOMO_SUBSCRIPTION_KEY Clé d'abonnement du produit
MOMO_API_USER / MOMO_API_KEY Identifiants API
MOMO_BASE_URL URL de l'API (sandbox par défaut)
MOMO_TARGET_ENVIRONMENT sandbox ou l'environnement du pays, ex. mtncongo
MOMO_CALLBACK_URL URL de callback, facultative
MOMO_TIMEOUT Délai HTTP en secondes, 30 par défaut
MOMO_MAX_RETRIES Nombre de nouvelles tentatives, 2 par défaut

En production, utilisez l'URL et l'environnement fournis par MTN pour votre pays. Pour la sandbox, provision_sandbox_user(subscription_key, callback_host) crée les identifiants.

Demander un paiement

from mtn_momo import Collections, Config, new_reference_id

momo = Collections(Config.from_env())
reference_id = new_reference_id()

save_order("ORDER-1", reference_id, status="PENDING")  # votre base, AVANT l'appel

momo.request_to_pay(
    reference_id=reference_id,
    amount="100",
    currency="EUR",          # sandbox ; devise locale en production
    external_id="ORDER-1",
    msisdn="46733123450",
)

Après une NetworkError ou une erreur HTTP 5xx, le résultat est inconnu. Ne créez pas de nouvelle référence : vérifiez celle qui est enregistrée.

Traiter un callback

Un callback est un signal, pas une preuve. Exemple Flask :

from mtn_momo import parse_callback

@app.post("/momo/callback")
def momo_callback():
    try:
        callback = parse_callback(request.get_data())
    except ValueError:
        return "", 400

    order = find_order(callback.external_id)
    if order is None:
        return "", 200

    payment = momo.verify(
        order.reference_id,
        external_id=order.id,
        amount=order.amount,
        currency=order.currency,
    )
    if payment.status.is_final:
        finalize_once(order.id, payment)
    return "", 200

Le SDK ne stocke rien : l'idempotence de la livraison relève de votre base. finalize_once doit être une mise à jour conditionnelle et atomique :

UPDATE orders SET status = :status WHERE id = :id AND status = 'PENDING';

Ne livrez que si le statut est SUCCESSFUL et qu'une ligne a été modifiée.

Réconcilier

À lancer périodiquement (cron, Celery...) pour les callbacks perdus :

from mtn_momo import reconcile

def check(reference_id):
    order = find_order_by_reference(reference_id)
    return momo.verify(
        reference_id,
        external_id=order.id,
        amount=order.amount,
        currency=order.currency,
    )

for result in reconcile(pending_reference_ids(), check):
    if result.is_final:
        finalize_once_by_reference(result.reference_id, result.transaction)
    elif result.error:
        log.warning("%s: %s", result.reference_id, result.error)

Développement

pip install -e ".[dev]"
ruff check .
mypy
pytest
python -m build

Les tests simulent HTTP et n'utilisent aucun identifiant.

Licence

MIT

Metadata

Release files for mtn-momo-collection 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mtn-momo-collection 0.1.0
File Size Uploaded Provenance Actions
mtn_momo_collection-0.1.0.tar.gz 11.7 kB With provenance Details for mtn_momo_collection-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mtn-momo-collection 0.1.0
File Interpreter ABI Platform Actions
mtn_momo_collection-0.1.0-py3-none-any.whl Python 3 none any Details for mtn_momo_collection-0.1.0-py3-none-any.whl

Total release size: 24.4 kB

Release files / mtn_momo_collection-0.1.0.tar.gz

Download URL mtn_momo_collection-0.1.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3ed785801221f689e4e38236e44b2b50ca8d4d8dc637235de431c1cc73d49ac3
BLAKE2b-256 checksum
How to use checksums
c367b61a37cb06bfe07e79db1144374859f46351c691fc7b43c435ab020525bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / mtn_momo_collection-0.1.0-py3-none-any.whl

Download URL mtn_momo_collection-0.1.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
444031f1e1c925eeb39e705db3fd0929a1032a3f96c33faad4acae8219d0e088
BLAKE2b-256 checksum
How to use checksums
693ee72ccfaf764db85ca264ce50d2417795362ce06acf5cf82a2ccc7a9af81e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for mtn-momo-collection 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mtn_momo_collection-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page