Ecraspay Django Package
A Django package for integrating with the Ecraspay API. This package provides functionalities for initiating and verifying payments, managing transactions, and simplifying payment workflows within Django applications.
Features
- Initiate payments using the Ecraspay API.
- Verify payment status via polling.
- Support for multiple payment methods (Card, Bank Transfer, USSD).
- Predefined Django models for storing payment data.
- Easy integration with existing apps and workflows.
Installation
-
Install the package using
pip:pip install ecraspay-django
-
Add the package to your Django project:
In your
settings.py, add the package toINSTALLED_APPS:INSTALLED_APPS = [ ..., "ecraspay_django", ]
Configuration
Add the following configuration to your settings.py:
ECRASPAY_API_KEY = "your-ecraspay-api-key" # Replace with your API key
ECRASPAY_ENVIRONMENT = "sandbox" # "sandbox" or "production"
ECRAS_REDIRECT_URL = "https://yourdomain.com/payment/success"
PAYMENT_PROCESSING_MODE = "checkout" # Default: "checkout"
If you're using environment variables, you can configure them as follows:
export ECRASPAY_API_KEY=your-api-key
export ECRASPAY_ENVIRONMENT=sandbox
export ECRAS_REDIRECT_URL=https://yourdomain.com/payment/success
Models
This package includes the following models:
-
Payment
Represents a payment initiated using the Ecraspay API.- Fields:
payment_reference,transaction_reference,amount,status,currency, etc.
- Fields:
-
AbstractPayment
Provides a base model for storing payments that can be extended by other apps.
Example Usage:
from ecraspay_django.models import Payment
# Example: Fetch a payment
payment = Payment.objects.get(payment_reference="PAY-20240405-ABC123")
print(payment.status, payment.amount)
Payment Workflow
1. Initiating a Payment
To initiate a payment, use the EcraspayService class:
from ecraspay_django.services import EcraspayService
# Initialize the service
service = EcraspayService()
# Initiate checkout
response = service.initiate_checkout(
amount=1000.00,
reference="PAY-20240405-ABC123",
customer_name="John Doe",
customer_email="johndoe@example.com",
description="School Fee Payment",
currency="NGN",
metadata={"school": "ABC School"}
)
print("Payment Link:", response["payment_link"])
2. Verifying a Payment
To verify a payment, use the verify_checkout method:
response = service.verify_checkout(reference="PAY-20240405-ABC123")
print("Payment Status:", response["responseBody"]["status"])
Views and Endpoints
This package includes the following API endpoints:
| Method | Endpoint | Description |
|---|---|---|
POST |
/payment/ |
Initiate a payment |
GET |
/payment/verify/<id>/ |
Verify a payment status |
Signals
The package provides a webhook_received signal to handle webhook events (optional in future integration):
from django.dispatch import receiver
from ecraspay_django.views import webhook_received
@receiver(webhook_received)
def handle_webhook(sender, request, event, **kwargs):
print("Webhook event received:", event)
Polling for Payment Verification
If you are not using webhooks, you can verify payments via polling. Use a periodic task scheduler like Celery or cron to check payment statuses.
Example Polling Function
from ecraspay_django.models import Payment
from ecraspay_django.services import EcraspayService
def verify_pending_payments():
service = EcraspayService()
pending_payments = Payment.objects.filter(status="PENDING")
for payment in pending_payments:
response = service.verify_checkout(payment.payment_reference)
status = response["responseBody"].get("status")
if status == "SUCCESS":
payment.status = "COMPLETED"
elif status == "FAILED":
payment.status = "FAILED"
payment.save()
print(f"Payment {payment.payment_reference} updated to {payment.status}")
URL Configuration
Add the following URLs to your project's urls.py:
from django.urls import path
from ecraspay_django.views import PaymentView, PaymentVerificationView
urlpatterns = [
path("payment/", PaymentView.as_view(), name="payment"),
path("payment/verify/<str:payment_id>/", PaymentVerificationView.as_view(), name="payment-verification"),
]
Utilities
Generate a Payment Reference:
from ecraspay_django.utils import payment_reference_generator
reference = payment_reference_generator()
print(reference) # e.g., PAY-20240405-ABC123
Contribution Guide
-
Clone the repository:
git clone https://github.com/yourusername/ecraspay-django.git
-
Install dependencies:
pip install -r requirements.txt
-
Run tests:
python manage.py test
-
Submit a pull request for improvements or bug fixes.
License
This package is released under the MIT License. See the LICENSE file for more details.
Contact
For questions or contributions:
- Email:
sammyboy.as@gmail.com - GitHub: Your GitHub Profile
Metadata
Release files for ecraspay-django 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ecraspay_django-0.1.1.tar.gz | 11.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ecraspay_django-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.5 kB
Release files / ecraspay_django-0.1.1.tar.gz
| Download URL | ecraspay_django-0.1.1.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19afa6da399b29e0e1851d452291068541054dfe6fe563cc0cd2e3e5860dba75
|
|
BLAKE2b-256 checksum How to use checksums |
53b1e3127d110cc70a3f7fa536f1a4002d72fe2fee90787748b602ec92b21d27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.9
|
Release files / ecraspay_django-0.1.1-py3-none-any.whl
| Download URL | ecraspay_django-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
11e00b9b0176b875ed5e7feea8a89559a8c71a00349687d8ced1c254ea7e9275
|
|
BLAKE2b-256 checksum How to use checksums |
f6b2cdfabf0ad6de6b2fe32951bfd36c89c45b3e1ca97228d16ee10dab59f95e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.9
|