Hello Guys, I have created and publish this Android library to easily implement UPI Payment Integration in Android app without any external SDK.
Requirement for this library: UPI Deep-linking supported application are required to be already installed in user mobile before using this library.
1. Enter UPA, NAME, AMOUNT and Description | 2. Click on Pay and Select UPI App | 3. Response received in previous activity. |
---|
You can clone this repository and import this project in Android Studio.
In your build.gradle
file of app module, add below dependency to import this library
dependencies {
implementation 'com.gpfreetech:IndiUpi:1.1'
}
In Android app, Create activity and implement basic payment integration step where you want to add. In demo app I have already created SampleActivity.java
See below code, these are parameters to start payment processing.
IndiUpi indiUpi = new IndiUpi.Builder()
.with(this)
.setPayeeVpa("payee@upi")
.setAmount("AMOUNT_IN_DECIMAL")
.setPayeeName("payee name")
.setDescription("DESCRIPTION")
.setTransactionId("UNIQUE_TRANSACTION_ID")
.setTransactionRefId("UNIQUE_TRANSACTION_REF_ID")
.setUrl("HTTP_OR_HTTPS", "WWW.EXAMPLE.COM", "API.php")
//internal parameter automatically add in URL same as above UPI request
.build();
Parameter Details:
Method | Mandatory | Description |
---|---|---|
with() | YES | Pass activity instance where Payment process is to be implemented |
setPayeeVpa() | YES | here VPA address of payee for like gpfreetech@upi |
setTransactionId() | YES | This field is used in Merchant Payments generated by PSPs. This is Unique key. and for demo purpose we have already generae random id using method in Validator.java file. You can check this |
setTransactionRefId() | YES | This is mandatory. Transaction reference ID. i.e. order number, subscription number, Bill ID, booking ID etc. this is very important for backend purpose and dynamic URL generation. You can validate your payemt using txnRefId |
setDescription() | YES | To provide a description about payment. for e.g. Home Rent |
setAmount() | YES | It takes the amount in String decimal format (xx.xx) For e.g. 05.10 will pay Rs. 05.11. |
setPayeeMerchantCode() | No | Payee Merchant code if present it should be passed. |
setUrl() | No | Here you can enter your scheme, authority and appendPath. ex .setUrl("http", "www.sample.com", "test.php") |
build() | YES |
It will build and returns the IndiUpisingle instance. |
To start the payment, just call pay()
method using current instance and after that transaction is started.
indiUpi.pay();
Or
indiUpi.pay("Payment Using"); // here your choice dialog title
To register for callback events, you will have to set PaymentStatusListener
with instance as below.
indiUpi.setPaymentStatusListener(this);
Description :
onTransactionCompleted()
- This method is invoked when transaction is completed. It may eitherSUCCESS
,SUBMITTED
orFAILED
.
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()
- call when Payment is successful.onTransactionSubmitted()
- call when Payment is partially done/In waiting/Submitted/Pending.onTransactionFailed()
- call when Payment is unsuccessful/failed.onTransactionCancelled()
- call when Payment cancelled (pressed back button or any reason).
@Override
public void onTransactionCompleted(TransactionResponse transactionResponse) {
// Transaction Completed
Log.d("TransactionResponse", transactionResponse.toString());
// normal
txtStatus.setText(transactionResponse.toString());
//or as HTML Text in format
txtStatus.setText(Html.fromHtml(transactionResponse.toHTMLString()));
}
@Override
public void onTransactionSuccess(TransactionResponse transactionResponse) {
// Payment Success
Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_success);
}
@Override
public void onTransactionSubmitted() {
// Payment Pending
Toast.makeText(this, "Payment Pending Or Submitted", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_success);
}
@Override
public void onTransactionFailed() {
// Payment Failed
Toast.makeText(this, "Payment Failed", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_failed);
}
@Override
public void onTransactionCancelled() {
// Payment Process Cancelled by User
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_failed);
}
To get transactions details, used onTransactionCompleted()
. Which provice parameter of TransactionResponse
. TransactionResponse instance includes details about completed transaction.
TransactionResponse
contains below information :
Method | Description |
---|---|
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 input |
Note : BHIM supports UPI deep-linking but does not return the transaction reference number, instead returns the txnref with the value “undefined” Same few UPI Deep-linking supported app does not return the transaction reference number.
Currently we are working on it, There are lot of improvements are still needed and in progress. Like Url parameter handling Minimum amount parameter handling
Suggestions are welcome.
If you have any issues or ideas about implementations then just raise issue and we are open for Pull Requests. You All Welcome.