📜  lipa na mpesa daraja python (1)

📅  最后修改于: 2023-12-03 14:44:00.664000             🧑  作者: Mango

Lipa na M-Pesa Daraja Python

Lipa na M-Pesa Daraja Python is a simple Python Package for integrating Safaricom's Lipa Na M-Pesa Online Payment API (Daraja API) with Python Applications.

Features
  • Generate M-Pesa Access Tokens
  • Perform Lipa Na M-Pesa Online Payment Transactions
  • Query Lipa Na M-Pesa Payment Status
  • Integrated with Requests Library
Installation

You can install Lipa na Mpesa Daraja Python using pip:

pip install lipanampesadarajapython
Getting Started
Create an Account

To use the Lipa na M-Pesa Daraja API, you need to create an account on Safaricom's developer portal.

Generate Access Token

Before you can initiate any requests to the API, you need to generate an access token. An access token is a security feature used to authenticate requests to the API.

Example:

from lipanampesadaraja import LipaNaMpesaDaraja

lnm = LipaNaMpesaDaraja(
  app_key='your_app_key',
  app_secret='your_app_secret',
  environment='sandbox', # change to 'production' for live transactions
)

access_token = lnm.generate_access_token()
Perform a Payment

To initiate a Lipa Na M-Pesa payment, you will need to provide the following details:

  • Business shortcode
  • Pass Key
  • Timestamp generated from the current UTC date time in the format YYYMMDDHHMMSS
  • Transaction Type
  • Amount
  • Phone number
  • Party B
  • Call Back URL
  • Description

Example:

from lipanampesadaraja import LipaNaMpesaDaraja

lnm = LipaNaMpesaDaraja(
  app_key='your_app_key',
  app_secret='your_app_secret',
  environment='sandbox', # change to 'production' for live transactions
)

access_token = lnm.generate_access_token()

response = lnm.lipa_na_mpesa_online(
    access_token=access_token,
    business_shortcode='your_business_shortcode',
    pass_key='your_pass_key',
    amount='10',
    phone_number='254720000000',
    callback_url='https://yourapp.com/callback',
    description='Test Transaction'
)

print(response)
Query Payment Status

You can also check the status of a previously initiated Lipa Na Mpesa payment transaction.

Example:

from lipanampesadaraja import LipaNaMpesaDaraja

lnm = LipaNaMpesaDaraja(
  app_key='your_app_key',
  app_secret='your_app_secret',
  environment='sandbox', # change to 'production' for live transactions
)

access_token = lnm.generate_access_token()

response = lnm.query_lipa_na_mpesa_payment_status(
    access_token=access_token,
    business_shortcode='your_business_shortcode',
    checkout_request_id='your_checkout_request_id'
)

print(response)
Conclusion

Lipa na M-Pesa Daraja Python is a simple yet powerful package for integrating Safaricom's Lipa na M-Pesa Online Payment API with Python Applications. With its features such as Generating Access Tokens, Performing Payments and Querying Payment Status, it is indeed a valuable tool for developers.