📅  最后修改于: 2023-12-03 14:44:00.664000             🧑  作者: Mango
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.
You can install Lipa na Mpesa Daraja Python using pip:
pip install lipanampesadarajapython
To use the Lipa na M-Pesa Daraja API, you need to create an account on Safaricom's developer portal.
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()
To initiate a Lipa Na M-Pesa payment, you will need to provide the following details:
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)
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)
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.