Skip to main content

mainfinity-django-mpesa

PyPI version CI Coverage Python versions Django versions License: MIT

A production-hardened Django app for Safaricom's Daraja M-PESA API.

Extracted from production use in Zaruni and packaged for reuse across any Django project that needs to collect or send money via M-PESA.


Features

  • STK Push — trigger the M-PESA PIN prompt on a customer's phone
  • C2B — receive paybill/till payments with validation and confirmation callbacks
  • B2C — send payouts, salaries, or refunds to phone numbers
  • Transaction Status — query stuck payments for reconciliation
  • Account Balance — check your M-PESA business account balance
  • Reversal — reverse a transaction programmatically
  • Idempotent callbacks — Safaricom retries are handled safely; a payment is settled exactly once
  • IP allowlist middleware — rejects callbacks from non-Safaricom sources
  • Mock client — full test suite runs with zero network access; ships a MockDarajaClient for your own tests
  • Swappable models — subclass the abstract models and add your own fields (orders, users, wallets)
  • Django signals — hook into payment_confirmed, payout_completed, etc. without modifying library code

Requirements

  • Python 3.10, 3.11, or 3.12
  • Django 4.2 or 5.1
  • djangorestframework ≥ 3.14
  • requests ≥ 2.31
  • Celery ≥ 5.3 (optional — set USE_CELERY=False for synchronous processing)

Installation

pip install mainfinity-django-mpesa

# With Celery support:
pip install mainfinity-django-mpesa[celery]

Quick start

See the full quickstart guide to go from install to a working STK Push in under 10 minutes.

# 1. Add to INSTALLED_APPS
INSTALLED_APPS = [
    ...
    "django_mpesa",
]

# 2. Configure
MPESA = {
    "ENV": "sandbox",
    "CONSUMER_KEY": lambda: os.environ["MPESA_CONSUMER_KEY"],
    "CONSUMER_SECRET": lambda: os.environ["MPESA_CONSUMER_SECRET"],
    "SHORTCODE": "174379",
    "PASSKEY": lambda: os.environ["MPESA_PASSKEY"],
    "STK_CALLBACK_URL": "https://yourapp.com/mpesa/stk/callback/",
    "TRANSACTION_MODEL": "myapp.MpesaTransaction",
    "CALLBACK_LOG_MODEL": "myapp.MpesaCallbackLog",
}

# 3. Include URLs
urlpatterns = [
    path("mpesa/", include("django_mpesa.urls")),
]

# 4. Initiate a payment
from django_mpesa.services import STKPushService

txn = STKPushService().initiate(
    phone_number="254712345678",
    amount=100,
    account_reference="INV-001",
    transaction_desc="Payment",
)

# 5. React to the callback
from django.dispatch import receiver
from django_mpesa.signals import payment_confirmed

@receiver(payment_confirmed)
def on_payment(sender, transaction, **kwargs):
    order = transaction.order
    order.mark_paid()

Documentation

Full documentation at mainfinity-django-mpesa.readthedocs.io.

Contributing

See CONTRIBUTING.md. All PRs touching tasks.py must include or update the idempotency concurrency test.

License

MIT — see LICENSE.

Metadata

Release files for mainfinity-django-mpesa 0.1.1

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

Source distribution (sdist)

Source distribution for mainfinity-django-mpesa 0.1.1
File Size Uploaded
mainfinity_django_mpesa-0.1.1.tar.gz 32.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mainfinity-django-mpesa 0.1.1
File Interpreter ABI Platform
mainfinity_django_mpesa-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 77.0 kB

Release files / mainfinity_django_mpesa-0.1.1.tar.gz

Download URL mainfinity_django_mpesa-0.1.1.tar.gz
Size 32.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ca53ea5055f16ff8ae0839768f37ce4644ae77901aba09ba4cbef8bbc3f7c298
BLAKE2b-256 checksum
How to use checksums
14675d7c7b46045d6f6dec9a6c7d734d8b38fd2a789a5949deca6c0afffcf7f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 2, 2026.

Transparency log

Release files / mainfinity_django_mpesa-0.1.1-py3-none-any.whl

Download URL mainfinity_django_mpesa-0.1.1-py3-none-any.whl
Size 44.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89d5b8ab0fa4124335f6b1c4b5eec45afa50429fcb1127d10cf3b24b3c712e12
BLAKE2b-256 checksum
How to use checksums
bf3831b68d24cf2cf77cd050a7b930aa1f13429e44d324d12b4bbe4cfd788952
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 2, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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