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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: MpesaRest-0.0.9.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

Hashes for MpesaRest-0.0.9.tar.gz
Algorithm Hash digest
SHA256 3032c83eea824a217f85c6d949c4aa87f0b4a203fea2e712ce91cdce94b8c015
MD5 2cbe53e7b6110fe7719f7ecada90d347
BLAKE2b-256 90dcedbb58617035849b308531f054d087b350ad2d90323c6ca908c1278c1d18

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: MpesaRest-0.0.9-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

Hashes for MpesaRest-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0a2ad88ce6c089b4ab618d46ad10d5ad3f8d0972046fd6fce5f6c1390c4e6f28
MD5 039a17035455b999511091e13a3a89f8
BLAKE2b-256 feaca91000a02f7f9e87998a14bcce53f5bcad29585d136bc200e9064287e5b0

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