๐Ÿ’ก Generating a UPI QR Code in Frappe Using Python

Unified Payments Interface (UPI) has become a preferred method of digital payments in India, offering quick, secure, and real-time transactions.

 · 2 min read

Integrating UPI QR codes directly into business documents like Sales Invoices or Quotations simplifies the payment process for clients. Instead of manually entering UPI details, customers can simply scan a QR code and pay instantly.


This blog will guide you through generating a UPI QR code dynamically using Python and integrating it within the Frappe framework.

โœ… Benefits of Using UPI QR Codes in ERP Systems

  • ๐Ÿ”’ Secure and instant payments
  • ๐Ÿงพ Paperless and contactless process
  • ๐Ÿ“ฉ Easy to embed in Print Formats
  • ๐Ÿ’ฐ Speeds up payment collection
  • ๐Ÿ“‰ Reduces manual errors in UPI transfers

๐Ÿ› ๏ธ How We Developed It

Add a new field in your custom or standard Doctype (e.g., Sales Invoice, Quotation):

  • Label: UPI QR String
  • Field Type: Small Text
  • Fieldname: custom_upi_qr_link

Step 2: Generate UPI String in validate Method

In your custom app or custom script, add the following logic to generate the UPI string:


def validate(self):
    self.custom_upi_qr_link = (
        f"upi://pay?pa={UpiID}"
        f"&pn={company_name or UPI_Account_Name}"
        f"&tn=Same State GST Client"
        f"&tr=&cu=INR&mode=01"
        f"&am={self.grand_total}"
    )

Step 3: Define get_qr_code Method in Your Custom App

Use the pyqrcode Python library to generate a base64 PNG QR code from the UPI string:


import pyqrcode

def get_qr_code(qr_text, scale=5):
    return pyqrcode.create(qr_text).png_as_base64_str(scale=scale, quiet_zone=1)


Step 4: Expose the Method in hooks.py

Make your method available in Jinja (for print formats):


jinja = {
    "methods": [
        "your_app_path.module_name.get_qr_code",
    ]
}

Replace your_app_path.module_name with the actual path to your method.

Step 5: Use in Print Format (HTML)

Now, you can use this function in any print format to show the QR code:


<img src="data:image/png;base64, {{ get_qr_code(doc.custom_upi_qr_link, scale=2) }}">

๐Ÿš€ Final Thoughts

This small feature can significantly improve your customer's payment experience and reduce payment delays. By leveraging Python and the flexibility of the Frappe framework, adding UPI QR functionality is both simple and powerful.

Ready To Unleash The Power of ERPNext?

We might just be the right partner you need.


No comments yet.

Add a comment
Ctrl+Enter to add comment