Skip to main content

Mobile payments integrations made easy.

Project description

Mobile Payments

An easy way to integrate mobile payments into your web project.

Motivation

Recently, VodaCom has released open API portal for M-Pesa. The following are some issues arose:

For Python integration, they have provided the package in zipped format. It is not user friendly and some developers found it challenging in installation.

Also, the sample code provided for Python implementation are long and most comprise repitition and become prone to errors for developers.

Mobile Payments package try to resolve above issues for Python by providing easy and user friendly installation, also the refactored code that is simplified.

Installation

Install the package using pip

pip install mobile-payments

The package comprise both original open API codes and refactored codes.

To use original open API code import open_api module

from mobile_payments.open_api import APIContext, APIMethodType, APIRequest

To use refactored code import MPESA from vodacom module.

from mobile_payments.vodacom import MPESA

Prerequisites

Python 3.6+

Examples

The following examples requires api key. It can be obtained by registering to open api portal and follow the instructions.

It also uses refactored code, if you want to use original open API code it is also possible but follow the examples in documentation.

Customer to Business payment via vodacom m-pesa

# vodacom M-PESA
from mobile_payments.vodacom import MPESA

api_key = '<your-api-key>'
public_key = '<open-api-public-key>'

m_pesa = MPESA(api_key=api_key, public_key=public_key)

# Customer to Business payment
parameters = {
    'input_Amount': 10, # amount to be charged
    'input_Country': 'TZN',
    'input_Currency': 'TZS',
    'input_CustomerMSISDN': '000000000001',
    'input_ServiceProviderCode': '000000',
    'input_ThirdPartyConversationID': 'c9e794e10c63479992a8b08703abeea36',
    'input_TransactionReference': 'T23434ZE3',
    'input_PurchasedItemsDesc': 'Shoes',
}

response = m_pesa.customer2business(parameters)

Sample response

{'body': {'output_ResponseCode': 'INS-0',
'output_ResponseDesc': 'Request processed successfully',
'output_TransactionID': '79eKKNrYVfCj',
'output_ConversationID': 'c9e794e10c63479992a8b08703abeea36', 'output_ThirdPartyConversationID': 'asv02e5958774f7ba228d83d0d689761'}}

Integrating in Django

If doing web project using Django framework

# views.py
from mobile_payments.vodacom import MPESA

def payments(request):
    if request.method == 'POST':
        form = PaymentForm(request.POST)
        if form.is_valid():
            m_pesa = MPESA(api_key, public_key)

            parameters = {
                'input_Amount': 10,
                'input_Country': 'TZN',
                'input_Currency': 'TZS',
                # '000000000001' - phone number for testing,
                'input_CustomerMSISDN': request.POST.get('phone'),
                'input_ServiceProviderCode': '000000',
                'input_ThirdPartyConversationID': get_random_string(18),
                'input_TransactionReference': get_random_string(18),
                'input_PurchasedItemsDesc': 'Shoes',
            }

            results = m_pesa.customer2business(parameters)

            if results.body['output_ResponseCode'] == 'INS-0':
                # successful transaction

            else:
                # unsuccessful transaction

More details on response codes, see api documentation

License

Code released under MIT LICENSE

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

mobile-payments-0.1.2.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

mobile_payments-0.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file mobile-payments-0.1.2.tar.gz.

File metadata

  • Download URL: mobile-payments-0.1.2.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10

File hashes

Hashes for mobile-payments-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d2b6341ad2ca22de11f0ae9ee16d1053757d02b0ae9297f5ac1d2a1909feea71
MD5 0b1e3d89e1c08e95d6af516997157647
BLAKE2b-256 826c2ebb7e792ff25fdbd3f2d34d61b8da8696fbf730aa2563b80e99f62c2263

See more details on using hashes here.

File details

Details for the file mobile_payments-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mobile_payments-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10

File hashes

Hashes for mobile_payments-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a3c3a7dcc45f2ed4cc0522c2a899601361252702b6410b7801a06a05e46920c5
MD5 4f534c7b667f62650bd46f047ada6514
BLAKE2b-256 345e73db1e82499962d63c7a6ee1e8d6fcd66e9d2b9beb80484bd2d1d870ad48

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