A django app to enable online mpesa payments.
Project description
django-mpesa-payments
django-mpesa-payments is a django app to enable mpesa transactions on the web. Currently it features only the STK Push functionality. The STK push will enable you to make payment request to users.
Quick start
-
Installation .. code-block::
pip install django-mpesa-payments
-
Install required libraries .. code-block::
pip install requests
-
Add "mpesa" to your INSTALLED_APPS setting like this .. code-block::
INSTALLED_APPS = [ ... 'mpesa', ]
-
Include the mpesa URLconf in your project urls.py like this:: .. code-block::
path('mpesa/', include('mpesa.urls')),
-
Run
python manage.py migrate
to create the mpesa models. -
Start the development server and visit http://127.0.0.1:8000/admin/ to add your paybill and developer account details for daraja. (you'll need the Admin app enabled).
APP ENDPOINTS
-
mpesa/request-stk-push/ This endpoint sends a payment request to a users phone
Below shows a test case for this endpoint. It demonstrates how this endpoint is used
.. code-block::
def test_send_stk_push(self): response = self.client.post( path='http://127.0.0.1:8000/mpesa/request-stk-push/', data={ 'account_number': 'test_account', # replace with the account number that is to receive funds 'amount': '100', 'phone_number': '712345678', # replace with user phone number. Notice that '+254' is not included. It is hardcoded in the app 'description': 'test_payment', # replace with adequate description } ) data = response.json() self.assertEqual(data['ResponseCode'], "0") self.assertEqual(data['ResponseDescription'], "Success. Request accepted for processing") self.assertEqual(data['CustomerMessage'], "Success. Request accepted for processing")
-
mpesa/stk-push/callback/ This endpoint receives the callback from safaricom servers. Safaricom calls this endpoint after processing the stk push. The structure of the post data sent by safaricom is demonstrated in the test case function below
.. code-block::
def test_callback_url(self): response = self.client.post( content_type='application/json', path='http://127.0.0.1:8000/mpesa/stk-push/callback/', data={ 'Body': { 'stkCallback': { 'MerchantRequestID': '<Merchant request ID>', 'CheckoutRequestID': '<Checkout request ID>', 'ResultCode': 1031, 'ResultDesc': 'Request cancelled by user' } } } ) data = response.json() self.assertEqual(data['ResultCode'], 0) self.assertEqual(data['ResultDesc'], 'Success')
-
mpesa/confirm-stk-push-payment/ This endpoint confirms an mpesa payment using the checkout request id Below shows a test case demonstrating how this endpoint is used.
.. code-block::
def test_confirm_mpesa_stk_push_payment(self): response = self.client.post( path='http://127.0.0.1:8000/mpesa/confirm-stk-push-payment/', data={ 'checkoutRequestID': '<Checkout request ID>' # checkout request id for a valid payment } ) data = response.json() self.assertEqual(data['ResponseCode'], "0") self.assertEqual(data['ResponseDescription'], "The service request has been accepted successsfully") self.assertEqual(data['ResultCode'], "0") self.assertEqual(data['ResultDesc'], "The service request is processed successfully.")
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
Hashes for django-mpesa-payments-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb36ab6eec51926c590c9c0c14d051a0fc823cabfa312f1306bc70215b90f27a |
|
MD5 | 717a202d8838b9bcc9a51ef8534d2be6 |
|
BLAKE2b-256 | d86bd450ecce2b716aeda16d0be533394d5561627b571c7d4b56f40b6aa8186a |