Skip to main content

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

Project description

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.0
  • djangorestframework ≥ 3.14
  • requests ≥ 2.31
  • Celery ≥ 5.3 (optional — set USE_CELERY=False for synchronous processing)

Installation

pip install django-mpesa

# With Celery support:
pip install 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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mainfinity_django_mpesa-0.1.0.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mainfinity_django_mpesa-0.1.0-py3-none-any.whl (44.9 kB view details)

Uploaded Python 3

File details

Details for the file mainfinity_django_mpesa-0.1.0.tar.gz.

File metadata

  • Download URL: mainfinity_django_mpesa-0.1.0.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mainfinity_django_mpesa-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bc3868debb4b891c67e8f8a82f28360bd13e503dbc5cbfd27f794bca9dac0be8
MD5 a320129f6f8ff8aab754097deaf40908
BLAKE2b-256 3058de5c50615a7848d2851b0de3879b529a3281435bf361f2628ff5aafd22ea

See more details on using hashes here.

File details

Details for the file mainfinity_django_mpesa-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mainfinity_django_mpesa-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aaa45a3cbba3ef0592b7463a9c2963e01962e70a1f0fb86b39306a90c363fbd
MD5 c667148a024f798e6466326eef6d529c
BLAKE2b-256 57718da5f9edbb192a6aeccd5102e49f4758a73bc4c2ade74ff8d6fa2d3a46a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page