Skip to main content

A Python SDK for integrating with the MTN MoMo API (Payment Service Bank)

Project description

MoMoPSBAPI Python SDK Documentation

Welcome to the MoMoPSBAPI Python SDK! This SDK enables developers to integrate with the MTN MoMo API for managing API users, API keys, OAuth 2.0 tokens, and payment processing in a seamless manner. Below, you'll find a comprehensive guide on installation, usage, and functionality.


Features

  • Create and manage API Users
  • Generate API Keys for secure communication
  • Obtain OAuth 2.0 tokens for authentication
  • Initiate and manage payment requests
  • Validate API responses

Requirements

Sandbox/Testing Users:

  1. MTN MoMo Developer Account: Sign Up Here
  2. Collection and Disbursement subscription keys

Production Users:

  1. Collection and Disbursement subscription keys
  2. API User ID and API Key

Installation

Install the required dependencies:

pip install requests

Quickstart Guide

Initialize the SDK

from momo_psb import MoMoPSBAPI

# Replace with your details
BASE_URL = "https://sandbox.momodeveloper.mtn.com"
SUBSCRIPTION_KEY = "your_subscription_key"

# Initialize the API client
api = MoMoPSBAPI(base_url=BASE_URL, subscription_key=SUBSCRIPTION_KEY)

Key API Operations

1. Create API User

Generate a unique API User.

import uuid

reference_id = str(uuid.uuid4())  # Generate a unique reference ID
callback_host = "https://your-callback-host.com"

response = api.create_api_user(reference_id, callback_host)
print("Status Code:", response.status_code)
print("Response:", response.json())

2. Generate API Key

Create an API Key for the generated API User.

api_user = "your_api_user_id"
response = api.create_api_key(api_user)
print("Status Code:", response.status_code)
print("API Key:", response.json().get("apiKey"))

3. Retrieve API User Details

Fetch details of an API User.

response = api.get_api_user_details(api_user)
print("Status Code:", response.status_code)
print("User Details:", response.json())

4. Obtain OAuth 2.0 Token

Generate an access token for authorization.

api_key = "your_api_key"
response = api.get_oauth_token(api_user, api_key)
print("Status Code:", response.status_code)
token_data = response.json()
access_token = token_data.get("access_token")
print("Access Token:", access_token)

5. Request to Pay

Initiate a payment request from a consumer.

payer_info = {
    "partyIdType": "MSISDN",
    "partyId": "+2348056042384"
}
reference_id = str(uuid.uuid4())

response = api.request_to_pay(
    reference_id=reference_id,
    access_token=access_token,
    amount="100.00",
    currency="EUR",
    external_id=str(uuid.uuid4()),
    payer=payer_info,
    payer_message="Payment for services",
    payee_note="Thank you for your payment"
)

print("Status Code:", response.status_code)
print("Response:", response.json())

Error Handling

Use the validate_response method to handle errors gracefully.

try:
    data = api.validate_response(response)
    print("Success:", data)
except Exception as e:
    print("Error:", e)

Notes

  1. Environment Setup:
    • Default environment is sandbox. Update the base URL for production use.
  2. Status Codes:
    • 200, 201, 202: Successful operations.
    • Other codes indicate errors and should be handled appropriately.
  3. Unique Identifiers:
    • Use uuid.uuid4() to generate reference IDs for requests.

Additional Resources


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

momo_psb-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

momo_psb-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file momo_psb-0.1.0.tar.gz.

File metadata

  • Download URL: momo_psb-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for momo_psb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a74909938c926f7efa105e86411c95472f704fbf973b9336be1324d3d9632b8
MD5 2597413c156ca52601e9f09e6bcfc36b
BLAKE2b-256 041fd31bd3825b59c73a7a63b2fe3905e08e049dd91d193cecc9fa5a93e622aa

See more details on using hashes here.

File details

Details for the file momo_psb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: momo_psb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for momo_psb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 367ad458557387d36888ebfd721b46af9b54bc2010e487f2543b94b8580cc3bb
MD5 aeb0325964e3559ea408b83e2dacaf57
BLAKE2b-256 a3e85a569efa8978d2be98b990df16225dc5e7dfe607efe946ebc58608177298

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page