Image for post
Image for post

💳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.

Shreyas Patil
Jun 7, 2019 · 4 min read

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 :

  • At least one UPI app should installed on Device.

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.
Image for post
Image for post
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

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'
}

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

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.

Initializing EasyUpiPayment instance.

Calls and Descriptions :

  • with() : Mandatory and this call take Activity as a parameter and.
  • setPayeeVpa() : Mandatory and takes VPA address of payee for e.g. shreyas@upi
  • setTransactionId() : Mandatory and used in Merchant Payments generated by PSPs.
  • setTransactionRefId() : Mandatory Transaction reference ID. This could be order number, subscription number, Bill ID, booking ID, insurance renewal reference, etc. Needed for merchant transactions and dynamic URL generation. It's mandatory because some apps like PhonePe generates the error if this field is absent.
  • setDescription() : Mandatory and have to provide a valid small note or description about payment. for e.g. For Food
  • setAmount() : Mandatory and it takes the amount in String decimal format (xx.xx) to be paid. For e.g. 90.88 will pay Rs. 90.88.
  • setPayeeMerchantCode() : Payee Merchant code if present it should be passed.
  • build() : It will build and returns the EasyUpiPayment instance.

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.

  • PaymentApp.BHIM_UPI
  • PaymentApp.AMAZON_PAY
  • PaymentApp.GOOGLE_PAY
  • PaymentApp.PHONE_PE
  • PaymentApp.PAYTM

Example:

easyUpiPayment.setDefaultPaymentApp(PaymentApp.BHIM_UPI);

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

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

easyUpiPayment.startPayment();

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

easyUpiPayment.setPaymentStatusListener(this);

Description :

  • onTransactionCompleted() - This method is invoked when a transaction is completed. It may either SUCCESS, SUBMITTED or FAILED.

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

  • onTransactionSuccess() - Invoked when Payment is successful.
  • onTransactionFailed() - Invoked when Payment is unsuccessful/failed.
  • onTransactionCancelled() - Invoked when Payment cancelled (User pressed back button or any reason).
  • onAppNotFound() - Invoked when app specified with setDefaultPaymentApp() is not exists on device.

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 :

  • getTransactionId() - Returns Transaction ID
  • getResponseCode() - Returns UPI Response Code
  • getApprovalRefNo() - Returns UPI Approval Reference Number (beneficiary)
  • getStatus() - Returns Status of transaction. (Submitted/Success/Failure)
  • getTransactionRefId() - Returns Transaction reference ID passed in the input.

To remove listeners, you can invokedetachListener() 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 Facebook, Twitter, LinkedIn, GitHub, Personal Site.

GitHub Repository :

GitHub Page Documentation :

MindOrks

Our community publishes stories worth reading on Android…

Shreyas Patil

Written by

📱Android Developer 💻Organizer @KotlinMumbai, 👨‍💻SWE @ScaleReal. More info: http://shreyaspatil.dev

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Shreyas Patil

Written by

📱Android Developer 💻Organizer @KotlinMumbai, 👨‍💻SWE @ScaleReal. More info: http://shreyaspatil.dev

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store