Skip to main content

Python Daraja

Description

Python Wrapper for handling payment requests through the Daraja MPESA API

Contribution

Usage

Installation

pip install python-daraja

Initial Setup

  • Set the following constants first before proceeding
from python_daraja import payment


payment.SHORT_CODE = "YOUR_SHORTCODE"
payment.PASSKEY = "YOUR PASSKEY"
payment.CONSUMER_SECRET = "YOUR CONSUMER SECRET"
payment.CONSUMER_KEY = "YOUR CONSUMER KEY"
payment.ACCOUNT_TYPE = "PAYBILL"  # Set to TILL to use BuyGoods instead of Pay Bill
  • The first function to call is one which would trigger an automatic STK Push on your customer's phone. There is no simpler way of knowing whether the customer has successfully paid or not (failures such as inputting the wrong PIN or just cancelling the request altogether). You are therefore advised to set up a simple server with an endpoint that will accept POST requests from Daraja API with the details of the transaction.
  • For the server, it must be secure (https instead of http), it must not be localhost nor 127.0.0.1:$PORT.
  • You can use services like ngrok to tunnel localhost to live secure server.
  • Your server's endpoint should also accept a POST request hence you may need to disable constraints such as CSRF (outside the scope of this project).
  • If at all you must have CSRF Protection in your server, then you need to allow requests originating from the following IP addresses (whitelisting):
    • 192.201.214.200
    • 196.201.214.206
    • 196.201.213.114
    • 196.201.214.207
    • 196.201.214.208
    • 196.201.213.44
    • 196.201.212.127
    • 196.201.212.128
    • 196.201.212.129
    • 196.201.212.136
    • 196.201.212.74
    • 196.201.212.69
  • You can now proceed by triggering the payment process; you should expect a Python Dict object with the results of the process. This will only tell you whether it was successful and not whether the customer has actually paid.
from python_daraja import payment


details = payment.trigger_stk_push(phone_number=2547123456, amount=1, callback_url='https://your-domain/callback/',
                                   description='Payment for services rendered',
                                   account_ref='Python Good PHP Bad and Co.')
print(details)
  • The MPESA Gateway will then send some POST data to your endpoint if the customer pays, otherwise, you will not receive any data showing that your customer hasn't paid a dime.

  • It is advisable to save the details received from the previous method call to a DB of your choice. Some of those details are used for other subsequent method calls especially in getting the transaction status of your payment requests.

  • In order to query the details of a payment request made through the automatic STK Push above:-

from python_daraja import payment


details = payment.query_stk_push(checkout_request_id='ws_CO_DMZ_123212312_2342347678234')
print(details)
  • Getting a 0 as the Response Code or Result Code generally means that the transaction was successful, any other digit signifies otherwise.

Remarks

  • Unfortunately as of the moment of release of this package, Safaricom has internal problems with other types of transactions such as C2B. Maintainers of the project will work on implementing more features when the issues are addressed.

Sponsor Python Daraja

Metadata

Release files for python-daraja 1.2.4

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

Source distribution (sdist)

Source distribution for python-daraja 1.2.4
File Size Uploaded
python-daraja-1.2.4.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-daraja 1.2.4
File Interpreter ABI Platform
python_daraja-1.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / python-daraja-1.2.4.tar.gz

Download URL python-daraja-1.2.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ed6e37dd5f8ddb04d9240fad00fce525ea1fb29d75412e9fa89e8a5f14933bd7
BLAKE2b-256 checksum
How to use checksums
09f06b2acf05b50fad904c0200fb6c867aae4c3094ac104871e08d22fbfc0e70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / python_daraja-1.2.4-py3-none-any.whl

Download URL python_daraja-1.2.4-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37775edd2cff1589dff54f806772a1e8be3d241c7617d154c17608243a755302
BLAKE2b-256 checksum
How to use checksums
66de3eccc30b1673db5e83fb52e6aefcedac7029bd8cef8a9d8f322fb19d1e73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

1.2.4 This release

2 release files

1.1.3

2 release files

1.0.3

2 release files

1.0.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