Implementation of eSewa Integration in Django
In this section, we will walk through the implementation of the eSewa payment gateway within a Django application. This integration will involve creating a product model, generating a signature for secure transactions, and managing the payment process.
1. Product Model
First, let's create a Product model to represent the items for sale. This model will include attributes for the product details such as name, price, and code.
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
code = models.CharField(max_length=20, unique=True) # eSewa product code
def __str__(self):
return self.name
2. Generating the Signature
Before processing a payment, we need to generate a signature using HMAC/SHA256 to ensure the transaction's integrity. The required parameters include total_amount, transaction_uuid, and product_code. For development Esewa has specified that we need to use the secret key's value as "8gBm/:&EnhH.1/q" Here's how to create the signature in Django:
import hmac
import hashlib
import base64
def generate_signature(key, message):
key = key.encode('utf-8')
message = message.encode('utf-8')
hmac_sha256 = hmac.new(key, message, hashlib.sha256)
digest = hmac_sha256.digest()
#Convert the digest to a Base64-encoded string
signature = base64.b64encode(digest).decode('utf-8')
return signature
3. Payment View
We will create a view to handle the payment process. This view will generate the necessary parameters, including the signature, and render a checkout form for the user to submit the payment.
from django.shortcuts import render
from django.utils.crypto import get_random_string
from .models import Product
def checkout(request, product_id):
product = Product.objects.get(id=product_id)
transaction_uuid = uuid.uuid4()
tax_amount = 10
total_amount = product.price + tax_amount
secret_key = '8gBm/:&EnhH.1/q'
data_to_sign = f"total_amount={total_amount},transaction_uuid={transaction_uuid},product_code=EPAYTEST"
result = generate_signature(secret_key, data_to_sign)
context = {
'product': product,
'tax_amount': tax_amount,
'total_amount': total_amount,
'transaction_uuid': transaction_uuid,
'product_delivery_charge': 0,
'product_service_charge': 0,
'signature': result,
}
return render(request, 'checkout.html', context)
4. Checkout Template
Next, we will create a simple HTML template for the checkout process. This form will send the payment details to eSewa.
<!-- templates/checkout.html -->
<form action="https://rc-epay.esewa.com.np/api/epay/main/v2/form" method="POST">
<input type="hidden" name="amount" value="{{ product.price }}" required>
<input type="hidden" name="tax_amount" value="{{ tax_amount }}" required>
<input type="hidden" name="total_amount" value="{{ total_amount }}" required>
<input type="hidden" name="transaction_uuid" value="{{ transaction_uuid }}" required>
<input type="hidden" name="product_code" value="{{ product.code }}" required>
<input type="hidden" name="product_service_charge" value="0" required>
<input type="hidden" name="product_delivery_charge" value="0" required>
<input type="hidden" name="success_url" value="https://yourdomain.com/success" required>
<input type="hidden" name="failure_url" value="https://yourdomain.com/failure" required>
<input type="hidden" name="signed_field_names" value="total_amount,transaction_uuid,product_code" required>
<input type="hidden" name="signature" value="{{ signature }}" required>
<input type="submit" value="Pay with eSewa">
</form>
5. URLs Configuration
Lastly, let's add a URL pattern to route requests to the checkout view.
from django.urls import path
from .views import checkout
urlpatterns = [
path('checkout/<int:product_id>/', checkout, name='checkout'),
]
Conclusion
In this implementation guide, we created a Product model, generated a secure signature using HMAC/SHA256, and set up a checkout process that integrates with eSewa's payment gateway. By following these steps, you can effectively handle transactions and ensure the integrity of the payment process.