Skip to main content

Django-esewa

A simple, developer-friendly package for integrating the eSewa Payment Gateway into Django applications.

Learn Django (100% Free) at your own pace

Overview

django-esewa was developed by Nischal Lamichhane to simplify eSewa integration for Python/Django developers. It aims to handle common payment gateway tasks like generating HMAC signatures, verifying transactions, and status checks (in future versions).

Features

  • HMAC Key Generation: Easily generate the signature required for eSewa requests.
  • Customization: Configure secret keys, product codes, success URLs, and failure URLs.

Future Goals

  • Transaction status verification.
  • Improved documentation for all class methods.

QuickStart

pip install django-esewa

Even though you can use the generate_signature(from django_esewa) function without creating an object of EsewaPayment, if you want to use other features, you need to instantiate an object of the class EsewaPayment.


Usage

All the amounts like amount, tax_amount, total_amount, product_delivery_charge, product_service_charge will be defaulted to 0. success_url will defaulted to http://localhost:8000/success/ and failure_url will be defaulted to http://localhost:8000/failure/. Secret key will be defaulted to "8gBm/:&EnhH.1/q". Product code will be defaulted to EPAYTEST. Transaction uuid will be defaulted to None.

Generating HTML Form

Views.py

from django_esewa import EsewaPayment

def confirm_order(request,id):
    order = Order.objects.get(id=id)

    payment = EsewaPayment(
        product_code=order.code,
        success_url="http://yourdomain.com/success/",
        failure_url="http://yourdomain.com/failure/",
        amount=order.amount,
        tax_amount=calculate_tax(),
        total_amount=order.total_amount,
        product_delivery_charge=order.delivery_charge,
        product_service_charge=order.service_charge
        transaction_uuid="transaction uuid",
        secret_key="your_secret_key",

    )
    signature = payment.create_signature() #Saves the signature as well as return it

    context = {
        'form':payment.generate_form()
    }
    
    return render(request,'order/checkout.html',context)

order/checkout.html

<form action="https://rc-epay.esewa.com.np/api/epay/main/v2/form" method="POST">
    {{form|safe}}
    <button type="submit">Pay with Esewa </button>
</form>

Generating a Signature

The generate_signature function helps create the HMAC signature required by eSewa for secure transactions.

Function Signature:

def generate_signature(
    total_amount: float,
    transaction_uuid: str,
    key: str = "8gBm/:&EnhH.1/q",
    product_code: str = "EPAYTEST"
) -> str:

Example:

from django_esewa import generate_signature

# During Development
signature = generate_signature(1000, "123abc")

# In Production
signature = generate_signature(1000, "123abc", "<your_private_key>", "<product_code>")

Using the EsewaPayment Class

EsewaPayment provides additional configuration options for success and failure URLs. List of all methods in EsewaPayment:

  • __init__()
  • create_signature()
  • generate_form()
  • get_status()
  • is_completed()
  • verify_signature()
  • log_transaction()
  • __eq__()

List of In-development methods:

  • generate_redirect_url()
  • refund_payment()
  • simulate_payment()

Initialization:

from django_esewa import EsewaPayment

payment = EsewaPayment(
    product_code="EPAYTEST",
        success_url="http://localhost:8000/success/",
        failure_url="http://localhost:8000/failure/",
        amount=100,
        tax_amount=0,
        total_amount=100,
        product_service_charge=0,
        product_delivery_charge=0,
        transaction_uuid="11-200-111sss1",
)

Signature generation

signature = payment.create_signature()

Form Generation

form = payment.generate_form()

Settings

From Version 1.0.8, We are improvising this package to work not only with Django but also with Other Python Frameworks. so there is no Explicit configuration for Django Settings. Feel free to use Previous configuration and then dynamically use the credentials using settings.getattr or python-decouple

Contributing

Current To-Do List

  • Write documentation for all methods in the EsewaPayment class.
  • Add refund method

How to Contribute

  1. Fork this repository.
  2. Create a feature branch.
  3. Commit your changes with clear messages.
  4. Submit a pull request (PR) with a detailed description of your changes.

Credits

django-esewa is maintained by Nischal Lamichhane. This package was created as a last-ditch effort to help Python/Django developers integrate eSewa Payment Gateway efficiently.

Metadata

Release files for django-esewa 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-esewa 1.1.0
File Size Uploaded
django_esewa-1.1.0.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-esewa 1.1.0
File Interpreter ABI Platform
django_esewa-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.9 kB

Release files / django_esewa-1.1.0.tar.gz

Download URL django_esewa-1.1.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4ce246a748b330677e944a2809e36688b14c5df75b97646ad851f791540e26c0
BLAKE2b-256 checksum
How to use checksums
3b098dcfc2268528b69464e57de33a1d4f754c33d86e11b889151983f2087e9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Release files / django_esewa-1.1.0-py3-none-any.whl

Download URL django_esewa-1.1.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97504583261796170c5d0255eaf21c104aec55517193d03a4de1cb6fddadf8f4
BLAKE2b-256 checksum
How to use checksums
96bbbe727d3dcbe554a5dfe9220dea6a82dc5fdb4c6b7e24d6088ab1ccc8786e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page