A Django integration for the ECRAS API.
Project description
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
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 ecraspay_django-0.1.1.tar.gz.
File metadata
- Download URL: ecraspay_django-0.1.1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19afa6da399b29e0e1851d452291068541054dfe6fe563cc0cd2e3e5860dba75
|
|
| MD5 |
490262eb587db0180f3dec38f72ef708
|
|
| BLAKE2b-256 |
53b1e3127d110cc70a3f7fa536f1a4002d72fe2fee90787748b602ec92b21d27
|
File details
Details for the file ecraspay_django-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ecraspay_django-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11e00b9b0176b875ed5e7feea8a89559a8c71a00349687d8ced1c254ea7e9275
|
|
| MD5 |
ee21e90a4aa9dad70cff3d3d230e1faf
|
|
| BLAKE2b-256 |
f6b2cdfabf0ad6de6b2fe32951bfd36c89c45b3e1ca97228d16ee10dab59f95e
|