Integrating Razorpay Payment Gateway in your Flask E-Commerce app

html python flask razorpay payment gateway e-commerce

Razorpay is one of the most popular payment services in India with a significant amount of customers because of its ease of use. This is a  short, crisp, and precise tutorial on integrating the service on your python-based backends like Flask or Django. For this tutorial, we will be only focusing on Flask but a very similar approach is followed in Django as well. 

Generating an order on Flask backend

Once the Continue button from the form is clicked, a post request will be sent to the backend which basically generates an Order ID and Payment ID for the user with the API keys obtained before. We will implement it step by step:

  1. Install the packages requests and razorpay using pip package management system:
    pip install razorpay requests 

     

  2. Import the above packages in your app.py script.
  3. Obtain the form values as shown:
  4. Store all the details in the notes json-like object
  5. You then need to declare the client object which will take the Razorpay key and Razorpay secret for authorization.
  6. Define the order amount (note that the amount is given in Paise, thus INR 999 = 999000 Paise), the currency, payment_capture status (1 means that the payment will be automatically accepted by the gateway and you don't need to manually accept it on your own) and the notes object declared above as a payment object. This payment JSON object automatically generates the order id which is returned to the frontend as a response.
  7. Render the payment.html template with response objects payment and razorpay_key.
Discussion