Python Package For MTN Zambia Momo API. This package can also be used by MTN momo in other countries.

Overview

MTN MoMo API Lite Python Client

Power your apps with Lite-Python MTN MoMo API

Usage

Installation

Add the latest version of the library to your project:

 $ git clone https://github.com/Mathewsmusukuma/lite-python-mtnmomo-api.git

This library supports Python 2.7+ or Python 3.4+

Sandbox Environment

Creating a sandbox environment API user

Next, we need to get the User ID and User Secret and to do this we shall need to use the Primary Key for the Product to which we are subscribed, as well as specify a host. The library ships with a commandline application that helps to create sandbox credentials. It assumes you have created an account on https://momodeveloper.mtn.com and have your Ocp-Apim-Subscription-Key.

## within the project, on the command line. In this example, our domain is akabbo.ug
$ mtnmomo
$ providerCallBackHost: https://akabbo.ug
$ Ocp-Apim-Subscription-Key: f83xx8d8xx6749f19a26e2265aeadbcdeg

The providerCallBackHost is your callback host and Ocp-Apim-Subscription-Key is your API key for the specific product to which you are subscribed. The API Key is unique to the product and you will need an API Key for each product you use. You should get a response similar to the following:

Here is your User Id and API secret : {'apiKey': 'b0431db58a9b41faa8f5860230xxxxxx', 'UserId': '053c6dea-dd68-xxxx-xxxx-c830dac9f401'}

These are the credentials we shall use for the sandbox environment. In production, these credentials are provided for you on the MTN OVA management dashboard after KYC requirements are met.

Configuration

Before we can fully utilize the library, we need to specify global configurations. The global configuration must contain the following:

  • BASE_URL: An optional base url to the MTN Momo API. By default the staging base url will be used
  • ENVIRONMENT: Optional environment, either "sandbox" or "production". Default is 'sandbox'
  • CALLBACK_HOST: The domain where you webhooks urls are hosted. This is mandatory.

Once you have specified the global variables, you can now provide the product-specific variables. Each MoMo API product requires its own authentication details i.e its own Subscription Key, User ID and User Secret, also sometimes refered to as the API Secret. As such, we have to configure subscription keys for each product you will be using.

The full list of configuration options can be seen in the example below:

config = {
   "ENVIRONMENT": os.environ.get("ENVIRONMENT"), 
   "BASE_URL": os.environ.get("BASE_URL"), 
   "CALLBACK_HOST": os.environ.get("CALLBACK_HOST"), # Mandatory.
   "COLLECTION_PRIMARY_KEY": os.environ.get("COLLECTION_PRIMARY_KEY"), 
   "COLLECTION_USER_ID": os.environ.get("COLLECTION_USER_ID"),
   "COLLECTION_API_SECRET": os.environ.get("COLLECTION_API_SECRET"),
   "REMITTANCE_USER_ID": os.environ.get("REMITTANCE_USER_ID"), 
   "REMITTANCE_API_SECRET": os.environ.get("REMITTANCE_API_SECRET"),
   "REMITTANCE_PRIMARY_KEY": os.envieon.get("REMITTANCE_PRIMARY_KEY"),
   "DISBURSEMENT_USER_ID": os.environ.get("DISBURSEMENT_USER_ID"), 
   "DISBURSEMENT_API_SECRET": os.environ.get("DISBURSEMENTS_API_SECRET"),
   "DISBURSEMENT_PRIMARY_KEY": os.environ.get("DISBURSEMENT_PRIMARY_KEY"), 
}

You will only need to configure the variables for the product(s) you will be using.

Collections

The collections client can be created with the following paramaters. Note that the COLLECTION_USER_ID and COLLECTION_API_SECRET for production are provided on the MTN OVA dashboard;

  • COLLECTION_PRIMARY_KEY: Primary Key for the Collection product on the developer portal.
  • COLLECTION_USER_ID: For sandbox, use the one generated with the mtnmomo command.
  • COLLECTION_API_SECRET: For sandbox, use the one generated with the mtnmomo command.

You can create a collection client with the following:

import os
from mtnmomo.collection import Collection

