💳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 :
- At least one UPI app should installed on Device.
See Demo Output :
Its implementation and flow is so easy and only of four steps as you can see below,
- Implement a caller to start transaction/payment.
- User will choose UPI app to pay with.
- User will complete the procedure of payment using UPI PIN.
- Transaction Details will get on the app and finished.
💻 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
build.gradle of app module, include this below dependency to import the EasyUpiPayment library in the app.
In the Android app, make any activity where you want to implement payment integration. Here, I have created
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 :
with(): Mandatory and this call take
Activityas a parameter and.
setPayeeVpa(): Mandatory and takes VPA address of payee for e.g.
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
If you want to pay only with a specific app like BHIM UPI, PhonePe, PayTm, etc. Then you can use a method
Following ENUM can be passed to this method.
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.
Event Callback Listeners
To register for callback events, you will have to set
EasyUpiPayment as below.
onTransactionCompleted()- This method is invoked when a transaction is completed. It may either
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 invoke
detachListener() after the transaction is completed or you haven’t to do with payment callbacks.
Thus, we have successfully integrated the UPI payment service in our Android app easily 😃.
Thank You! 😄
GitHub Repository :
📱Android Library to implement UPI Payment integration easily in Android App 💳💸 - PatilShreyas/EasyUpiPayment-Android
GitHub Page Documentation :