×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION Roadmap
     ❯   

ESEWA INTEGRATION

Status Check

Status Check

The Status Check functionality allows you to inquire about a transaction's status when it has been initiated but no response has been received from eSewa or from your merchant account. This is crucial for ensuring that users are kept informed about their payment statuses.

Success View

After successful payment from esewa's side it will redirect back to the success_url that we sent on the formdata. But esewa will add a 'data' parameter to the url making it look like localhost:8000/success/?data=yJ0cmFuc2FjdGlvbl9...... the value in the data is in base64 encoding. after decoding we can see it is in json format. so following is the code to check the status and validity of the transaction by using esewa's status checking API

import base64
import requests
from django.shortcuts import render
from django.http import HttpResponseBadRequest
import json

def success_view(request):
    context={}  # Create a dictionary to store the response
    data = request.GET.get('data')  # Get the data from the URL
    decoded_data = base64.b64decode(data).decode('utf-8')   # Decode the data
    data_dict = json.loads(decoded_data)    # Convert the data to a dictionary
    # Get the values from the dictionary
    total_amount = data_dict['total_amount'] 
    transaction_uuid = data_dict['transaction_uuid']
    product_code = data_dict['product_code']

    # Make a request to the eSewa API to get the transaction status
    request_url = f'https://uat.esewa.com.np/api/epay/transaction/status/?product_code={product_code}&total_amount={total_amount}&transaction_uuid={transaction_uuid}'
    response = requests.get(request_url)
    response = json.loads(response.text)

    # Put the Status in message key of the context dictionary
    context['message'] = response['status']
    print(context)
    return render(request, 'success.html',context )

Success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Status</title>
</head>
<body>
    <h1>{{message}}</h1>
</body>
</html>

After Successful Payment

Upon successful payment, the user will be redirected to the specified success URL. The response parameters are encoded in Base64. Below is an example of the decoded response body:

{
  "transaction_code": "0LD5CEH",
  "status": "COMPLETE",
  "total_amount": "1,000.0",
  "transaction_uuid": "240613-134231",
  "product_code": "NP-ES-ABHISHEK-EPAY",
  "signed_field_names": "transaction_code,status,total_amount,transaction_uuid,product_code,signed_field_names",
  "signature": "Mpwy0TFlHqpJjFUDGic+22mdoenITT+Ccz1LC61qMAc="
}

Here is an example of the response body encoded in Base64:

eyJ0cmFuc2FjdGlvbl9jb2RlIjoiMExENUNFSCIsInN0YXR1cyI6IkNPTVBMRVRFI
iwidG90YWxfYW1vdW50IjoiMSwwMDAuMCIsInRyYW5zYWN0aW9uX3V1aWQiOiIyNDA
2MTMtMTM0MjMxIiwicHJvZHVjdF9jb2RlIjoiTlAtRVMtQUJISVNIRUstRVBBWSI
sInNpZ25lZF9maWVsZF9uYW1lcyI6InRyYW5zYWN0aW9uX2NvZGUsc3RhdHVzLHR
vdGFsX2Ftb3VudCx0cmFuc2FjdGlvbl91dWlkLHByb2R1Y3RfY29kZSxzaWduZWRfZ
mllbGRfbmFtZXMiLCJzaWduYXR1cmUiOiJNcHd5MFRGbEhxcEpqRlVER2ljKzIybWR
vZW5JVFQrQ2N6MUxDNjFxTUFjPSJ9

Make sure to verify the integrity of the response body by comparing the signature that eSewa sends with the signature that you generate. The signature should be generated using the same method as the request's signature.

Transaction Status Check

To check the transaction status when no response is received, you can call the eSewa API with the following parameters:

  • product_code: Your product code used in the transaction.
  • pid: The transaction UUID.
  • amount: The total amount of the transaction.

The API URL for checking the transaction status is as follows:

Example Response

The response for a transaction status check will provide information such as:

{
  "product_code": "EPAYTEST",
  "transaction_uuid": "123",
  "total_amount": 100,
  "status": "COMPLETE",
  "ref_id": "0001TS9"
}

Response Parameter Description and Format

Response Types Response Description Response Format
PENDING Payment initiated but not completed yet
{ 
"product_code": "EPAYTEST",
 "transaction_uuid": "240508-101430", 
"total_amount": 100.0, 
"status": "PENDING", 
"ref_id": null 
}
COMPLETE Successful payment
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-10108", 
"total_amount": 100.0, 
"status": "COMPLETE", 
"ref_id": "0007G36" 
}
FULL_REFUND Full payment refunded to the customer
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-101431", 
"total_amount": 100, 
"status": "FULL_REFUND", 
"ref_id": "0007G36" 
}
PARTIAL_REFUND Partial payment refunded to the customer
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-101431", 
"total_amount": 100.0, 
"status": "PARTIAL_REFUND", 
"ref_id": "0007G36" 
}
AMBIGUOUS Payment is in a halted state
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-101431", 
"total_amount": 100.0, 
"status": "AMBIGUOUS", 
"ref_id": "0KDL6NA"
 }
NOT_FOUND Payment terminated at eSewa: Session expired
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-101430", 
"total_amount": 100.0, 
"status": "NOT_FOUND", 
"ref_id": null 
}
CANCELED Canceled/reversed from eSewa side
{ 
"product_code": "EPAYTEST", 
"transaction_uuid": "240508-102939", 
"total_amount": 10.0, 
"status": "CANCELED", 
"ref_id": "0KDL6NA" 
}
Service is currently unavailable
{ "code": 0, "error_message": "Service is currently unavailable" }

References


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.