Git Product home page Git Product logo

tralahm / pytekmomoapi Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 76 KB

An Opinionated Python SDK for the for the MTN Mobile Money API. The goal of this SDK library is to enable python developers implement MTN Mobile Money APIs in a flexible, yet consistent manner.

Home Page: https://pytekmomoapi.readthedocs.io/

License: GNU General Public License v3.0

Python 100.00%
momoapi mtnmobilemoney tralahtek tralahm finance mobilemoney api-client sdk-python

pytekmomoapi's Introduction

Build Status Build status Documentation Status License: GPLv3 Organization Views PRs Welcome GitHub pull-requests Language

pytekmomoapi.

An Opinionated Python SDK for the for the MTN Mobile Money API. The goal of this SDK library is to enable python developers implement MTN Mobile Money APIs in a flexible, yet consistent manner.

The Open API is a JSON REST API that is used by Partner systems to access services in the Wallet platform. The Open API exposes services that are used by e.g. online merchants for managing payments and other financial services.

TralahTek TralahM TralahM

Documentation

More Documentation can be found on readthedocs below:

Documentation

How to Install

# In terminal do:
$ pip install pytekmomoapi

Building from Source for Developers

$ git clone https://github.com/TralahM/pytekmomoapi.git
$ cd pytekmomoapi
$ python setup.py install

Quickstart

Installation

pip install pytekmomoapi

Sandbox User Provisioning

We have provided a convenient function, tekmomoapi.get_user_id_and_api_key , to help you quickly provision and get the required credentials i.e the user_id and apiKey on the sandbox environment.

The Library also provided a utility function to get a random uuid.uuid4 string, this is quite useful because the remote Momo Developer API heavily uses uuid4s for identification and referencing, i.e during user provisioning, financial transactions, party identification and referencing. The function tekmomoapi.get_random_uuid_str provides that convenience.

This reduces the amount of boilerplate code that would be otherwise required.

Example

import tekmomoapi
creds=dict(
   collection_subscription_key="3265aef9928c259a31b44faf812dc2da",
   remittance_subscription_key="928c259a31b44faf812dc2da3265aef9",
   disbursement_subscription_key="31b44faf812dc2da928c259a3265aef9",
)
cuser = tekmomoapi.get_user_id_and_api_key(
    creds.get("collection_subscription_key"))

duser = tekmomoapi.get_user_id_and_api_key(
    creds.get("disbursement_subscription_key"))

user = tekmomoapi.get_user_id_and_api_key(
   creds.get("remittance_subscription_key"))
    user = tekmomoapi.get_user_id_and_api_key(creds.get("remittance_subscription_key"))
    print(user)
    {'user_id': 'fcbf0091-2dbe-4f37-9131-46a2a0dcda9e', 'apiKey': 'cb196acfa75b4bf1858b5dccdbb605a6'}

Authentication and Authorization

OAuth Token is generated from the merchants' API Key and Secret. The API Key and API Secret can be obtained through the provisioning API in Sandbox.

We have built-in the authentication and authorization processes so you dont have to worry about base64 encodings, whether to user Bearer or Basic Authentication, enabling you to focus on more important details i.e your business logic implementation and other more fun stuff.

You can obtain the authentication header string anytime if you wish to do so by using the top level tekmomoapi.scaffold.BaseAPI{.pycode .python} and its subclassess tekmomoapi.scaffold.BaseAPI.basic_auth_str or the tekmomoapi.scaffold.BaseAPI.bearer_auth_str property methods.

Example

RemAPI = tekmomoapi.RemittanceAPI(
    creds.get("remittance_subscription_key"),
    base_url=tekmomoapi.SANDBOX_BASE_URL,
    **user,
)
DisAPI = tekmomoapi.DisbursementAPI(
    creds.get("disbursement_subscription_key"),
    base_url=tekmomoapi.SANDBOX_BASE_URL,
    **duser,
)
ColAPI = tekmomoapi.CollectionAPI(
    creds.get("collection_subscription_key"),
    base_url=tekmomoapi.SANDBOX_BASE_URL,
    **cuser,
)
print("Remittance Basic Auth str: ",RemAPI.basic_auth_str)
print("Collection Bearer Auth str: ",ColAPI.bearer_auth_str)
print("Disbursement Basic Auth str: ",DisAPI.basic_auth_str)
print("Disbursement Bearer Auth str: ",DisAPI.bearer_auth_str)

Account Balance and Account Holder Validation

All Subclasses of tekmomoapi.scaffold.BaseAPI provide a method for checking the account balance of the respective concrete API as well a method for checking whether an account id or PartyId is active and valid.

Example

print(
    f"ColAPI.get_account_balance: {ColAPI.get_account_balance('sandbox')}")
rembal=RemAPI.get_account_balance()
disbal=DisAPI.get_account_balance(x_target_environment="sandbox")
# print(rembal)
# print(disbal)
# Check if Account is Active
tmsisdn = "22997108557"
ColAPI.is_account_active(tmsisdn, 'msisdn', 'sandbox')
True
RemAPI.is_account_active(tmsisdn, 'msisdn',
'sandbox')
True
DisAPI.is_account_active(tmsisdn, 'msisdn', 'sandbox')
True
ColAPI.get_account_balance: {'availableBalance': '1000', 'currency': 'EUR'}

