How to Generate a QR Code that Specifies the UPI ID and Amount?


BarcodeBC > Articles > How to Generate a QR Code in C# that Specifies the UPI ID and Amount?


Here's an example code in C# that uses BarcodeBC.com .NET Barcode Generator Library to generate a QR Code that specifies the UPI ID and amount. You can also confirm whether the user has paid or not after scanning the QR Code. You can firstly download a free trial here.


How to Generate a QR Code in C# that Specifies the UPI ID and Amount?

Please firstly add your project reference to the BC.NetBarcodeGenerator.All library dll. And then refer to the following C# example code. It uses BarcodeBC.com .NET Barcode Generator to generate a QR Code that specifies the UPI ID and amount. Meanwhile, we also provide online guide for using BC.NetBarcodeGenerator.All library.


using BC.AspNetBarcodeGeneratorTrial.All;
using System.Drawing;

// Create a new QR Code object
Qrcode barcode = new Qrcode();

// Set the data to be encoded in the QR Code
string upiId = "your_upi_id_here";
decimal amount = 10.00m;
string qrData = $"upi://{upiId}?amount={amount.ToString("0.00")}";
barcode.SetData = qrData;

// Set the QR Code encoding mode
barcode.SetEncodingMode = QrcodeEncodingMode.Automatic

// Set the size of the QR Code image
barcode.AutoSize = false;
barcode.SetModuleSize = 3;
barcode.SetWidth = 80;
barcode.SetHeight = 80;

// Generate the QR Code image and save it to a file
barcode.GenerateBarcode("qrcode.png");

This code first creates a Qrcode object and sets the data to be encoded in the QR Code using the SetData property. The UPI ID and amount are combined into a single string using the string.Format() method.

The encoding mode of the QR Code is set to Automatic which allows the QR Code generator to automatically choose the best encoding mode for the given data.

The size of the QR Code image is set using the SetModuleSize, SetWidth, and SetHeight properties of the Qrcode object. In this example, the size is set to 80 pixels.

Finally, the QR Code image is generated and saved to a file using the GenerateBarcode method.



How to Confirm Whether the User Has Paid or Not After Scanning the QR Code?

To confirm whether the user has paid or not after scanning the QR Code, you need to integrate a payment gateway or payment processor into your application. These payment gateways or processors provide APIs that allow you to verify the payment status. Here are the general steps to verify payment status automatically.

1. Integrate a payment gateway or processor into your application and initiate a payment transaction with the user's payment details (such as the UPI id and amount).

2. Once the user has scanned the QR Code and made the payment, the payment gateway or processor will send a payment confirmation to your application.

3. Your application can use the payment gateway or processor's API to verify the payment status and retrieve the payment details.

4. If the payment is successful, you can update your application's database or record system to indicate that the payment has been received.

5. If the payment is unsuccessful, you can display an error message to the user and prompt them to try again or use an alternative payment method.

Note that the specific implementation of the payment gateway or processor API integration will vary depending on the chosen payment gateway or processor.

The following is an example code to confirm the payment status automatically using UPI.


// Assuming the payment token and amount have been received and stored in variables
string upiId = "example@upi"; // Replace with the actual UPI ID
decimal amount = 10.50m; // Replace with the actual amount

// Generate the QR Code using BarcodeBC.com .NET Barcode Generator
Qrcode barcode = new Qrcode();
string qrData = $"upi://pay?pa={upiId}&am={amount}"; // QR data in UPI format
barcode.SetData(qrData);

// Save the generated QR Code to a file or display it on screen for scanning by the user

// Use a payment gateway API to check the payment status
string paymentId = ""; // Payment ID returned by the payment gateway
string gatewayResponse = CheckPaymentStatus(paymentId); // Function to check payment status using the payment gateway API

if (gatewayResponse == "success")
{
    // Payment is successful
    // Do something here, such as updating a database or displaying a success message to the user
}
else
{
    // Payment failed or pending
    // Do something here, such as displaying an error message to the user or retrying the payment
}

How to Save the QR Code to Display It on Screen for Scanning by the User?

To save a generated QR Code and display it on screen for scanning by the user, you can follow these steps:

1. Generate the QR Code using a barcode generator library or service, such as BarcodeBC.com .NET Barcode Generator. Save the generated image to a file on your server.

2. Retrieve the image file and serve it to the user's web browser. You can do this by creating a URL endpoint that returns the image file as a response. For example, you can use ASP.NET MVC to create a controller action that retrieves the image file from disk and returns it as a FileContentResult.

3. In your HTML code, use an img tag with the URL of the endpoint you created as the src attribute. For example,


<img src="/qrcode/getimage" alt="QR Code">

This will display the QR Code image on the page, which the user can then scan using their device's camera.

Here's an example code for generating and displaying a QR Code using BarcodeBC.com .NET Barcode Generator and ASP.NET MVC


// Generate the QR Code and save it to a file
Qrcode barcode = new Qrcode();
barcode.SetData = "upi://pay?pa=example@upi&pn=Example%20Name&am=100";
barcode.GenerateBarcode("qrcode.png");

// Create a controller action that returns the QR Code image file
public FileContentResult GetImage()
{
    string path = Server.MapPath("~/qrcode.png");
    byte[] imageData = System.IO.File.ReadAllBytes(path);
    return new FileContentResult(imageData, "image/png");
}

Note that in this example, the QR Code contains a UPI payment link with a UPI ID of "example@upi" and an amount of 100. You'll need to replace this with your own UPI ID and amount as necessary. Also, be sure to handle errors and security issues properly, such as ensuring that the QR Code is not tampered with and that payments are properly verified before marking them as paid.

BTW, we also provide .NET libraries for .NET PDF QR Code generation and .NET Core QR Code generation.