Skip to main content

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.

Features

This includes the following:

  1. A python library to accept send and receive MPESA payments in less than 10 lines of code.
  2. 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]

  1. Your consumer key.
  2. Your consumer secret.
  3. The business shortcode.

[B2C/B2B]

  1. Your organisation shortcode
  2. Initiator name
  3. Security credential
  4. QueueTimeOutURL
  5. 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

  1. 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")
  1. 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.get("stkpush/callback")
def stk_push_callback(data: STKPushResult):
    ## do your zing
    print(data)
    return {"OK"}

Start the server

$ uvicorn main:app --reload

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

mpesasync-0.1.0b0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

mpesasync-0.1.0b0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file mpesasync-0.1.0b0.tar.gz.

File metadata

  • Download URL: mpesasync-0.1.0b0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.0 Windows/10

File hashes

Hashes for mpesasync-0.1.0b0.tar.gz
Algorithm Hash digest
SHA256 1cc4e8a1e2b152f60246caf3bce76a8dac80e42e936d3894875da404e86902c7
MD5 4e4be0da5a81e3b415190dcfe9d70a6d
BLAKE2b-256 b1a4995767e73bd8d3d325d2d76982ae2cd59326482b9989f036cd80b77f3420

See more details on using hashes here.

Provenance

File details

Details for the file mpesasync-0.1.0b0-py3-none-any.whl.

File metadata

  • Download URL: mpesasync-0.1.0b0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.0 Windows/10

File hashes

Hashes for mpesasync-0.1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 49877498b436df22191e94fa748730d79fc13bea2742217034c8da4907c7f058
MD5 6059f693b6746286d98fcd29c628ba2c
BLAKE2b-256 f2351f168cc38d093a7aa382ffc784bb05e88e920981b8d69bc950768b606071

See more details on using hashes here.

Provenance

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