A Mpesa Daraja API library to quickly get started sending and receiving money from mpesa
Project description
Mpesasync
A asynchronous python library to the Mpesa Daraja API. Latest Release
Features
This includes the following:
- A python library to accept send and receive MPESA payments in less than 10 lines of code.
- A sample implementation of the library in fast api.
Installation
$ pip install mpesasync
Development
- Create a virtual environment
python -m venv venv
- Activate your virtual environment
$source venv\bin\activate
or in windows> venv\scripts\activate
- Install Poetry
pip install poetry
- Install project
poetry install
- Run tests
pytest
Getting started
To get started you need the following from the Mpesa Daraja Portal
[STK PUSH]
- Your consumer key.
- Your consumer secret.
- The business shortcode.
[B2C/B2B]
- Your organisation shortcode
- Initiator name
- Security credential
- QueueTimeOutURL
- Result url => This has to be a publicly accessible callback that mpesa will send transaction results to.
For testing purposes, you can get test credentials here. On the sandbox portal, create an new app and use the provided credentials.
Using the library
STK Push
- Initialise and authenticate the STKPush sdk
from mpesasync import Mpesa, MpesaEnvironment
from mpesasync.lipa_na_mpesa import STKPush
mpesa_app = STKPush(
Environment=MpesaEnvironment.production, # use sandbox to authenticate with sandbox credentials
BusinessShortCode=1234,
CallBackURL="https://mydomain.com/path",
PassKey="" # use the passkey obtained from the daraja portal
)
await mpesa_app.authorize(consumer_key="YOUR CONSUMER KEY",
consumer_secret="YOUR CONSUMER SECRET")
- Send an STKPush prompt
await mpesa_app.stk_push(
amount=1.0, phone_number="phone number"
)
The phone number can be any of +254XXXXXXXXX, 254XXXXXXXXX, 0XXXXXXXXX, the SDK will sanitise the phone numbers for you.
If the transaction is sucessfull, mpesa will send a confirmation to your configured callback url. You can also use the library to parse the json data. A callback implemented in FastAPI could look like.
## main.py
from mpesasync.contracts import STKPushResult
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.post("stkpush/callback")
def stk_push_callback(data: STKPushResult):
## do your zing
print(data)
return {"OK"}
Start the server
$ uvicorn main:app --reload
Business to Customer Payments
Use this SDK to disburse money to your customers
from mpesasync.mpesa_business.mpesa_business import *
from mpesasync.types import CommandId
mpesa_app = MpesaBusiness(InitiatorName="testapi",
SecurityCredential=MpesaBusiness.get_security_credential(
initiator_password="YOUR INITIATOR",
mpesa_environment=MpesaEnvironment.production
),
OrganizationShortcode="",
QueueTimeOutURL="https://mydomain.com/b2c/queue",
ResultUrl="https://mydomain.com/b2c/result",
Environment=MpesaEnvironment.production)
await mpesa_app.authorize(consumer_key="CONSUMER KEY", consumer_secret="CONSUMER SECRET")
await mpesa_app.business_to_customer(phoneNumber="Phone number",
amount=100,
commandId=CommandId.BusinessPayment
)
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
Built Distribution
File details
Details for the file mpesasync-1.0.0.tar.gz
.
File metadata
- Download URL: mpesasync-1.0.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0468099dc7b062d9a060e2988a78493d665fd23f6b7020072f7583eec28d4e74 |
|
MD5 | 956c50615e75def4b99cdbcf883efd11 |
|
BLAKE2b-256 | c675352b197424d87e15806dcace9db942c0ea099c0aa0eec26ec592912d156b |
Provenance
File details
Details for the file mpesasync-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: mpesasync-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 075e9e141d1282216898b81a280060646b131afc4be4a3cc63cce8c6536c51ea |
|
MD5 | 5096109f4282e96a36a6f4bef7e89d9b |
|
BLAKE2b-256 | 81e0abdeb04a8d6bc3490b15c56660b0901bb96fa1187cf4eb644ab8067c98ed |