Coder Social home page Coder Social logo

martinmogusu / django-daraja Goto Github PK

View Code? Open in Web Editor NEW
60.0 8.0 68.0 159 KB

A python django library for interacting with the MPESA Daraja API

License: MIT License

Python 100.00%
daraja daraja-api daraja-framework mpesa-api mpesa mpesa-rest safaricom-api safaricom-mpesa-apis mobile-money python django django-library

django-daraja's Introduction

Daraja logo

django-daraja

This is a django library based on the Safaricom MPESA daraja API. This is a django library based on the Safaricom MPESA daraja API. Use it for a simplified experience, spend less time setting up...

Build Status Documentation Status License: MIT PyPI version PyPI - Python Version

Read the full documentation at https://django-daraja.readthedocs.io

MPESA Daraja API documentation can be found at https://developer.safaricom.co.ke

Installation

To install the package, run

$ pip install django_daraja

Examples

STK Push

An example, to send an STK push prompt to customer phone, then display response message

    from django_daraja.mpesa.core import MpesaClient

    def index(request):
        cl = MpesaClient()
        phone_number = '0700111222'
        amount = 1
        account_reference = 'reference'
        transaction_desc = 'Description'
        callback_url = 'https://api.darajambili.com/express-payment'
        response = cl.stk_push(phone_number, amount, account_reference, transaction_desc, callback_url)
        return HttpResponse(response)

On your browser, you will receive a message Success. Request accepted for processing on success of the STK push, and on the phone number specified you will receive an MPESA PIN prompt. Once the transaction is complete, you will receive a notification on the callback URL you provided. If you used the exact callback URL in the example above (i.e. https://api.darajambili.com/express-payment), you can head over to https://darajambili.com to view the notification received

B2C Payment

An example, to perform a BusinessPayment B2C (Business to Customer) transaction

    from django_daraja.mpesa.core import MpesaClient

    def index(request):
        cl = MpesaClient()
        phone_number = '0700111222'
        amount = 1
        transaction_desc = 'Business Payment Description'
        occassion = 'Test business payment occassion'
        callback_url = 'https://api.darajambili.com/b2c/result'
        response = cl.business_payment(phone_number, amount, transaction_desc, callback_url, occassion)
        return HttpResponse(response)

On your browser, you will receive a message Accept the service request successfully. on success of the transaction. Once the transaction is complete, you will receive a notification on the callback URL you provided. If you used the exact callback URL in the example above (i.e. https://api.darajambili.com/b2c/result), you can head over to https://darajambili.com to view the notification received

The full documentation of all supported API's can be found at https://django-daraja.readthedocs.io

django-daraja's People

Contributors

bedah-kym avatar claudeomosa avatar martinmogusu avatar vincod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-daraja's Issues

Invalid Access Token Error

I configured everything as guided and it worked. Then I come the next day and rerun the code without changing anything and I get the:
{ "requestId":"5115-78674050-1", "errorCode": "404.001.03", "errorMessage": "Invalid Access Token" } . So after waiting impatiently, it worked again. This keeps happening. Is there a possibility that it's lagging behind on updating the access token, coz you said that it does that automatically?

Default amount setting for the 'lipa na mpesa'

django_daraja/mpesa.core
In the stk_push function the amount variable is by default set to 1
Here is the rendered json data
data = { 'BusinessShortCode': business_short_code, 'Password': password, 'Timestamp': timestamp, 'TransactionType': transaction_type, 'Amount': '1', 'PartyA': party_a, 'PartyB': party_b, 'PhoneNumber': phone_number, 'CallBackURL': callback_url, 'AccountReference': account_reference, 'TransactionDesc': transaction_desc }

Unable to generate access token.

I have upgraded to version 1.3.0 and now the generate_access_token method is raising a Unable to generate access token error.

File "/home/bwaks/Desktop/tech/Job/views.py", line 418, in MakePaymentView
response = cl.stk_push(phone_number, amount, account_reference, transaction_desc, callback_url)

File "/home/bwaks/Desktop/tech/tech/lib/python3.10/site-packages/django_daraja/mpesa/core.py", line 115, in stk_push
'Authorization': 'Bearer ' + mpesa_access_token(),

File "/home/bwaks/Desktop/tech/tech/lib/python3.10/site-packages/django_daraja/mpesa/utils.py", line 176, in mpesa_access_token
access_token = generate_access_token()

File "/home/bwaks/Desktop/tech/tech/lib/python3.10/site-packages/django_daraja/mpesa/utils.py", line 149, in generate_access_token
raise MpesaError('Unable to generate access token')
django_daraja.mpesa.exceptions.MpesaError: Unable to generate access token

Invalid Credentials Error in Response

This is the Error I'm getting.
{ "requestId":"22759-3566117-1", "errorCode": "500.001.1001", "errorMessage": "Wrong credentials" }
Yet I have provided the correct consumer Key and Secrect.
What could be the issue.

Invalis Access token

{ "requestId":"1459-15094918-1", "errorCode": "404.001.03", "errorMessage": "Invalid Access Token" }

I set everything ok, my Api is live

phone number formatter to utilise regular expressions

if len(phone_number) < 9: raise IllegalPhoneNumberException('Phone number too short')
I believe the utility function for formatting phone numbers should be implemented using a regular expression, unless there was a specific reason not to do so

Transaction Status Api

Hi @martinmogusu and co. The library is great thanks a lot for it.
Have you considered adding the Transaction Status api to it?
If you like I can try making a PR for it.
Thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.