Verify razorpay transaction ID – django/python

Razorpay is india’s first payment gateway and widely used now a days. This article is about how to verify razorpay transaction ID. You can verify razorpay transaction ID in just 3 steps.

Step 1: Install razorpay module

Step 2: get key ID and secret

Step 3: fetch transaction details

 

Step 1: Install razorpay module using pip

pip install razorpay

Step 2: Get key ID and secret

Login to razorpay dashboard and get key ID and key secret. You can find key Id and secret in razorpay dashboard from settings menu.

step1

You can get key Id and secret for test mode as well as for live mode. You can switch between modes from drop down menu shown here.

step 2

 

After selecting the mode(test/live), you can generate key ID and secret from API keys tab. Secret will be shown only one time and you can save or download the key ID and secret when you generate it. If you lost your secret then you can regenerate key ID and secret.

Step 3: Verify Transaction ID

After getting keyId and keySecret, you can fetch transaction details by following code.

client = razorpay.Client(auth=(keyId, keySecret))
apiResponse = client.payment.fetch(transactionId)

Source code:

Create a file razorpay.py and place code below in the file. Replace keyId and keySecret parameters with key ID and secret you get from razorpay dashboard(follow step 2).

import razorpay

def verifyTransactionId(transactionId = None):
   keyId = "XXXXXXXXXX"
   keySecret = "XXXXXXXXXX"
   apiUrl = "https://api.razorpay.com/v1/payments/"

   try:
       if transactionId:
           client = razorpay.Client(auth=(keyId, keySecret))
           apiResponse = client.payment.fetch(transactionId)
           #print("response text", apiResponse)

           if apiResponse.get('error', None):
               #print("\n\n error")
               return {"status": False, "apiResponse": apiResponse}
           elif apiResponse.get('id', None):
               #print("\n\n payment success")
               return {"status": True, "apiResponse": apiResponse}
           else:
               #print("\n\n Pending Approval")
               return {"status": False, "apiResponse": apiResponse}
       else:
           return {
                   "status": False,
                   "apiResponse": {
                       "error": {
                                "code":"BAD_REQUEST_ERROR",
                                "description":"transaction Id can not be empty"
                        }
                   }
                   }
   except Exception as e:
       print(e)
       return {"status": False, "apiResponse": str(e) }

Import this file where you want to use this.

#Import file

from razorpay import verifyTransactionId

Verify transaction Id and get transaction details

razorPayResponse = verifyTransactionId(transactionId)
if not razorPayResponse.get('status'):
    #print("razorPayResponse status false")
elif razorPayResponse.get('status'):
    #check whether transaction Id is not used earlier

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares