A reusable Django app for integrating the PayHero payment gateway.
Project description
Django PayHero Integration
A reusable Django app for integrating the PayHero payment gateway. This package provides a simple interface to initiate M-Pesa STK Push payments and handles the callback from PayHero to update transaction statuses.
Features
- Simple service class for initiating payments.
- Django model for tracking transaction status.
- Callback view to handle payment notifications from PayHero.
- Easy configuration through Django settings.
Installation & Setup
Requirements
- Python 3.8+
- Django 3.2+
- requests library
Steps
-
Install the requests library:
pip install django-payhero
-
Add
payhero_djangoto yourINSTALLED_APPSin your project'ssettings.py:# settings.py INSTALLED_APPS = [ # ... other apps 'payhero_django', ]
-
Add PayHero configuration to your
settings.py:# settings.py PAYHERO_SETTINGS = { "API_KEY": "YOUR_PAYHERO_API_KEY", # Replace with your actual API Key "CHANNEL_ID": "YOUR_PAYHERO_CHANNEL_ID", # Replace with your Channel ID "CALLBACK_URL_NAME": "payhero_callback", # The name of the callback URL }
Security Note: Always use environment variables or Django's secrets management to store your API_KEY. Do not hardcode it in your settings file in production.
-
Include the callback URL in your project's main
urls.py:# your_project/urls.py from django.urls import path, include urlpatterns = [ # ... your other urls path('payments/', include('payhero_django.urls')), ]
This will expose the callback endpoint at
/payments/payhero/callback/. -
Run migrations to create the necessary database tables for tracking transactions:
python manage.py makemigrations payhero_django python manage.py migrate
Usage Example
Here is how you can initiate a payment from one of your own app's views.
# in your_app/views.py
from django.shortcuts import render
from django.http import JsonResponse
from payhero_django.api import PayHeroClient
from payhero_django.exceptions import PayHeroAPIError
import uuid
def initiate_payment_view(request):
if request.method == 'POST':
amount = request.POST.get('amount')
phone_number = request.POST.get('phone_number') # e.g., 2547xxxxxxxx
if not amount or not phone_number:
return JsonResponse({'error': 'Amount and phone number are required.'}, status=400)
# 1. Initialize the client
payhero_client = PayHeroClient()
# 2. Generate a unique reference for the transaction
external_reference = str(uuid.uuid4())
try:
# 3. Call the API
api_response = payhero_client.initiate_stk_push(
amount=int(amount),
phone_number=phone_number,
external_reference=external_reference,
customer_name="John Doe" # Optional
)
# 4. The transaction is now pending. The status will be updated
# by the callback. You can redirect the user or show a message.
return JsonResponse({
'message': 'STK Push initiated. Please check your phone.',
'data': api_response
})
except PayHeroAPIError as e:
# Handle potential API errors
return JsonResponse({'error': str(e)}, status=500)
return render(request, 'your_payment_template.html')
How the Callback Works
-
When a payment status changes (e.g., from pending to success or failed), PayHero will send a POST request to the callback URL you configured (
/payments/payhero/callback/). -
The view provided in this app (
payhero_django/views.py) will securely receive this request. -
It finds the transaction in your database using the reference provided in the callback data.
-
It updates the status of the
PayHeroTransactionmodel to reflect the payment outcome (e.g., SUCCESS, FAILED). -
You can then query the
PayHeroTransactionmodel in your own app to check the status of an order and fulfill it if the payment was successful.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_payhero-0.1.1.tar.gz.
File metadata
- Download URL: django_payhero-0.1.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a6d14adde9974445c234d2573cb0a8a86841a36088dd3446b906752f699bf34
|
|
| MD5 |
8351a7dcb930d1bb2f45b54e14f19eab
|
|
| BLAKE2b-256 |
47d4aa880996d5d9abc0d20a4d7feae18482d3e1d0b58eaf2de99f6ec6d5cd98
|
File details
Details for the file django_payhero-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_payhero-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a9253ea8221f611587571a160c6e1962bc46007f9a1de69525504564cc9b146
|
|
| MD5 |
17aa44e988bd1cf80d16c669443f2995
|
|
| BLAKE2b-256 |
12f058910892d33dd787f55a3da515f16ce9efe49526ea387534cc98a457f8d2
|