client = Collection({
        "COLLECTION_USER_ID": os.environ.get("COLLECTION_USER_ID"),
        "COLLECTION_API_SECRET": os.environ.get("COLLECTION_API_SECRET"),
        "COLLECTION_PRIMARY_KEY": os.environ.get("COLLECTION_PRIMARY_KEY"),
    })

Methods

  1. requestToPay: This operation is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction is executed once the payer has authorized the payment. The transaction will be in status PENDING until it is authorized or declined by the payer or it is timed out by the system. Status of the transaction can be validated by using getTransactionStatus.

  2. getTransactionStatus: Retrieve transaction information using the transactionId returned by requestToPay. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error.

  3. getBalance: Get the balance of the account.

  4. isPayerActive: check if an account holder is registered and active in the system.

Sample Code

import os
from mtnmomo.collection import Collection

client = Collection({
    "COLLECTION_USER_ID": os.environ.get("COLLECTION_USER_ID"),
    "COLLECTION_API_SECRET": os.environ.get("COLLECTION_API_SECRET"),
    "COLLECTION_PRIMARY_KEY": os.environ.get("COLLECTION_PRIMARY_KEY"),
})

client.requestToPay(
    mobile="0966456787", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")

Disbursement

The Disbursements client can be created with the following paramaters. Note that the DISBURSEMENT_USER_ID and DISBURSEMENT_API_SECRET for production are provided on the MTN OVA dashboard;

  • DISBURSEMENT_PRIMARY_KEY: Primary Key for the Disbursement product on the developer portal.
  • DISBURSEMENT_USER_ID: For sandbox, use the one generated with the mtnmomo command.
  • DISBURSEMENT_API_SECRET: For sandbox, use the one generated with the mtnmomo command.

You can create a disbursements client with the following

import os
from mtnmomo.disbursement import Disbursement

client = Disbursement({
    "DISBURSEMENT_USER_ID": os.environ.get("DISBURSEMENT_USER_ID"),
    "DISBURSEMENT_API_SECRET": os.environ.get("DISBURSEMENT_API_SECRET"),
    "DISBURSEMENT_PRIMARY_KEY": os.environ.get("DISBURSEMENT_PRIMARY_KEY"),
})

Methods

  1. transfer: Used to transfer an amount from the owner’s account to a payee account. Status of the transaction can be validated by using the getTransactionStatus method.

  2. getTransactionStatus: Retrieve transaction information using the transactionId returned by transfer. You can invoke it at intervals until the transaction fails or succeeds.

  3. getBalance: Get your account balance.

  4. isPayerActive: This method is used to check if an account holder is registered and active in the system.

Sample Code

import os
from mtnmomo.disbursement import Disbursement

client = Disbursement({
    "DISBURSEMENT_USER_ID": os.environ.get("DISBURSEMENT_USER_ID"),
    "DISBURSEMENT_API_SECRET": os.environ.get("DISBURSEMENT_API_SECRET"),
    "DISBURSEMENT_PRIMARY_KEY": os.environ.get("DISBURSEMENT_PRIMARY_KEY"),
})

client.transfer(amount="600", mobile="0966456787", external_id="123456789", payee_note="dd",      payer_message="dd", currency="EUR")

Credit

This repo was forked Sparkplug and modified to work well for MTN Zambia and MTN momo in other countries.

Thank you.

Owner
Mathews Musukuma
Python | JavaScript Developer
Mathews Musukuma
Discord bot for user notes.

Noter A discord bot for handling notes for users. Want to keep track of things about your discord users? Then this bot is for you! Links DB Browser fo

Ori 2 Jun 05, 2022
Create Multiple CF entry for multiple websites

AWS-CloudFront Problem: Deploy multiple CloudFront for account with multiple domains. Functionality: Running this script in loop and deploy CloudFront

Giten Mitra 5 Nov 18, 2022
A way to export your saved reddit posts to a Notion table.

reddit-saved-to-notion A way to export your saved reddit posts and comments to a Notion table.Uses notion-sdk-py and praw for interacting with Notion

19 Sep 12, 2022
Demo of using Telegram to send alert message

MIAI_Telegram Demo of using Telegram to send alert message Video link: https://youtu.be/oZ9CsIrlMgg #MìAI Fanpage: http://facebook.com/miaiblog Group

4 Jun 20, 2021
historical code from reddit.com

This repository is archived. This repository is archived and will not receive any updates or accept issues or pull requests. To report bugs in reddit.

The Reddit Archives 16.3k Dec 31, 2022
A python bot that scrapes free udemy coupons and sends them to Telegram.

About: A python telegram bot that scrapes information about fresh free Udemy coupons content from couponscorpion.com and sends it to teleram channel h

Irina Gayday 1 Dec 19, 2021
An Open Source ALL-In-One Telegram RoBot, that can do lot of things.

An Open Source ALL-In-One Telegram RoBot, that can do lot of things.

JOBIN 0 Dec 01, 2021
How to make a QR Code of your own in python

QR CODE Bilgilendirme! " pip install qrcode pillow " kurmalısınız.

Aktali 1 Dec 24, 2021
TFT Bot that automatically surrenders and allows finishing TFT Passes easily.

Image Based TFT Bot TFT Bot that automatically surrenders and allows finishing TFT Passes easily. Please read full file! You can check new releases he

1 Feb 06, 2022
A GitHub Follower Bot that is a WIP.

GitHub Follower Bot (WIP) Work In Progress This bot is a WIP. There are still many features I plan to add and code I need to improve (I'm still fairly

Christian Deacon 71 Dec 29, 2022
IMDb + Auto + Unlimited Filter BoT

Telegram Movie Bot Features Auto Filter Manuel Filter IMDB Admin Commands Broadcast Index IMDB search Inline Search Random pics ids and User info Stat

Jos Projects 82 Dec 27, 2022
Notion API Database Python Implementation

Python Notion Database Notion API Database Python Implementation created only by database from the official Notion API. Installing / Getting started p

minwook 78 Dec 19, 2022
SSH-Restricted deploys an SSH compliance rule (AWS Config) with auto-remediation via AWS Lambda if SSH access is public.

SSH-Restricted SSH-Restricted deploys an SSH compliance rule with auto-remediation via AWS Lambda if SSH access is public. SSH-Auto-Restricted checks

Adrian Hornsby 30 Nov 08, 2022
Python wrapper for the GitLab API

Python GitLab python-gitlab is a Python package providing access to the GitLab server API. It supports the v4 API of GitLab, and provides a CLI tool (

1.9k Dec 31, 2022
Free TradingView webhook alert for basic plan users.

TradingView-Free-Webhook-Alerts Project start on 01-02-2022 Providing the free webhook service to the basic plan users in TradingView. Portal ↠ Instal

Freeman 31 Dec 25, 2022
Asyncio SDK for Azure Cosmos DB

Asyncio SDK for Azure Cosmos DB. This library is intended to be a very thin asyncio wrapper around the Azure Comsos DB Rest API. It is not intended to have feature parity with the Microsoft Azure SDK

Grant McDonald 4 Dec 04, 2021
This is Pdisk Upload Bot made using Python with Pyrogram Framework. Its capable of uploading direct download link with thumbnail or without thumbnail & with Title Support.

Pdisk-Upload-Bot Introduction This Is PDisk Upload Bot Used To Upload Direct Link To Pdisk With Thumb Support Deploy Heroku Deploy Local Deploy pip in

HEIMAN PICTURES 32 Oct 21, 2022
tgEasy's Official Assistant Bot and Example Bot

tgEasy Assistant The assistant bot that helps people with tgEasy directly on Telegram. This repository contains the source code of @tgEasyRobot and th

Divide Projects™ 4 Dec 26, 2022
A Flask & Twilio Secret Santa app.

🎄 ✨ Secret Santa Twilio ✨ 📱 A contactless Secret Santa game built with Python, Flask and Twilio! Prerequisites 📝 A Twilio account. Sign up here ngr

Sangeeta Jadoonanan 5 Dec 23, 2021
A Telegram bot to extracting text from images. All languages supported.

OCR Bot A Telegram bot to extracting text from images. All languages supported. Deploy to Heroku Local Deploying Clone the repo git clone https://gith

6 Oct 21, 2022