How to create a dynamic QR Code for money transfer via UPI | In Android.

Prashant Verma
3 min readOct 11, 2020

--

Photo by Markus Winkler on Unsplash

In this blog, We are going to learn how to make a dynamic QR Code to transfer money through the UPI payment method. To build this application I will use constraint layout for the responsive application, material Design for good styling, data binding for the binding of the layout’s views, and zxing dependency for the QR Code.

QR code consists of black modules arranged in a square pattern on a white background. QR code is an image that contains information in the form of a pattern. and those information will be get from the custom URL. We encode the URL to QR code image.

What is Dynamic QR Code

In the Dynamic QR Code, The fields (param-value) of the URL will be changed from time to time. For example, you have taken a loan from a bank. Now your last date is today to pay the full payment. but when you will be doing late payment then penalty will also be added. So according to the time of payment, when you will scan the QR Code to pay the amount then the amount will get changed according to the current time. And this payable amount will be non-editable.

This is a functionality that we can do it with our QR Code. And these types of QR Code used in business and banking apps.

URL Link Specification and Parameters

UPI with URL spec must be as follows. All PSP applications must mandatorily implement listening to “UPI” links within their mobile applications for QR.

upi://pay?param-name=param-value&param-name=param-value&…

Where param-name can be any of the valid parameters. Now let's see the implementation of the QR Code.

Implementation of QR Code

First, you need to add this dependency in your app-level Gradle file.

implementation 'com.google.zxing:core:3.3.3'

In this xml code, we have to define the ImageView layout. in this ImageView layout, we will set the QR code.

activity_qr_code

In java class, we are having a URL that is containing the information of bank details and user details in the value param. In the below code I am setting param am=TOTAL_AMONT that means this amount will be non-editable when you will scan the QR code and you can set this (TOTAL_AMOUNT) field from the backend data. Then every time you will get a different amount to pay when the value will be changed from the backend. This is one feature of dynamic QR code.

Now our next step is to convert this information (containing by URL) to a pattern (QR Code). For this I have written a method textToImageIncode(URL). this method will return you a Bitmap. and this bitmap you can set on ImageView.

QRCodeActivity

NOTE: There are lots of params name that we can use in the URL. but I will mention some params name those are mandatory in the QR code. without these params name and values your QR Code will not work.

Mandatory params name in the URL:- pa(payee VPA), pn(payee name), am(not editable amount applied only for dynamic QR code), mode(initiation mode), sign(Base 64 encoded Digital signature needs to be passed in this tag), orgid(If the transaction is initiated by any PSP app then the respective orgID needs to be passed. For merchant initiated/created intent/QR ‘000000’ will be used).

For more params name and values you can refer to this doc.

--

--