In this tutorial, we’ll explore how to initiate a UPI payment from an Android application using Java and the EasyUpiPayment library.
You’ll learn how to:
Add the EasyUpiPayment dependency to an Android project.
Create a simple payment button.
Configure the payee, amount, and transaction reference.
Launch an installed UPI application.
Handle the response returned by the payment flow.
Understand the security checks required before accepting a payment as complete.
Important: This is an educational example of initiating a UPI payment. A response returned to an Android app is not independently verified proof that money was received.
1. What Is EasyUpiPayment?
EasyUpiPayment is an open-source Android library designed to simplify intent-based UPI payment initiation.
It provides a convenient API for configuring payment details and launching compatible UPI applications.
However, the original EasyUpiPayment Android repository was archived in November 2023. Before adopting it in a new application, review its compatibility with your Android Gradle Plugin, target SDK, and current UPI app behavior.
For a commercial application, evaluate a maintained payment provider or an acquiring bank's integration before choosing this library.
2. Requirements
Before starting, prepare the following:
Android Studio and a compatible Android SDK.
An Android project using Java.
A compatible EasyUpiPayment dependency.
A physical Android device with a supported UPI app for testing.
A valid recipient UPI ID and permission to use it.
The original library documentation specifies Android compatibility requirements. Confirm the minimum SDK supported by the version you select rather than assuming that every current Android configuration will work.
3. Add the Gradle Dependency
Open your app module's build.gradle file and add the published dependency:
dependencies {
implementation "dev.shreyaspatil.EasyUpiPayment:EasyUpiPayment:3.0.3"
}If your project uses build.gradle.kts, use Kotlin DSL syntax:
dependencies {
implementation("dev.shreyaspatil.EasyUpiPayment:EasyUpiPayment:3.0.3")
}Sync the project and resolve any compatibility issues before proceeding.
You can check the published artifact on Maven Central.
Compatibility note: The library is archived, so the dependency being published does not guarantee compatibility with every current Android toolchain. Confirm the API and package names against the version you actually resolve.
4. Create the Payment Screen
Create or update activity_main.xml inside your app's res/layout directory.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/id_pay_using_upi_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Pay Using UPI App" />
</FrameLayout>This layout contains a single button that starts the UPI payment flow.
In a real application, display the order description and amount before the customer proceeds. The amount should come from a trusted order record, not from an editable value supplied by the client.
5. Implement UPI Payment Initiation in Java
Create or update MainActivity.java.
The following example demonstrates the original library's builder-style API. Verify the imports and method signatures against the version resolved by Gradle before using the code in your project.
package com.example.upiintegration;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import com.shreyaspatil.easyupipayment.EasyUpiPayment;
import com.shreyaspatil.easyupipayment.listener.PaymentStatusListener;
import com.shreyaspatil.easyupipayment.model.PaymentApp;
import com.shreyaspatil.easyupipayment.model.TransactionDetails;
public class MainActivity extends AppCompatActivity
implements PaymentStatusListener {
private EasyUpiPayment easyUpiPayment;
private Button payButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
payButton = findViewById(
R.id.id_pay_using_upi_app
);
payButton.setOnClickListener(view ->
startUpiPayment()
);
}
private void startUpiPayment() {
// Demo values only.
// Use an order reference generated by your backend
// for a real application.
String transactionId =
"TID" + System.currentTimeMillis();
String transactionRefId =
"TREF" + System.currentTimeMillis();
EasyUpiPayment.Builder builder =
new EasyUpiPayment.Builder(this)
.with(PaymentApp.ALL)
.setPayeeVpa("your-business@upi")
.setPayeeName("Your Business")
.setTransactionId(transactionId)
.setTransactionRefId(transactionRefId)
.setDescription("Demo Payment")
.setAmount("100.00");
try {
easyUpiPayment = builder.build();
easyUpiPayment.setPaymentStatusListener(this);
easyUpiPayment.startPayment();
} catch (Exception e) {
Toast.makeText(
this,
"Unable to start UPI payment",
Toast.LENGTH_LONG
).show();
}
}
@Override
public void onTransactionCompleted(
TransactionDetails transactionDetails
) {
// Inspect the returned details using the API
// available in your resolved library version.
//
// Do not mark an order as paid based only on
// this callback. Verify the payment independently.
Toast.makeText(
this,
"UPI response received. Verifying payment...",
Toast.LENGTH_LONG
).show();
}
@Override
public void onTransactionCancelled() {
Toast.makeText(
this,
"Payment was cancelled or not completed",
Toast.LENGTH_LONG
).show();
}
}Replace the demo values
Before testing, replace:
your-business@upiwith a valid recipient UPI ID.Your Businesswith the actual payee name.The demo description and amount with appropriate test values.
The example uses a generated transaction reference to illustrate the flow. For an actual order, create and store a unique order or payment reference on your backend before launching the UPI app.
API compatibility: The sample uses the package names from the older library API. If the 3.0.3 artifact resolves but these imports or signatures do not compile, consult the library's archived source and examples. Do not assume the old code is a verified, drop-in build for a current Android project.
6. How the UPI Payment Flow Works
The general flow is:
The customer selects an order and taps the payment button.
The app prepares the payee and transaction details.
Android launches a compatible UPI app.
The customer reviews the details and authorizes the payment in that app.
The UPI app returns a response to the initiating application.
The merchant verifies the actual payment status through the appropriate trusted payment channel.
The application updates the order only after verification.
The Android callback is useful for updating the interface, but it should not be treated as the final source of truth for a financial transaction.
7. Important UPI Parameters
| Parameter | Purpose |
|---|---|
| Payee VPA | Recipient's UPI address |
| Payee name | Name displayed for the recipient |
| Transaction ID | Identifier used by the payment flow |
| Transaction reference ID | Reference associated with the transaction |
| Description | Short explanation of the payment |
| Amount | Amount requested from the customer |
Use unique references for individual payment attempts and retain a reliable mapping between your internal order ID and the payment attempt.
8. Testing the Integration
Test on a physical Android device with a compatible UPI application installed.
Check the following scenarios:
A compatible UPI app opens with the expected details.
No compatible UPI app is installed.
The customer cancels the payment.
The payment app returns an error or incomplete response.
The network is unavailable when your app checks the order status.
The customer retries after an uncertain result.
The same order receives duplicate callbacks or status updates.
Do not interpret an app-launch failure, cancellation callback, or missing response as proof that no money moved. If the result is uncertain, reconcile the payment status before allowing another attempt or changing the order state.
9. Production Security: Verify Before Confirming an Order
This is the most important part of payment integration.
A message such as “Transaction Successful” shown by the Android app is not sufficient evidence to fulfill an order. Client-side code can be modified, and returned transaction details may be incomplete or unreliable.
For a production system:
Create the order or payment attempt on your backend.
Store the expected amount, currency, order ID, payee, and transaction reference.
Initiate the payment using the appropriate integration.
Verify the transaction using the acquiring bank, payment provider, or another authoritative mechanism available for your integration.
Match the verified amount and transaction reference to the expected order.
Make order updates idempotent so that duplicate callbacks or notifications cannot fulfill an order twice.
Reconcile pending and disputed transactions through your provider's status-checking process.
Use authenticated server-to-server notifications or webhooks when your payment provider supports them, and verify their authenticity according to that provider's documentation.
A direct UPI intent flow does not automatically provide a payment gateway, a server-side verification API, or a webhook. You must confirm what verification facilities are available through your bank or payment partner.
For general UPI information, consult the NPCI UPI FAQs. For a managed payment integration, review your provider's official Android and server-side documentation.
10. Direct UPI Intent vs. Payment Gateway
These approaches are not interchangeable.
Direct UPI intent: Your app initiates a payment in a compatible UPI app. You must arrange a reliable way to confirm receipt before fulfilling a commercial order.
Payment gateway or acquiring-bank integration: A provider supplies an integration and the relevant payment-status mechanisms. You still need to validate payment details and follow the provider's verification requirements.
For e-commerce, paid subscriptions, donations, or other transactions where fulfillment depends on confirmed payment, choose the integration based on the verification and reconciliation capabilities you actually have.
11. Common Problems and Solutions
No UPI application found
Check that a compatible UPI app is installed and enabled on the test device. Device and Android-version differences can affect app discovery and intent handling.
Dependency or import errors
Confirm the resolved dependency coordinate, version, package names, and method signatures. The original EasyUpiPayment repository is archived, so an older tutorial may not compile unchanged with a current toolchain.
Payment response is missing
The user may have switched apps, cancelled, encountered a network issue, or completed a payment whose response was not returned as expected. Check the authoritative payment status before deciding how to proceed.
Order marked as paid too early
Do not use a button click, activity result, or client-side success message as proof of payment. Confirm the transaction through a trusted provider or bank mechanism.
Duplicate payment attempts
Use unique payment-attempt references and idempotent backend updates. Reconcile uncertain attempts before asking the customer to pay again.
12. Frequently Asked Questions
Does direct UPI integration require a payment gateway?
Not necessarily for initiating a UPI intent. However, commercial applications still need a dependable way to verify payment receipt and reconcile transactions. An acquiring bank or payment provider may be necessary to provide that capability.
Can I test UPI payments on an emulator?
A physical Android device with an installed, compatible UPI app is generally the more reliable test environment. Emulator behavior can vary.
Does EasyUpiPayment guarantee a successful payment?
No. The library helps initiate the payment flow and receive a response. It does not, by itself, establish that funds were received and matched to your order.
Is EasyUpiPayment recommended for a new production app?
Evaluate it carefully because its original repository was archived in 2023. For a new commercial application, prefer an integration with documented compatibility, ongoing maintenance, and suitable server-side payment verification.
Should the app store the user's UPI PIN?
No. The customer authorizes the payment inside the UPI application. Your app must never ask for or store the customer's UPI PIN.
Conclusion
UPI intent integration can help Android applications initiate payments through compatible UPI apps. EasyUpiPayment offers a convenient example of this approach, but its archived status and compatibility limitations should be considered before adopting it.
For a production payment flow, the key requirement is not simply opening a UPI app or receiving a callback. It is reliably verifying the transaction, matching it to the correct order, and handling retries and uncertain results safely.
No comments:
Post a Comment