Create UPI Payment QR Codes

Introduction

This small Python script creates UPI payment QR codes that open a payment app when scanned. It accepts a UPI ID from the user and builds three UPI payment URLs for popular apps then generates and displays QR images for each URL. The script is simple and useful for quickly sharing payment links as QR codes.

Import library

  • import qrcode loads the qrcode library used to generate QR image objects.

Get user input

  • upi_id = input(“Enter your UPI ID: “) prompts the user to type the UPI ID that will be encoded inside each QR code.

UPI pattern reminder

  • The commented line shows the common UPI payment URL format where pa is payee address and other fields can include name amount currency and a note.

Build app specific UPI URLs

  • The script constructs three UPI URLs using Python formatted strings. Each URL embeds the user supplied UPI ID and uses a placeholder payee name and merchant code.
  • phonepay_url paytm_url and google_pay_url are separate strings but currently use the same fields and values which make them functionally identical apart from the variable name.

Generate QR images

  • qrcode.make(…) converts each URL string into a QR image object.

Show QR images

  • Calling .show() on each image opens the default image viewer to display the QR. On many systems this opens a temporary image window.

How to run this script

Install dependencies

  • Run pip install qrcode pillow in your Python environment.

Run the script

  • Execute python main.py in your terminal.
  • Enter a valid UPI ID when prompted. The script will open three image windows with the generated QR codes.

Practical notes and cautions

  • QR URL fields
    The UPI URL template supports additional parameters like pn payee name am amount and tn transaction note. This script currently includes only payee name and a sample merchant code. For production use include appropriate parameters and validate them first.
  • App behaviour and compatibility
    Scanning a UPI URL depends on the scanning app and the mobile operating system. Some apps may ignore unknown fields or require specific parameter sets. Test QR codes with the apps you expect users to use.
  • Security and privacy
    Do not embed sensitive personal data in QR codes. Validate the UPI ID before sharing and confirm the payee name is correct. Anyone scanning the QR can initiate a payment request.
  • Image display differences
    The .show() method opens images via the default image viewer. If you need to save these QR codes to disk or embed them on a website use .save(“filename.png”) instead. This script intentionally does not save files.

Simple improvements you can mention to readers

  • Use URL encoding for special characters in payee names or notes.
  • Add an amount am parameter to prefill payment amounts. Explain how that affects user experience.
  • Save QR images to disk if readers want to print or share them.
  • Validate the UPI ID format before generating QR codes.

Conclusion

This script is a handy starting point to generate UPI QR codes quickly using Python. It demonstrates how to build UPI payment URLs and convert them into scannable QR images with minimal code. It is ideal for demo purposes or as the basis for a small utility app.

Share this post:
Facebook
Twitter
LinkedIn

Web Development Projects

Interested in more? Check out my Machine Learning projects as well.

Machine Learning Projects

Interested in more? Check out my Machine Learning projects as well.

Python Projects

Interested in more? Check out my Python projects as well.