Skip to main content

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

  1. Install the package using pip:

    pip install ecraspay-django
    
  2. Add the package to your Django project:

    In your settings.py, add the package to INSTALLED_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:

  1. Payment
    Represents a payment initiated using the Ecraspay API.

    • Fields: payment_reference, transaction_reference, amount, status, currency, etc.
  2. 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

  1. Clone the repository:

    git clone https://github.com/yourusername/ecraspay-django.git
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Run tests:

    python manage.py test
    
  4. 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:


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ecraspay_django-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ecraspay_django-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file ecraspay_django-0.1.0.tar.gz.

File metadata

  • Download URL: ecraspay_django-0.1.0.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

Hashes for ecraspay_django-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4428e09bce83ee624f99ab0b06b7bcd0630eb97202bbc0734134e2c2b99f0b8a
MD5 0dd53aabff9ba847534b3ca158de9abe
BLAKE2b-256 00442d1a2a5138fd4fd9fa8e435d256ffbeb55348ece5cf66e19291432fe7880

See more details on using hashes here.

File details

Details for the file ecraspay_django-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ecraspay_django-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e38454c5636ada9ce1260557cf64093189d4451be335baa9e1dfa12aaa152cbc
MD5 8badba97e16822139157dfc26de9dd05
BLAKE2b-256 7422f8bb9e042cc088ef9c96454c5592ebc3976134a059e93d38838164f0b2b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page