Skip to content

munal777/esewa-payment-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

40 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

eSewa Payment Integration

A full-stack payment integration system for eSewa (Nepal's digital wallet) built with Django REST Framework (Backend) and React + Vite (Frontend).

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This project demonstrates a complete eSewa payment integration workflow, including:

  • Payment initialization with HMAC-SHA256 signature generation
  • Secure payment form submission to eSewa gateway
  • Payment callback handling and verification
  • Beautiful UI with payment status display

โœจ Features

  • ๐Ÿ” Secure Payment Processing: HMAC-SHA256 signature generation for payment authentication
  • ๐Ÿ’ณ eSewa Integration: Full integration with eSewa's v2 API
  • โš›๏ธ Modern Frontend: React 19 with Vite for fast development
  • ๐ŸŽจ Tailwind CSS: Beautiful, responsive UI components
  • ๐Ÿ”„ Payment Verification: Automatic payment status verification and display
  • ๐Ÿ“ฑ Mobile Responsive: Works seamlessly on all devices
  • ๐Ÿš€ RESTful API: Django REST Framework backend with clean API endpoints

๐Ÿ›  Tech Stack

Backend

  • Python 3.x
  • Django 5.2.6 - Web framework
  • Django REST Framework 3.16.1 - API development
  • django-cors-headers 4.7.0 - CORS handling
  • python-decouple 3.8 - Environment variable management

Frontend

  • React 19.1.1 - UI library
  • Vite 7.1.2 - Build tool
  • React Router DOM 7.8.2 - Routing
  • Axios 1.11.0 - HTTP client
  • Tailwind CSS 4.1.13 - Styling
  • Lucide React 0.543.0 - Icons

๐Ÿ“ Project Structure

esewa-integration/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ manage.py
โ”‚   โ”œโ”€โ”€ db.sqlite3
โ”‚   โ”œโ”€โ”€ core/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ admin.py
โ”‚   โ”‚   โ”œโ”€โ”€ apps.py
โ”‚   โ”‚   โ”œโ”€โ”€ models.py
โ”‚   โ”‚   โ”œโ”€โ”€ views.py          # Payment API views
โ”‚   โ”‚   โ”œโ”€โ”€ urls.py           # API endpoints
โ”‚   โ”‚   โ””โ”€โ”€ utils.py          # Signature generation utilities
โ”‚   โ”œโ”€โ”€ myproject/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ settings.py       # Django settings
โ”‚   โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ”‚   โ””โ”€โ”€ wsgi.py
โ”‚   โ””โ”€โ”€ esewa-env/            # Virtual environment
โ”‚
โ””โ”€โ”€ frontend/
    โ”œโ”€โ”€ package.json
    โ”œโ”€โ”€ vite.config.js
    โ”œโ”€โ”€ index.html
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ main.jsx
    โ”‚   โ”œโ”€โ”€ App.jsx
    โ”‚   โ”œโ”€โ”€ components/
    โ”‚   โ”‚   โ””โ”€โ”€ payments/
    โ”‚   โ”‚       โ””โ”€โ”€ EsewaPaymentForm.jsx
    โ”‚   โ”œโ”€โ”€ pages/
    โ”‚   โ”‚   โ”œโ”€โ”€ Home.jsx
    โ”‚   โ”‚   โ””โ”€โ”€ PaymentResult.jsx
    โ”‚   โ”œโ”€โ”€ services/
    โ”‚   โ”‚   โ””โ”€โ”€ paymentService.js
    โ”‚   โ”œโ”€โ”€ config/
    โ”‚   โ”‚   โ””โ”€โ”€ api.js
    โ”‚   โ””โ”€โ”€ routes/
    โ”‚       โ””โ”€โ”€ AppRoutes.jsx
    โ””โ”€โ”€ public/

๐Ÿ“ฆ Prerequisites

  • Python 3.8 or higher
  • Node.js 18.x or higher
  • npm or yarn package manager
  • eSewa Merchant Account (for production) or Test Credentials

๐Ÿš€ Installation

Backend Setup

  1. Navigate to backend directory:

    cd backend
  2. Create and activate virtual environment:

    python -m venv esewa-env
    
    # On Windows
    esewa-env\Scripts\activate
    
    # On macOS/Linux
    source esewa-env/bin/activate
  3. Install dependencies:

    pip install django djangorestframework django-cors-headers python-decouple
  4. Create .env file in the backend directory:

    DJANGO_SECRET_KEY=your-django-secret-key-here
    DEBUG=True
    
    # eSewa Configuration
    ESEWA_SECRET_KEY=8gBm/:&EnhH.1/q
    ESEWA_MERCHANT_CODE=EPAYTEST
    ESEWA_PAYMENT_CALLBACK_URL=http://localhost:5173/payment-result

    Note: The above credentials are for eSewa's test environment. For production, use your actual merchant credentials.

  5. Run migrations:

    python manage.py migrate
  6. Create superuser (optional):

    python manage.py createsuperuser
  7. Start development server:

    python manage.py runserver

    Backend will be available at http://localhost:8000

Frontend Setup

  1. Navigate to frontend directory:

    cd frontend
  2. Install dependencies:

    npm install
  3. Create .env file in the frontend directory:

    VITE_API_URL=http://localhost:8000
  4. Start development server:

    npm run dev

    Frontend will be available at http://localhost:5173

โš™๏ธ Configuration

Backend Environment Variables

Variable Description Example
DJANGO_SECRET_KEY Django secret key your-secret-key
DEBUG Debug mode True or False
ESEWA_SECRET_KEY eSewa merchant secret key 8gBm/:&EnhH.1/q (test)
ESEWA_MERCHANT_CODE eSewa merchant/product code EPAYTEST (test)
ESEWA_PAYMENT_CALLBACK_URL Success/Failure callback URL http://localhost:5173/payment-result

Frontend Environment Variables

Variable Description Example
VITE_API_URL Backend API base URL http://localhost:8000

๐Ÿ“š API Documentation

Base URL

http://localhost:8000

Endpoints

1. Initialize Payment

Endpoint: POST /payments/esewa/

Description: Generates eSewa payment payload with signature for secure payment processing.

Request Body:

{
  "amount": "1000",
  "tax_amount": "0",
  "product_service_charge": "0",
  "product_delivery_charge": "0"
}

Request Parameters:

Parameter Type Required Description
amount string Yes Payment amount
tax_amount string No Tax amount (default: "0")
product_service_charge string No Service charge (default: "0")
product_delivery_charge string No Delivery charge (default: "0")

Response:

{
  "amount": "1000",
  "tax_amount": "0",
  "total_amount": "1000",
  "transaction_uuid": "550e8400-e29b-41d4-a716-446655440000",
  "product_code": "EPAYTEST",
  "product_service_charge": "0",
  "product_delivery_charge": "0",
  "success_url": "http://localhost:5173/payment-result",
  "failure_url": "http://localhost:5173/payment-result",
  "signed_field_names": "total_amount,transaction_uuid,product_code",
  "signature": "Gw2ax/N4W1q0TBqoXfwLhxuLOjDUxIzd08kLCHPn42s="
}

Response Fields:

Field Type Description
amount string Payment amount
tax_amount string Tax amount
total_amount string Total payment amount
transaction_uuid string Unique transaction identifier
product_code string eSewa merchant code
product_service_charge string Service charge
product_delivery_charge string Delivery charge
success_url string Success callback URL
failure_url string Failure callback URL
signed_field_names string Comma-separated field names used in signature
signature string HMAC-SHA256 signature

cURL Example:

curl -X POST http://localhost:8000/payments/esewa/ \
  -H "Content-Type: application/json" \
  -d '{"amount": "1000"}'

2. Verify Payment

Endpoint: GET /payments/esewa/verify/

Description: Verifies payment status after eSewa callback.

Query Parameters:

Parameter Type Required Description
data string Yes Base64-encoded payment data from eSewa

Request Example:

GET /payments/esewa/verify/?data=eyJ0cmFuc2FjdGlvbl91dWlkIjoiMTIzNDU2IiwicHJvZHVjdF9jb2RlIjoiRVBBWVRFU1QiLCJ0b3RhbF9hbW91bnQiOiIxMDAwIiwic3RhdHVzIjoiQ09NUExFVEUifQ==

Response:

{
  "data": {
    "transaction_uuid": "550e8400-e29b-41d4-a716-446655440000",
    "product_code": "EPAYTEST",
    "total_amount": "1000",
    "status": "COMPLETE",
    "transaction_id": "0008ACQ",
    "ref_id": "000AXC"
  }
}

Response Fields:

Field Type Description
transaction_uuid string Original transaction UUID
product_code string eSewa merchant code
total_amount string Transaction amount
status string Payment status (COMPLETE, FAILED, PENDING)
transaction_id string eSewa transaction ID
ref_id string eSewa reference ID

Status Values:

  • COMPLETE - Payment successful
  • FAILED - Payment failed
  • PENDING - Payment pending verification

Error Response:

{
  "error": "No data received"
}

cURL Example:

curl -X GET "http://localhost:8000/payments/esewa/verify/?data=eyJ0cmFuc2FjdGlvbl91dWlkIjoiMTIzNDU2In0="

Error Handling

The API uses standard HTTP status codes:

Status Code Description
200 Success
400 Bad Request - Missing or invalid parameters
500 Internal Server Error

๐ŸŽฎ Usage

Making a Payment

  1. Open the application in your browser at http://localhost:5173

  2. Enter payment amount in the form (default: NPR 1000)

  3. Click "Pay with eSewa" button

  4. Complete payment on eSewa's payment page:

    • For test environment, you can use demo credentials
    • Test Success ID: 9806800001, 9806800002, 9806800003, 9806800004, 9806800005
    • Password: Nepal@123
    • MPIN: 1234
  5. View payment result - You'll be redirected to the payment result page showing:

    • โœ… Success status with transaction details
    • โŒ Failure status
    • โณ Pending status

Frontend Components

EsewaPaymentForm Component

Located at src/components/payments/EsewaPaymentForm.jsx

import EsewaPaymentForm from './components/payments/EsewaPaymentForm';

function App() {
  return <EsewaPaymentForm />;
}

PaymentResult Component

Located at src/pages/PaymentResult.jsx

Automatically handles the callback from eSewa and displays the payment status.

Payment Service

The paymentService.js provides two main functions:

import { generateEsewaPayment, EsewaPaymentVerify } from './services/paymentService';

// Initialize payment
const response = await generateEsewaPayment(amount);

// Verify payment
const result = await EsewaPaymentVerify({ data: encodedData });

๐Ÿ”„ How It Works

Payment Flow

  1. User Initiates Payment:

    • User enters amount in the frontend form
    • Frontend sends POST request to /payments/esewa/
  2. Backend Generates Payload:

    • Generates unique transaction UUID
    • Creates signing string from required fields
    • Generates HMAC-SHA256 signature using merchant secret key
    • Returns complete payment payload
  3. Frontend Submits to eSewa:

    • Creates a hidden HTML form with payload data
    • Submits form to eSewa gateway (https://rc-epay.esewa.com.np/api/epay/main/v2/form)
    • User is redirected to eSewa payment page
  4. User Completes Payment:

    • User logs into eSewa
    • Reviews payment details
    • Confirms payment
  5. eSewa Redirects Back:

    • eSewa redirects to callback URL with base64-encoded payment data
    • Frontend extracts the data query parameter
  6. Payment Verification:

    • Frontend sends GET request to /payments/esewa/verify/ with encoded data
    • Backend decodes and parses payment result
    • Returns payment status and details
  7. Display Result:

    • Frontend displays success/failure/pending status
    • Shows transaction details

Signature Generation

The backend uses HMAC-SHA256 for signature generation:

# 1. Build signing string
signed_string = "total_amount=1000,transaction_uuid=550e8400-e29b-41d4-a716-446655440000,product_code=EPAYTEST"

# 2. Generate HMAC-SHA256 signature
signature = base64.b64encode(
    hmac.new(
        secret_key.encode('utf-8'),
        signed_string.encode('utf-8'),
        hashlib.sha256
    ).digest()
).decode('utf-8')

Transaction UUID

Each payment gets a unique UUID v4 identifier:

import uuid

transaction_uuid = str(uuid.uuid4())
# Example: "550e8400-e29b-41d4-a716-446655440000"

๐Ÿ”’ Security

Best Practices Implemented

  1. HMAC Signature: All payments include HMAC-SHA256 signature for authentication
  2. Environment Variables: Sensitive credentials stored in .env files (not in Git)
  3. CORS Configuration: Controlled cross-origin requests
  4. UUID Transactions: Unique identifiers prevent replay attacks
  5. Base64 Encoding: Payment callback data is base64-encoded
  6. HTTPS: Use HTTPS in production for secure data transmission

Security Recommendations

  • โœ… Never commit .env files to version control
  • โœ… Use strong, unique secret keys in production
  • โœ… Implement rate limiting on payment endpoints
  • โœ… Add logging and monitoring for payment transactions
  • โœ… Validate and sanitize all user inputs
  • โœ… Use HTTPS for all production endpoints
  • โœ… Implement webhook signature verification
  • โœ… Store transaction records in database for audit trail

Production Checklist

  • Set DEBUG=False in Django settings
  • Use production eSewa credentials
  • Configure proper CORS origins (remove CORS_ALLOW_ALL_ORIGINS)
  • Set up HTTPS/SSL certificates
  • Configure database backups
  • Implement comprehensive logging
  • Set up monitoring and alerts
  • Add rate limiting middleware
  • Configure allowed hosts in Django settings
  • Use environment-specific configuration

๐Ÿงช Testing

Test eSewa Credentials

For development and testing, use eSewa's test environment:

  • Gateway URL: https://rc-epay.esewa.com.np/api/epay/main/v2/form
  • Secret Key: 8gBm/:&EnhH.1/q
  • Merchant Code: EPAYTEST

Test Login Credentials:

  • ID: 9806800001, 9806800002, 9806800003, 9806800004, 9806800005
  • Password: Nepal@123
  • MPIN: 1234

Running Tests

# Backend tests
cd backend
python manage.py test

# Frontend tests (if configured)
cd frontend
npm test

๐Ÿ› Troubleshooting

Common Issues

  1. CORS Errors:

    • Ensure django-cors-headers is installed and configured
    • Check CORS_ALLOW_ALL_ORIGINS in settings.py
  2. Signature Mismatch:

    • Verify ESEWA_SECRET_KEY matches your merchant account
    • Check field order in signed_field_names
  3. Payment Callback Not Working:

    • Ensure callback URL is accessible
    • Check URL format (must include protocol: http:// or https://)
  4. Module Import Errors:

    • Activate virtual environment
    • Reinstall dependencies: pip install -r requirements.txt

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

๐Ÿ“ž Support

For issues and questions:

  • Open an issue on GitHub
  • Contact eSewa support for merchant account queries

๐Ÿ™ Acknowledgments

  • eSewa - Nepal's digital wallet
  • Django REST Framework team
  • React and Vite communities

Note: This is a development implementation. For production use, implement additional security measures, database storage for transactions, proper error handling, and comprehensive logging.

๐Ÿ“ Additional Resources

About

A simple eSewa integration project with a Django REST API and React frontend for handling secure payments and transaction verification.

Topics

Resources

Stars

Watchers

Forks

Contributors