Skip to main content

A wrapper library for the Daraja Mpesa API

Project description

MPESA CONNECT

A wrapper library for the Daraja Mpesa API

Language

Features

  • Authorization
  • Mpesa Express
    • STK Push
    • Query
  • Customer To Business (C2B)
    • Register URL
    • Simulate
  • Business To Customer (B2C)
  • Account Balance
  • Transaction Status

Installation

$ pip install mpesa-connect

Usage

NOTE: Before you start, make sure to go through the official Daraja Mpesa API documentation

Create an app instance.

from mpesa_connect import App

# Sandbox
app = App.create_sandbox(consumer_key=..., consumer_secret=...)

# Production
app = App.create_production(consumer_key=..., consumer_secret=...)

Generate an authorization token.

from mpesa_connect import Authorization

auth = Authorization(app)
result = auth.generate_token()
access_token = result.access_token

You can attach this token to the service instance or include it as an argument to the api methods calls

Mpesa Express

STK Push

from mpesa_connect import STKPush

stk = STKPush(app, access_token=access_token)
result = stk.process_request(
    business_short_code=...,
    phone_number=...,
    amount=...,
    call_back_url=...,
    account_reference=...,
    transaction_desc=...,
    password=...,
    timestamp=...,
)

Query

result = stk.query(
    business_short_code=...,
    checkout_request_id=...,
    password=...,
)

You can use the generate_password helper to create a password

from mpesa_connect.utils import generate_password

password = generate_password(
    business_short_code=....,
    pass_key=...,
    timestamp=...,
)

Alternatively, you can include the pass_key argument in place of password to auto generate the password

Customer To Business (C2B) API

Register URL

from mpesa_connect import C2B
from mpesa_connect.enums import ResponseType, TransactionType

c2b = C2B(app, access_token=access_token)
result = c2b.register_url(
    short_code=...,
    validation_url=...,
    confirmation_url=...,
    response_type=ResponseType.COMPLETED,
)

Simulate

result = c2b.simulate(
    short_code=...,
    command_id=TransactionType.CUSTOMER_PAY_BILL_ONLINE,
    amount=...,
    msisdn=...,
    bill_ref_number=...,
)

Business To Customer (B2C) API

from mpesa_connect import B2C
from mpesa_connect.enums import TransactionType

b2c = B2C(app, access_token=access_token)
result = b2c.payment_request(
    initiator_name=...,
    security_credential=...,
    amount=...,
    command_id=TransactionType.BUSINESS_PAYMENT,
    party_a=...,
    party_b=...,
    queue_time_out_url=...,
    result_url=...,
    remarks=...,
    occassion=...,
)

Account Balance API

from mpesa_connect import AccountBalance
from mpesa_connect.enums import TransactionType, IdentifierType

ab = AccountBalance(app, access_token=access_token)
result = ab.query(
    initiator=...,
    security_credential=...,
    command_id=TransactionType.ACCOUNT_BALANCE,
    identifier_type=IdentifierType.ORGANIZATION_SHORT_CODE,
    party_a=...,
    queue_time_out_url=...,
    result_url=...,
    remarks=...,
)

Transaction Status API

from mpesa_connect import TransactionStatus
from mpesa_connect.enums import TransactionType, IdentifierType

ts = TransactionStatus(app, access_token=access_token)
result = ts.query(
    initiator=...,
    security_credential=...,
    transaction_id=...,
    command_id=TransactionType.TRANSACTION_STATUS_QUERY,
    identifier_type=IdentifierType.ORGANIZATION_SHORT_CODE,
    party_a=...,
    queue_time_out_url=...,
    result_url=...,
    remarks=...,
    occassion=...,
)

All API methods return a result object with a response property which is a requests.Response object and other various properties corresponding to the json body of the response

Running Tests

Install dependencies

$ poetry install

Create .env file from .env.example then edit it to add your app credentials and test parameters

$ cp .env.example .env

Run tests

$ poetry run pytest

License

MIT

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

mpesa_connect-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

mpesa_connect-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file mpesa_connect-0.1.0.tar.gz.

File metadata

  • Download URL: mpesa_connect-0.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-58-generic

File hashes

Hashes for mpesa_connect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fbda1ba986b60162e39803cb07915e8d2f3ec45d94ebf76cbc40788409baff35
MD5 74e013fca7dbc221d9811c16ca37db0b
BLAKE2b-256 9cc18ae2b88c890d7226ab32b8010d9703c0f954d9697f8317911837a1cc25f2

See more details on using hashes here.

Provenance

File details

Details for the file mpesa_connect-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mpesa_connect-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-58-generic

File hashes

Hashes for mpesa_connect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e07178e5ba4bd3f2ee13e32a22c149e94733024dbdef86cc6bbf6a9ae8748fe
MD5 8680ed344560d225b3bac69362c5a7cb
BLAKE2b-256 0adcef58cf85ef8ce9b8f918cb381a37f1ea4fdd70bf7f9fef0f38251b7ff685

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