Skip to main content

Mpesa API SDK

Project description

MPESA REST API Python SDK

Language Build Status

Simple to use helper sdk for Python developers seeking to integrate MPESA API without much hustle. Supports Python 2 & 3.

Urls are found on urls.py and depending on the environment set sandbox or production urls are used.

To generate access token use your client and secret from M-Pesa api account and use oauth_generate_token method.

Installation

$ pip install mpesa-api-sdk

Usage

from mpesa.gateway import Mpesa

access_token = oauth_generate_token(consumer_key="Consumer Key", consumer_secret="Consumer Secret", grant_type="client_credentials", env="sandbox")

mpesa = Mpesa(access_token)

C2B Register Url

data = {
    "ShortCode": " " ,
    "ResponseType": " ",
    "ConfirmationURL": " ",
    "ValidationURL": " "
}

mpesa.c2b_register_url(data)

C2B Simulate Transaction

data = {
    "ShortCode": "",
    "CommandID": "CustomerPayBillOnline",
    "Amount": "",
    "Msisdn": "",
    "BillRefNumber": ""
}
mpesa.c2b_simulate_transaction(data)

B2B Payment Request

data = {
    "Initiator": "",
    "SecurityCredential": "",
    "CommandID": "", 
    "SenderIdentifierType": "",
    "RecieverIdentifierType": "", 
    "Amount": "",
    "PartyA": "",
    "PartyB": "",
    "AccountReference": "",
    "Remarks": "",
    "QueueTimeOutURL": "",
    "ResultURL": ""
}

mpesa.b2b_payment_request(data)

B2C Payment Request

data = {
    "InitiatorName": "",
    "SecurityCredential": "",
    "CommandID": "",
    "Amount": "",
    "PartyA": "",
    "PartyB": "",
    "Remarks": "",
    "QueueTimeOutURL": "" ,
    "ResultURL": "",
    "Occassion":  ""
}

mpesa.b2c_payment_request(data)

Transaction Status

data = {
    "Initiator": "",
    "SecurityCredential": "",
    "CommandID": "TransactionStatusQuery",
    "TransactionID": "",
    "PartyA": "",
    "IdentifierType": "1",
    "ResultURL": "",
    "QueueTimeOutURL": "",
    "Remarks": " ",
    "Occasion": " "
}

mpesa.transation_status_request(data)

Account Balance

data = {
    "Initiator": "",
    "SecurityCredential": "",
    "CommandID": "AccountBalance",
    "PartyA": "",
    "IdentifierType": "4",
    "Remarks": "",
    "QueueTimeOutURL": "",
    "ResultURL": ""
}

mpesa.account_balance_request(data)

Reversal Request

data = {
    "Initiator": "",
    "SecurityCredential": "",
    "CommandID":"TransactionReversal",
    "TransactionID": "",
    "Amount": "",
    "ReceiverParty": "",
    "RecieverIdentifierType":"11",
    "ResultURL": "",
    "QueueTimeOutURL": "",
    "Remarks": "",
    "Occasion": ""
}

mpesa.reversal_request(data)

NOTE: Access token generated from M-Pesa API expires after 3599 seconds

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

mpesa-sdk-1.0.1.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

mpesa_sdk-1.0.1-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

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