Collections API

The CollectionAPI provides a method for requesting payments from customers as well as a method for checking the status of a payment request in addition to the aforementioned account balance and validation methods defined in the BaseAPI base class.

The CollectionAPI uses the CParty and PaymentRequest types for its PaymentRequest and Party object arguments.

The tekmomoapi.get_collection_party_obj and tekmomoapi.get_payment_request_obj provide convenience function to create the required Party and Transfer objects.

Example

Create a Collection PaymentRequest Object using the convenience functions.

payment_request_obj = tekmomoapi.get_payment_request_obj(
    amount="100",
    currency="EUR",
    externalId=get_random_uuid_str(),
    payer=tekmomoapi.get_collection_party_obj(
        "msisdn", "22997108557").to_jsonable(),
    payerMessage="Payment Request Message Here",
    payeeNote="Customer Note Here",
)

Request Payment

Using the payment_request_obj in the example above, we can request payment from a customer using the tekmomoapi.CollectionAPI.request_payment function.

ColAPI.request_payment(
    payment_request_obj.externalId,
    payment_request_obj,
    x_callback_url=None,
    x_target_environment="sandbox",
)
# Check Payment Request Status
print(ColAPI.get_payment_request_status(payment_request_obj.externalId))
{"amount": "100", "currency": "EUR", "financialTransactionId": "454621636",
"externalId": "8b2e92fd-a9a6-48e1-bae5-099d5d3ad4c8", "payer": {"partyIdType":
"MSISDN", "partyId": "22997108557"}, "payerMessage": "Payer Message Here",
"payeeNote": "Payee Note Here", "status": "SUCCESSFUL"}

Disbursements API

The DisbursementAPI provides a method for initiating transfers from the business to other parties as well as a method for checking the status of a transfer request in addition to the account balance and validation methods defined in the base class BaseAPI.The DisbursementAPI uses the DTransfer and DParty classes to specify its Party and Transfer object arguments.

The tekmomoapi.get_disbursement_obj and tekmomoapi.get_disbursement_party_obj provide convenience functions to create the correct and desired Transfer and Party objects.

Example

Create a Disbursement Transfer Object using the convenience functions.

dtransfer_obj = tekmomoapi.get_disbursement_transfer_obj(
    amount="100",
    currency="EUR",
    externalId=get_random_uuid_str(),
    payee=tekmomoapi.get_disbursement_party_obj(
        "msisdn", "22997108557"
    ),  # .to_jsonable(),
    payerMessage="Disbursement Transfer Message Here",
    payeeNote="Payee Business Note Here",
)

Transfer Funds

Using the dtransfer_obj in the example above, we can transfer funds using the tekmomoapi.DisbursementAPI.transfer function.

dtres = DisAPI.transfer(
    dtransfer_obj.externalId,
    dtransfer_obj,
    x_callback_url=None,
    x_target_environment="sandbox",
)
# Check Transaction Status
print(DisAPI.get_transfer_status(dtransfer_obj.externalId))
{"amount": "100", "currency": "EUR", "financialTransactionId": "1501780124",
"externalId": "f832c026-fd6a-4414-9660-df3080471bdf",
"payee": {"partyIdType": "MSISDN", "partyId": "22997108557"},
"payerMessage": "Disbursement Transfer Message Here",
"payeeNote": "Payee Business Note Here", "status": "SUCCESSFUL"}

Remittance API

The RemittanceAPI is almost identical to the DisbursementAPI, the methods provided by each are the same and are used in a similar fashion. The difference is the type of Transfer and Party objects passed to their parameters. To Distinguish between the two the RemittanceAPI uses RTransfer and RParty classes.

The tekmomoapi.get_remittance_obj and tekmomoapi.get_remittance_party_obj provide convenience functions to create the correct and desired Transfer and Party objects.

Example

Create a Remittance Transfer Object using the convenience functions.

transfer_obj = tekmomoapi.get_remittance_transfer_obj(
    amount="100",
    currency="EUR",
    externalId=get_random_uuid_str(),
    payee=tekmomoapi.get_remittance_party_obj(
        "msisdn", "22997108557"
    ),  # .to_jsonable(),
    payerMessage="Remittance Transfer Message Here",
    payeeNote="Payee Business Note Here",
)

Transfer Funds

Using the transfer_obj in the example above, we can transfer funds using the tekmomoapi.RemittanceAPI.transfer function.

tres = RemAPI.transfer(
    transfer_obj.externalId,
    transfer_obj,
    x_callback_url=None,
    x_target_environment="sandbox",
)
# Check Transaction Status
print(RemAPI.get_transfer_status(transfer_obj.externalId))
{"amount": "100", "currency": "EUR", "financialTransactionId": "2105998992",
"externalId": "566ee525-c60a-4aac-9fb2-0c9dc24dd290", "payee": {"partyIdType":
"MSISDN", "partyId": "22997108557"}, "payerMessage": "Remittance Transfer Message Here",
"payeeNote": "Payee Business Note Here", "status": "SUCCESSFUL"}

Contributing

See the Contributing File

See the Pull Request File

Support

LICENCE

Read the license here

Self-Promotion

TralahM TralahM

Blog

TralahTek

pytekmomoapi's People

Contributors

semgrep-bot avatar tralahm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

blackadams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.