An interaction of the Safaricom Daraja Api with Python
Project description
Mpesa Rest Api
A special interaction with the Safaricom daraja Api using python suitable for business payment integration. create your consumer key and consumer secret from the safaricom daraja developer's portal
Installation
pip install MpesaRest
Usage
Instantiate Business to client Lipa na Mpesa Stk Push
Prompt user to Accept Payment for your service using lipa na mpesa
from MpesaRest.mpesarest import StartService as Mpesa
from typing import Any, Dict
import pprint
app = Mpesa(
consumer_key='GfcDOBUOM4oFzQpmq6QUYL2TR8rJXhvM',
consumer_secret='66olbx4MCiDMfoIz',
business_code=174379,
passcode='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
call_back='https://myapp.co.ke/',
environment='development',
phone_number=254794784462,
BusinessShortCode=174379,
Accountreference='MyCompany'
)
pay: Dict[str, Any] = app.prompt_payment_for_service({
'phone': '254794784462',
'amount': 3000,
'description': 'pay for the service ...'
})
pprint.pprint(pay)
#### Check Transaction Status for transaction
status = app.check_lipa_na_mpesa_status(pay['CustomerID'])
pprint.pprint(status)
# One Can prompt for payment from multiple clients
mult = app.prompt_payment_for_service(
[
{
'phone': '254794784462',
'amount': 3000,
'description': 'Pay for my service'
},
{
'phone': '254794784462',
'amount': 6000,
'description': 'pay for cool project'
}
]
)
pprint.pprint(mult)
Reverse Mpesa Transaction
from MpesaRest import Mpesa
app = Mpesa(
consumer_key='GfcDOBUOM4oFzQpmq6QUYL2TR8rJXhvM',
consumer_secret='66olbx4MCiDMfoIz',
business_code=174379,
passcode='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
call_back='https://myapp.co.ke/',
environment='development',
phone_number=254794784462,
BusinessShortCode=174379,
Accountreference='MyCompany'
)
transaction = app.prompt_payment_for_service({
'name': 'lumuli',
'phone': '254794784462',
'amount': 3000
})
credential = "gtuVU97sOygJeUCC+22dnZTYVfSseHMmbzQydjzbeQQrKU9hFfEljKINBw4iIhDqan417UPquzdoBND2F6e7r/4emGYzLPK9OBlTkUKB+rZx+ttNFyw0kq2+k93JMcaAAS9rbu3dZSw8mE47EHLE9PNQ0V8qdp0xhcLpi0GQptwBLQPD9gzKvSqz/E0hg1YisKFtOZizQ2PadX9KqxLKFYD1No/UJEXYEyduemKe6WmI/T7m5llYzIZRu3AdCcAF4JU8vFP/GMAn0uJB/xlGf5+23VV7Q/O+l/mkMXaN401EHO9OygTWiSf3+c8BN7wwpQQUCDh3T+mzWKc74AMZ6w=="
app.reverse_transaction(3000, transaction['CustomerID'], security_credential=credential)
Request payment from clients
from MpesaRest.mpesarest import StartService as Mpesa
app = Mpesa(
consumer_key='GfcDOBUOM4oFzQpmq6QUYL2TR8rJXhvM',
consumer_secret='66olbx4MCiDMfoIz',
business_code=174379,
passcode='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
call_back='https://myapp.co.ke/',
environment='development',
phone_number=254794784462,
BusinessShortCode=174379,
Accountreference='MyCompany'
)
app.request_payment(254794784462, 3000, '')
Check Account Balance status
from MpesaRest.mpesarest import StartService as Mpesa
app = Mpesa(
consumer_key='GfcDOBUOM4oFzQpmq6QUYL2TR8rJXhvM',
consumer_secret='66olbx4MCiDMfoIz',
business_code=174379,
passcode='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
call_back='https://myapp.co.ke/',
environment='development',
phone_number=254794784462,
BusinessShortCode=174379,
Accountreference='MyCompany'
)
app.check_account_balance()
Contribution
Contribute by creating pull request
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
MpesaRest-0.0.8.tar.gz
(5.9 kB
view details)
Built Distribution
File details
Details for the file MpesaRest-0.0.8.tar.gz
.
File metadata
- Download URL: MpesaRest-0.0.8.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8962e13ba90f44ba13a60aef92f209ff56fd0d8fc6c6cd6be6de32e11c95e7e7 |
|
MD5 | 384e7861a4ed8463402d18e0bebd51b2 |
|
BLAKE2b-256 | f6849ba0afa4d08515c43c6a8b96ccb2b5732b2fa2e64baededdbb22b7429c17 |
Provenance
File details
Details for the file MpesaRest-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: MpesaRest-0.0.8-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ab5e7b75473723f872cb4f2083e3e6c658361246dd81ac5d95f914fd2fb269c |
|
MD5 | df35c9be21495c78a2f08ec529b8b07a |
|
BLAKE2b-256 | 40932310bb2f607f796f306db49586acc57239f6439ad64456ecb83512c6b922 |