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.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distributions

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

Uploaded Python 3

mpesapy-1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpesapy-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 f81217d50817cb39fba2f3098b08577c6a997a691debd3ffe398ff6b0df2f48a
MD5 d94f482e7c1728e457a54363ca4ab964
BLAKE2b-256 6fc74c581a22410bf61883366f2300592a097e582f1b31911f7c346dd7e0d1ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mpesapy-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 806ea49ab6898aa54495e639754de77011c949e7cfbe4b1e0cf11b569fad9ffc
MD5 32cc6c0e6ed241ff2ce5a496cde94f12
BLAKE2b-256 b3ac726cc222571915b13fbfd39ef1e8d0b1eb60e77a1ad1bafc9d31a5a70c42

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mpesapy-1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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-py3-none-any.whl
Algorithm Hash digest
SHA256 4d69a3aa1cc05be62485740bbbe4ab5b46fbb35f671aed91b5d9917e3c4c12c1
MD5 501db5a554ba1b5b5004610a473973e5
BLAKE2b-256 cd234131bf83d81147e257d9887594fcc188425536ee829044fa2eb79e8f4b9f

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