Skip to content
Shreyas Patil's Blog

💳UPI Payment Integration in Android with EasyUpiPayment library📱💸

Cover image for 💳UPI Payment Integration in Android with EasyUpiPayment library📱💸

Hello everyone, in this article we will learn to implement UPI payment integration easily in any android app.

EasyUpiPayment is developed by me and available open-source on GitHub. You can find the repository at the end of the article. This library identifies installed UPI apps on the device and when a user chooses to pay with a specific app, it connects with an app via deep-linking and completes the payment procedure and returns back to the app with the transaction details.

The one and only one basic requirement of this library:


See Demo Output

Its implementation and flow is so easy and only of four steps as you can see below:

  1. Implement a caller to start transaction/payment.
  2. User will choose UPI app to pay with.
  3. User will complete the procedure of payment using UPI PIN.
  4. Transaction Details will get on the app and finished.

Demo of EasyUpiPayment Android Library Demo of EasyUpiPayment Android Library


💻 Getting Started

Let’s get started to the code!

Open Android Studio. Create a new project OR you can simply clone this repository: https://github.com/PatilShreyas/EasyUpiPayment-Android.git

Gradle Setup

In build.gradle of app module, include this below dependency to import the EasyUpiPayment library in the app.

dependencies {
    implementation 'com.shreyaspatil:EasyUpiPayment:2.0'
}

App Setup

In the Android app, make any activity where you want to implement payment integration. Here, I have created MainActivity.java.


Initializing EasyUpiPayment

You can see below code, these are minimum and mandatory calls to enable payment processing. If any of it is missed then an error will be generated. For example, consider parameters as follows:

Calls and Descriptions:


App-Specific Payment

If you want to pay only with a specific app like BHIM UPI, PhonePe, PayTm, etc. Then you can use a method setDefaultPaymentApp() of EasyUpiPayment.

Following ENUM can be passed to this method:

Example:

easyUpiPayment.setDefaultPaymentApp(PaymentApp.BHIM_UPI);

After this while payment, this app will be opened for a transaction.


Proceed to Payment

To start the payment, just call startPayment() method of EasyUpiPayment and after that transaction is started.

easyUpiPayment.startPayment();

Event Callback Listeners

To register for callback events, you will have to set PaymentStatusListener with EasyUpiPayment as below:

easyUpiPayment.setPaymentStatusListener(this);

Description:

NOTE: If onTransactionCompleted() is invoked it doesn’t means that payment is successful. It may fail but transaction is completed is the only purpose.


Getting Transaction Details

To get details about transactions, we have a callback method onTransactionCompleted() with a parameter of TransactionDetails. TransactionDetails instance includes details about the previously completed transaction.

To get details, below method of TransactionDetails are useful:


Remove Listener

To remove listeners, you can invoke detachListener() after the transaction is completed or you haven’t to do with payment callbacks.

easyUpiPayment.detachListener();

Thus, we have successfully integrated the UPI payment service in our Android app easily 😃.

Thank You! 😃

If you need any help get in touch with me on: https://shreyaspatil.dev

GitHub Repository: https://github.com/PatilShreyas/EasyUpiPayment-Android

GitHub Page Documentation: https://patilshreyas.github.io/EasyUpiPayment-Android/



Previous Post
Animated Material Dialog — Android 📱 😍🎨
Next Post
Firebase Database Pagination — Android 🔥