InstantPayUpi is a Android Library to implement UPI Payment integration easily in Android App. It supports almost all UPI Apps. It is simple, lightweight and used for quick setup.
- Payment will be only processed when payee is having a
valid merchant account of UPI
. Otherwise, payment won’t be successful. This won’t work for general UPI payee user.
- Add the following to the
settings.gradle
file.dependencyResolutionManagement { repositories { maven { url 'https://jitpack.io' } }}
- Add library dependency to the app level
build.gradle
file.dependencies { implementation 'com.github.mdrafi786:location-manager:$latest_stable_version' }
-
Firstly, you need declare variable of
InstantUpiPayment
.private lateinit var instantUpiPayment: InstantUpiPayment
-
Initialize the
InstantUpiPayment
with required parameter :Note: Here
this
is context of ActivtityinstantUpiPayment = InstantUpiPayment(this) { this.paymentApp = PaymentApp.ALL this.payeeVpa = "Merchant VPA or UPI id" this.payeeName = "Merchant Name" this.transactionId = "Merchant transactionId" this.transactionRefId = "Merchant transactionRefId" this.payeeMerchantCode = "Merchant code" this.description = "Note whatever you want to show" this.amount = "Amount" }
-
Fields Mandatory Description paymentApp
❌ Selects default payment app to pay with. Like PaymentApp.GOOGLE_PAY
. Default isPaymentApp.ALL
payeeVpa
✔ It takes VPA address of payee for e.g. himanshu@upi payeeName
✔ Name of payee payeeMerchantCode
✔ Payee Merchant code. This should be valid. transactionId
✔ This field is used in Merchant Payments generated by PSPs. transactionRefId
✔ 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. This is mandatory because absencse of this field generated error in apps like PhonePe description
✔ To provide a valid small note or description about payment. for e.g. For Food amount
✔ It takes the amount in String decimal format (xx.xx) to be paid. For e.g. 90.88 will pay Rs. 90.88. Note: Payee should be a valid merchant account holder otherwise transaction won’t be completed. If any of the above field is initiated using invalid values then Builder will throw
IllegalStateException
with appropriate message. Make sure you’re handling it.
- If you don’t want to give user a choice to pay using one of the multiple available UPI apps then you can set specific payment app for transaction.
- In such cases, payment will only be possible if that specific
app is installed
on user device.
-
PaymentApp.ALL
(Default) - Choice will be displayed. -
PaymentApp.AMAZON_PAY
-
PaymentApp.BHIM_UPI
-
PaymentApp.GOOGLE_PAY
-
PaymentApp.PAYTM
-
PaymentApp.PHONE_PE
-
For e.g. If you want to pay with BHIM UPI only, then code will look like…
val easyUpiPayment = InstantUpiPayment(this) { paymentApp = PaymentApp.BHIM_UPI // Other fields initialization }
Note : If defined app is not installed in user’s device then
AppNotFoundException
will be thrown.
- To initate payment firstly you have to declare
ActivityResultContracts
like this:var activityResult: ActivityResultLauncher<Intent> = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) { result -> instantUpiPayment.onActivityResult(result) }
- Once you have
InstantUpiPayment
instance built successfully. You are ready to initiate payment. Initiate payment usingstartPayment()
method.instantUpiPayment.startPayment(activityResult)
- Add a listener to get transaction callbacks
// Register Listener for Events instantUpiPayment.addPaymentStatusListener({ transactionDetails -> // Handle transection status here when (transactionDetails.transactionStatus) { TransactionStatus.SUCCESS -> { } TransactionStatus.FAILED -> { } TransactionStatus.SUBMITTED -> { } } }, { // Handle failure here like transection cancelled })
-
Once transaction is completed, in callback, you’ll get instance of
TransactionDetails
. -
Fields Description transactionId
Returns Transaction ID responseCode
Returns UPI Response Code approvalRefNo
Returns UPI Approval Reference Number (beneficiary) transactionStatus
Returns Status of transaction.(SUBMITTED/SUCCESS/FAILURE) transactionRefId
Returns Transaction reference ID passed in input amount
Returns Transaction amount
-
In
TransactionStatus
, you’ll getENUM
amongSUBMITTED
,SUCCESS
andFAILED
. Their meaning is: -
Status Description TransactionStatus.SUBMITTED
🟡 Transaction is in PENDING state. Money might get deducted from user’s account but not yet deposited in payee’s account. TransactionStatus.SUCCESS
🟢 Transaction is successful. TransactionStatus.FAILED
🔴 Transaction is failed.