Skip to main content

Wrapper for M-pesa daraja api

Project description

Mpesapy

This wrapper seeks to help python developers implement the various Mpesa APIs without much hustle. It is based on the REST API (daraja) whose documentation is available on http://developer.safaricom.co.ke.

The wrapper implements the following endpoints:

  1. Authentication
  2. Lipa na M-Pesa Online Payment
  3. C2B
  4. B2C
  5. B2B
  6. Account Balance
  7. Transaction Status
  8. Reversal

Getting Started

Prerequisites

  1. Click here and create your app on the Mpesa daraja portal.
  2. Once that is done, you will be provided with a Consumer Key and a Consumer Secret
  3. For development and testing environment use sandbox for env
  4. For live environment use api for env

Installation:

pip install git+https://github.com/dennismwagiru/mpesapy.git

Usage

Import Mpesa and create object

from mpesapy import Mpesa
mpesa = Mpesa('sandbox', '600462', Consumer Key, Consumer Secret)
Generate Access Token
access_token = mpesa.get_access_token()
Lipa Na M-Pesa Online Payment API
res_json = mpesa.lipa_na_mpesa_online(Password='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919', 
                                      Timestamp='20180704203000', Amount='10', PartyA='254708374149', PartyB='174379', 
                                      PhoneNumber='254708374149', 
                                      CallBackURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU', 
                                      AccountReference='Test', TransactionDesc='Test')
C2B API

Register urls:

res_json = mpesa.c2b_register_url(ValidationURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU', 
                                  ConfirmationURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU')

Simulate Transaction:

res_json = mpesa.c2b_simulate(Amount=amount, MSISDN=phone_no, BillRefNumber=account_no)
B2C Payment Request
res_json = mpesa.b2c_payment_request(InitiatorName='testapi', Amount=10, PartyB='254708374149', Remarks='Test', 
                                     SecurityCredential='nR47eKS3OWCwOGJofwW4Vze5Y2r9VtiNF+YrxopbnxjRckpZHDp379XscBqPLibV6ZKhQ0OjUFRTR7cVnxLrhF4PDZr8Eg4+euuYL/HB9DQHom0kuDwvxUS+ctQsRZ8gBB8d+QYBqb1hzsBuxl2jNohQpeqVOZ1tb1UzGPnLQfAcQuf/x6q5Ze0orzvC2BmCw75GhJl4quZEG2Pou72PQD2IAiQSUOWYSHcJC/3oyYqtLx6Vl98F9Qjx+6oKZHXqokkWccf2vOyl6ApQ5BKubfUPVSa9ggl87ZdffNueifs60HAIHKtD77NyV4G3vXfKBxbm5Z9AqVLbPp6yXS9AAw==',
                                     QueueTimeOutURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU', 
                                     ResultURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU', Occassion='Test')
B2B Payment Request
res_json = mpesa.b2b_payment_request(Amount=10, PartyB='600000', RecieverIdentifierType=4, Remarks='Okay', 
                                     Initiator='testapi',
                                     SecurityCredential='nR47eKS3OWCwOGJofwW4Vze5Y2r9VtiNF+YrxopbnxjRckpZHDp379XscBqPLibV6ZKhQ0OjUFRTR7cVnxLrhF4PDZr8Eg4+euuYL/HB9DQHom0kuDwvxUS+ctQsRZ8gBB8d+QYBqb1hzsBuxl2jNohQpeqVOZ1tb1UzGPnLQfAcQuf/x6q5Ze0orzvC2BmCw75GhJl4quZEG2Pou72PQD2IAiQSUOWYSHcJC/3oyYqtLx6Vl98F9Qjx+6oKZHXqokkWccf2vOyl6ApQ5BKubfUPVSa9ggl87ZdffNueifs60HAIHKtD77NyV4G3vXfKBxbm5Z9AqVLbPp6yXS9AAw==', 
                                     QueueTimeOutURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU',
                                     ResultURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU', AccountReference='Test')
Account Balance Request
res_json = mpesa.balance(PartyA='600462', IdentifierType=4, Remarks='Okay', Initiator='testapi', 
                         SecurityCredential='nR47eKS3OWCwOGJofwW4Vze5Y2r9VtiNF+YrxopbnxjRckpZHDp379XscBqPLibV6ZKhQ0OjUFRTR7cVnxLrhF4PDZr8Eg4+euuYL/HB9DQHom0kuDwvxUS+ctQsRZ8gBB8d+QYBqb1hzsBuxl2jNohQpeqVOZ1tb1UzGPnLQfAcQuf/x6q5Ze0orzvC2BmCw75GhJl4quZEG2Pou72PQD2IAiQSUOWYSHcJC/3oyYqtLx6Vl98F9Qjx+6oKZHXqokkWccf2vOyl6ApQ5BKubfUPVSa9ggl87ZdffNueifs60HAIHKtD77NyV4G3vXfKBxbm5Z9AqVLbPp6yXS9AAw==', 
                         QueueTimeOutURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU',
                         ResultURL='https://putsreq.com/C1HAyC3fEEbl2UaEu6lU')
Transaction Status Request
Reversal Request

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

mpesapy-1.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

mpesapy-1.0.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file mpesapy-1.0.1.tar.gz.

File metadata

  • Download URL: mpesapy-1.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for mpesapy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c57e3ec1f28519589d5b95623bdee34aae92179f89a79aca8c23665a51c9de2a
MD5 744f25e68071aba22fe0ffcd4b5220ba
BLAKE2b-256 524ddaa1f76b9e99a1092c08209f0d5b7bd5252af9956c64f3a5836234ea0be2

See more details on using hashes here.

File details

Details for the file mpesapy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mpesapy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for mpesapy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 056de1fa17e5d06e9dfcd19a9050661cf3ae7db952f131ed8f092d892873c447
MD5 2e57e0b493ee0e227078ccb4041f061f
BLAKE2b-256 28bbbfb730d70655ff3646689eff6a0b9682c0e70ce10b4b6b6c13ad9136cd0b

See more details on using hashes here.

Supported by

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