A Python library for easy integration with Nepali payment gateways like eSewa and Khalti.
Project description
Nepal Gateways 🇳🇵
A Python library providing a unified interface for integrating various Nepali payment gateways and digital wallets into your Python applications.
Overview
Integrating multiple payment gateways can be complex due to differing APIs, authentication mechanisms, and response formats. nepal-gateways aims to simplify this by offering:
- A consistent API structure for common operations like payment initiation and verification across different gateways.
- Clear error handling with custom exceptions.
- Type-hinted and well-documented code.
Supported Gateways
Currently, the following gateways are supported:
- eSewa (ePay v2 - with HMAC Signature) - See eSewa Client Documentation
- Khalti (ePayment v2 API) - See Khalti Client Documentation
- (Other gateways will be added based on demand and API availability)
Installation
You can install nepal-gateways using pip:
pip install nepal-gateways
Or with uv:
uv pip install nepal-gateways
The library requires the requests package for making HTTP calls, which will be installed automatically as a dependency.
Quick Start Examples
Below are quick start examples for the supported gateways. For full details, please refer to the specific documentation linked above.
Quick Start - eSewa Example
For full details, please see the eSewa Client Documentation.
1. Configuration & Initialization (eSewa):
from nepal_gateways import EsewaClient, ConfigurationError
from typing import Union # For Amount type alias if used in example
# Define type alias for clarity (Amount for eSewa can be float or int)
EsewaAmount = Union[int, float]
OrderID = str # Common OrderID type
# For Sandbox/UAT
esewa_sandbox_config = {
"product_code": "EPAYTEST", # Your sandbox merchant code from eSewa
"secret_key": "8gBm/:&EnhH.1/q", # eSewa's official UAT secret key
"success_url": "https://yourdomain.com/payment/esewa/success",
"failure_url": "https://yourdomain.com/payment/esewa/failure",
"mode": "sandbox"
}
try:
esewa_client = EsewaClient(config=esewa_sandbox_config)
except ConfigurationError as e:
print(f"eSewa Configuration Error: {e}")
# Handle error
2. Initiating a Payment (eSewa):
from nepal_gateways import InitiationError
merchant_order_id: OrderID = "ESORDER-001"
payment_amount: EsewaAmount = 100 # Base amount (e.g., Rs. 100)
try:
# For eSewa, 'amount' is the base, other charges are separate parameters.
# Total amount for signature will be amount + tax_amount + product_service_charge + product_delivery_charge
init_response = esewa_client.initiate_payment(
amount=payment_amount,
order_id=merchant_order_id,
tax_amount=0, # Example: 0 tax (formatted to "0" by client)
product_service_charge=0, # Example: 0 service charge
product_delivery_charge=0 # Example: 0 delivery charge
)
if init_response.is_redirect_required:
print(f"eSewa: Redirect User to: {init_response.redirect_url}")
print(f"eSewa: With Method: {init_response.redirect_method}") # Should be POST
print(f"eSewa: And Form Fields: {init_response.form_fields}")
# In a web app, render an HTML form that auto-submits these fields.
except InitiationError as e:
print(f"eSewa Initiation Failed: {e}")
3. Verifying a Payment (eSewa - in your callback handler):
eSewa redirects to your success_url with a data query parameter (Base64 encoded JSON).
from nepal_gateways import VerificationError, InvalidSignatureError
# Example: request_data_from_esewa = {"data": "ACTUAL_BASE64_STRING_FROM_ESEWA_CALLBACK"}
# Replace placeholder with actual data for testing
request_data_from_esewa = {"data": "GET_THIS_FROM_A_REAL_SANDBOX_TRANSACTION_CALLBACK"}
try:
verification = esewa_client.verify_payment(
transaction_data_from_callback=request_data_from_esewa
)
if verification.is_successful:
print(f"eSewa: Payment Verified for Order ID: {verification.order_id}, eSewa Txn ID: {verification.transaction_id}")
# Update your database, fulfill order
else:
print(f"eSewa: Payment Not Verified for Order ID: {verification.order_id}. Status: {verification.status_code}")
except InvalidSignatureError:
print("eSewa CRITICAL: Callback signature is invalid!")
except VerificationError as e:
print(f"eSewa Verification process error: {e}")
except Exception as e:
print(f"eSewa: Unexpected error during verification: {e}")
Quick Start - Khalti Example
For full details, please see the Khalti Client Documentation.
1. Configuration & Initialization (Khalti):
from nepal_gateways import KhaltiClient, ConfigurationError # Assuming Esewa types already imported
# For Khalti, amount is always in Paisa (integer)
KhaltiAmount = int
# For Sandbox/UAT
khalti_sandbox_config = {
"live_secret_key": "your_khalti_sandbox_live_secret_key", # Get from test-admin.khalti.com
"return_url_config": "https://yourdomain.com/payment/khalti/callback",
"website_url_config": "https://yourdomain.com",
"mode": "sandbox"
}
try:
khalti_client = KhaltiClient(config=khalti_sandbox_config)
except ConfigurationError as e:
print(f"Khalti Configuration Error: {e}")
# Handle error
2. Initiating a Payment (Khalti):
# from nepal_gateways import InitiationError # Already imported
merchant_order_id_khalti: OrderID = "KHORDER-002"
payment_amount_paisa: KhaltiAmount = 15000 # Rs. 150 in Paisa
purchase_description = "Monthly Subscription"
try:
init_response_khalti = khalti_client.initiate_payment(
amount=payment_amount_paisa,
order_id=merchant_order_id_khalti,
description=purchase_description,
customer_info={"name": "Test Customer", "email": "test@example.com"} # Optional
)
if init_response_khalti.is_redirect_required:
print(f"Khalti: PIDX: {init_response_khalti.payment_instructions.get('pidx')}")
print(f"Khalti: Redirect User to: {init_response_khalti.redirect_url}") # This is a GET redirect
# In a web app, perform an HTTP redirect to this URL.
except InitiationError as e:
print(f"Khalti Initiation Failed: {e}")
3. Verifying a Payment (Khalti - in your callback handler):
Khalti redirects to your return_url (a GET request) with query parameters like pidx, status, txnId, etc.
# from nepal_gateways import VerificationError # Already imported
# Example: query_params_from_khalti = {"pidx": "actual_pidx", "txnId": "actual_txnid", ...}
# Replace placeholder with actual data for testing
query_params_from_khalti = {"pidx": "GET_PIDX_FROM_REAL_SANDBOX_CALLBACK"}
try:
verification_khalti = khalti_client.verify_payment(
transaction_data_from_callback=query_params_from_khalti
)
if verification_khalti.is_successful: # Checks Khalti Lookup API status "Completed"
print(f"Khalti: Payment Verified for Order ID (PIDX): {verification_khalti.order_id}, Khalti Txn ID: {verification_khalti.transaction_id}")
# Update your database, fulfill order
else:
print(f"Khalti: Payment Not Verified for Order ID (PIDX): {verification_khalti.order_id}. Status: {verification_khalti.status_code}")
except VerificationError as e: # Covers various verification issues including API errors
print(f"Khalti Verification process error: {e}")
except Exception as e:
print(f"Khalti: Unexpected error during verification: {e}")
Logging
This library uses Python's standard logging module. All loggers within this package are children of the nepal_gateways logger.
To enable logging (e.g., for debugging), configure the nepal_gateways logger in your application:
import logging
# Simplest configuration to see debug messages on console
logging.basicConfig(level=logging.DEBUG)
# Or, more specific configuration:
# logger = logging.getLogger('nepal_gateways')
# logger.setLevel(logging.DEBUG)
# stream_handler = logging.StreamHandler() # Example handler
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# stream_handler.setFormatter(formatter)
# logger.addHandler(stream_handler)
The library itself does not add any handlers by default (except a NullHandler at the package level to prevent "No handler found" warnings if the application has not configured logging).
Contributing
Contributions are welcome! If you'd like to add support for a new gateway, improve existing ones, or fix bugs, please refer to our CONTRIBUTING.md file.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer
This is an unofficial library. While efforts are made to ensure correctness and security, always perform thorough testing, especially with live credentials and real transactions. The maintainers are not responsible for any financial loss or issues arising from the use of this software. Always refer to the official documentation of the respective payment gateways.
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 nepal_gateways-0.2.0.tar.gz.
File metadata
- Download URL: nepal_gateways-0.2.0.tar.gz
- Upload date:
- Size: 78.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d00a4a72016c61ff15b59363333969c1ae378fee670fa433f79cb0f4f4cf4ec9
|
|
| MD5 |
1166a1cb8ad51b6978a4b1f375d8ddee
|
|
| BLAKE2b-256 |
dde4e63f131d734720baa0528a158727118783d668d9251d53dce1a30c94d22b
|
File details
Details for the file nepal_gateways-0.2.0-py3-none-any.whl.
File metadata
- Download URL: nepal_gateways-0.2.0-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c1746349506175bdcb78896928f6372a9071178ccea421a5a504a664ba4841b
|
|
| MD5 |
7881520af36d86348483ea1fe6d735f5
|
|
| BLAKE2b-256 |
d4a521ba5d3dfe62548c93087a389b6d8061e236643f10a601d96350c6123b1c
|