Skip to main content

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


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.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

MpesaRest-0.0.6-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file MpesaRest-0.0.6.tar.gz.

File metadata

  • Download URL: MpesaRest-0.0.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for MpesaRest-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7e268f8f867bdcf0334887d5d0b28ea005f5720674bf0123b4c55632104d33be
MD5 a3d162066776d84e9a73440e4587ce2d
BLAKE2b-256 11473912441d7f754dffb31927c1c8f066dfe984e3dfdf44bcf000e69fb9a019

See more details on using hashes here.

Provenance

File details

Details for the file MpesaRest-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: MpesaRest-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for MpesaRest-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 971c1f46c47ae68e89601d1fa57b04fb34f67870fae09ca96f7fc7eb8b9aabcf
MD5 642e372998ed908fd6eeefea54ca3ba5
BLAKE2b-256 d91f1f20b51f45a8707934046a2f5552e146674b07edd4167a40ef04eb34bd1b

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