Integrating UPI payments inside your Android app

Abdul Kadir
Apr 12 · 3 min read

Alright, In this post I’ll walk you guys through a toy app that will allow you to make payments using the Unified Payments Interface(UPI) that has been recently popularized in India.

For those of you who don’t know UPI (Unified Payments Interface), developed by the National Payments Corporation of India, makes making payments between different bank accounts a hassle-free process. There are many UPI client apps and you can link more than one bank account with your UPI app. You can read more about it here.

Now let’s start building the app.

This is the basic layout file for our Single Activity. You could create your own or just copy-past from here.

So we have a bunch of Edit Text Views that take in the person’s name, their UPI ID, the amount to be transferred and a Button that performs the transaction. Simple stuff!

Now let’s move to where the real action is happening.

MainActivity.kt

Our app only has a single screen and hence a single activity file. I’ll skip through all the house-keeping stuff like initializing views et al. Feel free to copy the code of the file as-is or directly jump to the part where we actually integrate UPI. I’ll explain what this particular code snippet does -

fun payUsingUpi(amount: String, upiId: String, name: String, note: String) {

val uri = Uri.parse("upi://pay").buildUpon()
.appendQueryParameter("pa", upiId)
.appendQueryParameter("pn", name)
.appendQueryParameter("tn", note)
.appendQueryParameter("am", amount)
.appendQueryParameter("cu", "INR")
.build()


val upiPayIntent = Intent(Intent.ACTION_VIEW)
upiPayIntent.data = uri

// will always show a dialog to user to choose an app
val chooser = Intent.createChooser(upiPayIntent, "Pay with")

// check if intent resolves
if (null != chooser.resolveActivity(packageManager)) {
startActivityForResult(chooser, UPI_PAYMENT)
} else {
Toast.makeText(this@UPIActivity, "No UPI app found, please install one to continue", Toast.LENGTH_SHORT).show()
}

}

So this function is responsible for adding UPI to your app. You need to call this function usually after you get all the information required for the transaction. In our app, it’s the four edit text fields. After filling those out, we capture the values in the onClickListnener of the ‘send’ button and pass in all the required information into this function. Let’s take a look at what this function does.

The code is straightforward. We are building a URI path which will allow us to make the transaction. This is achieved using the buildUpon() method on the ‘uri’ object and adding query parameters to it. The query parameters are key-value pairs which get added to the base URI path. As you can see, we append five query parameters in the following order — pa, pn, tn, am, cu. Each parameter means something in the URI, ‘pa’ is for the UPI id to which you want to transfer some amount, ‘pn’ is for the name, ‘tn’ is the note that is attached at the time of transaction, ‘am’ is the actual amount, and ‘cu’ is the currency. We then create an intent and set this ‘uri’ object as the intent’s data. Then we create another intent that always shows the user a dialog when the chooser intent is thrown. The dialog differs from device to device but it shows a list of apps which is capable of handling the URI path that was passed in the intent.

After creating both the intents, we ‘startActivityForResult()’ and select the UPI app which we want to use. After finishing the transaction, we return back to our own app and check whether the transaction was successful by overriding the Activity’s onActivityResult() method. These are all the steps required to integrate UPI payments into your app!

If you have done everything correctly, it should look a little something like this

App in action

I hope you enjoyed this post and if you would like to look at the Java version of the project, please head over to this video.

Abdul Kadir

Written by

A voracious reader; Master’s student. If I am not hacking away on my laptop you can find me flipping through a book or playing football.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade