Mpesa API SDK
Project description
MPESA REST API Python SDK
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
Release history Release notifications | RSS feed
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.2b2.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for mpesa_sdk-1.0.2b2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f14647621d18dfad736fa93cd3b09bd19a3d931e46399e6074ba53b6bfdaf508 |
|
MD5 | 8061ac40b69dd5fd4d276d042033d371 |
|
BLAKE2b-256 | bc28513222b1542a17ee2bd156f015b7e8d82f639514e032552040ae4c4708b0 |