mpesa-connect


Namempesa-connect JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/enwawerueli/mpesa-connect
SummaryA wrapper library for the Daraja Mpesa API
upload_time2023-07-15 06:08:21
maintainer
docs_urlNone
authorEmz D
requires_python>=3.8,<4.0
licenseMIT
keywords daraja mpesa payment
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MPESA CONNECT

A wrapper library for the Daraja Mpesa API

[![Language](https://img.shields.io/badge/language-python-green.svg)](https://python.org)

## 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](https://developer.safaricom.co.ke/Documentation)* 

Create an app instance. 

```python
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.

```python
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**
```python
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=...,
    # access_token=access_token
)
```

**Query**
```python
result = stk.query(
    business_short_code=...,
    checkout_request_id=...,
    password=...,
)
```
You can use the `generate_password` helper to create a password

```python
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**
```python
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**
```python
result = c2b.simulate(
    short_code=...,
    command_id=TransactionType.CUSTOMER_PAY_BILL_ONLINE,
    amount=...,
    msisdn=...,
    bill_ref_number=...,
)
```

### Business To Customer (B2C) API

```python
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

```python
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

```python
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`](https://requests.readthedocs.io/en/latest/api/#requests.Response) object, plus various properties corresponding to the json body of the response

## Running Tests

Install dependencies

    $ poetry install

Create `.env` file from [.env.example](https://github.com/enwawerueli/mpesa-connect/blob/main/.env.example) then edit it to add your app credentials and test parameters

    $ cp .env.example .env

 Run tests

    $ poetry run pytest

## License

[MIT](https://github.com/enwawerueli/mpesa-connect/blob/main/LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/enwawerueli/mpesa-connect",
    "name": "mpesa-connect",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "daraja,mpesa,payment",
    "author": "Emz D",
    "author_email": "seaworndrift@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e0/c6/5f295200a5f68b941a388d063cecdddb6253bad22ca36fbee22bb3d429f2/mpesa_connect-0.1.4.tar.gz",
    "platform": null,
    "description": "# MPESA CONNECT\n\nA wrapper library for the Daraja Mpesa API\n\n[![Language](https://img.shields.io/badge/language-python-green.svg)](https://python.org)\n\n## Features\n\n- Authorization\n- Mpesa Express\n  - STK Push\n  - Query\n- Customer To Business (C2B)\n    - Register URL\n    - Simulate\n- Business To Customer (B2C)\n- Account Balance\n- Transaction Status\n\n## Installation\n\n    $ pip install mpesa-connect\n\n## Usage\n\n*NOTE: Before you start, make sure to go through the official Daraja Mpesa API [documentation](https://developer.safaricom.co.ke/Documentation)* \n\nCreate an app instance. \n\n```python\nfrom mpesa_connect import App\n\n# Sandbox\napp = App.create_sandbox(consumer_key=..., consumer_secret=...)\n\n# Production\napp = App.create_production(consumer_key=..., consumer_secret=...)\n```\n\nGenerate an authorization token.\n\n```python\nfrom mpesa_connect import Authorization\n\nauth = Authorization(app)\nresult = auth.generate_token()\naccess_token = result.access_token\n```\n*You can attach this token to the service instance or include it as an argument to the api methods calls*\n\n### Mpesa Express\n\n**STK Push**\n```python\nfrom mpesa_connect import STKPush\n\nstk = STKPush(app, access_token=access_token)\nresult = stk.process_request(\n    business_short_code=...,\n    phone_number=...,\n    amount=...,\n    call_back_url=...,\n    account_reference=...,\n    transaction_desc=...,\n    password=...,\n    timestamp=...,\n    # access_token=access_token\n)\n```\n\n**Query**\n```python\nresult = stk.query(\n    business_short_code=...,\n    checkout_request_id=...,\n    password=...,\n)\n```\nYou can use the `generate_password` helper to create a password\n\n```python\nfrom mpesa_connect.utils import generate_password\n\npassword = generate_password(\n    business_short_code=....,\n    pass_key=...,\n    timestamp=...,\n)\n```\nAlternatively, you can include the `pass_key` argument in place of `password` to auto generate the password\n\n### Customer To Business (C2B) API\n\n**Register URL**\n```python\nfrom mpesa_connect import C2B\nfrom mpesa_connect.enums import ResponseType, TransactionType\n\nc2b = C2B(app, access_token=access_token)\nresult = c2b.register_url(\n    short_code=...,\n    validation_url=...,\n    confirmation_url=...,\n    response_type=ResponseType.COMPLETED,\n)\n```\n\n**Simulate**\n```python\nresult = c2b.simulate(\n    short_code=...,\n    command_id=TransactionType.CUSTOMER_PAY_BILL_ONLINE,\n    amount=...,\n    msisdn=...,\n    bill_ref_number=...,\n)\n```\n\n### Business To Customer (B2C) API\n\n```python\nfrom mpesa_connect import B2C\nfrom mpesa_connect.enums import TransactionType\n\nb2c = B2C(app, access_token=access_token)\nresult = b2c.payment_request(\n    initiator_name=...,\n    security_credential=...,\n    amount=...,\n    command_id=TransactionType.BUSINESS_PAYMENT,\n    party_a=...,\n    party_b=...,\n    queue_time_out_url=...,\n    result_url=...,\n    remarks=...,\n    occassion=...,\n)\n```\n\n### Account Balance API\n\n```python\nfrom mpesa_connect import AccountBalance\nfrom mpesa_connect.enums import TransactionType, IdentifierType\n\nab = AccountBalance(app, access_token=access_token)\nresult = ab.query(\n    initiator=...,\n    security_credential=...,\n    command_id=TransactionType.ACCOUNT_BALANCE,\n    identifier_type=IdentifierType.ORGANIZATION_SHORT_CODE,\n    party_a=...,\n    queue_time_out_url=...,\n    result_url=...,\n    remarks=...,\n)\n```\n\n### Transaction Status API\n\n```python\nfrom mpesa_connect import TransactionStatus\nfrom mpesa_connect.enums import TransactionType, IdentifierType\n\nts = TransactionStatus(app, access_token=access_token)\nresult = ts.query(\n    initiator=...,\n    security_credential=...,\n    transaction_id=...,\n    command_id=TransactionType.TRANSACTION_STATUS_QUERY,\n    identifier_type=IdentifierType.ORGANIZATION_SHORT_CODE,\n    party_a=...,\n    queue_time_out_url=...,\n    result_url=...,\n    remarks=...,\n    occassion=...,\n)\n```\n\nAll API methods return a result object with a `response` property which is a [`requests.Response`](https://requests.readthedocs.io/en/latest/api/#requests.Response) object, plus various properties corresponding to the json body of the response\n\n## Running Tests\n\nInstall dependencies\n\n    $ poetry install\n\nCreate `.env` file from [.env.example](https://github.com/enwawerueli/mpesa-connect/blob/main/.env.example) then edit it to add your app credentials and test parameters\n\n    $ cp .env.example .env\n\n Run tests\n\n    $ poetry run pytest\n\n## License\n\n[MIT](https://github.com/enwawerueli/mpesa-connect/blob/main/LICENSE)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A wrapper library for the Daraja Mpesa API",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/enwawerueli/mpesa-connect",
        "Repository": "https://github.com/enwawerueli/mpesa-connect"
    },
    "split_keywords": [
        "daraja",
        "mpesa",
        "payment"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4baf248102b967c19e7bfa9d02de7bd8bb68d2f792bcfa5bd2d7be2a018fba76",
                "md5": "7d7094e92029fa33b1c3e1bedcd4fdab",
                "sha256": "ab4942004a0cfa7eca9dd8e79b771cfd1d2b55a8a7cc902afe1e715dde6d5553"
            },
            "downloads": -1,
            "filename": "mpesa_connect-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7d7094e92029fa33b1c3e1bedcd4fdab",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 12044,
            "upload_time": "2023-07-15T06:08:19",
            "upload_time_iso_8601": "2023-07-15T06:08:19.710072Z",
            "url": "https://files.pythonhosted.org/packages/4b/af/248102b967c19e7bfa9d02de7bd8bb68d2f792bcfa5bd2d7be2a018fba76/mpesa_connect-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0c65f295200a5f68b941a388d063cecdddb6253bad22ca36fbee22bb3d429f2",
                "md5": "3e52cb94ca315348ad5e97c6f774de2b",
                "sha256": "6108e280d1cb3de17c7c7dd953191b5c16cbcfa2ae3b424f4a227f9f61f195fc"
            },
            "downloads": -1,
            "filename": "mpesa_connect-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3e52cb94ca315348ad5e97c6f774de2b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 10089,
            "upload_time": "2023-07-15T06:08:21",
            "upload_time_iso_8601": "2023-07-15T06:08:21.655587Z",
            "url": "https://files.pythonhosted.org/packages/e0/c6/5f295200a5f68b941a388d063cecdddb6253bad22ca36fbee22bb3d429f2/mpesa_connect-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-15 06:08:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "enwawerueli",
    "github_project": "mpesa-connect",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mpesa-connect"
}
        
Elapsed time: 0.09315s