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.1.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for mpesa_sdk-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16bc6632d86305478e9eb8e8bdfa1ee4de9d8c62a955904ea974092f1e06b709 |
|
MD5 | 03df65c5716fb075e49550746a70e602 |
|
BLAKE2b-256 | 2e9d11a9b927fe3dd374d976674f81eafb1ccddaeabd787cc79561806d975c45 |