/UPI_Payment

Simple Android with UPI Payment gateway.

Primary LanguageJavaApache License 2.0Apache-2.0

Easy UPI Payment - Android Library 📱💳

Introduction

Hello Everyone, I have developed this Android library to easily implement UPI Payment Integration in Android app.
UPI apps are required to be installed already before using this library because, internally this API calls UPI apps for payment processing. Before using it, make sure that your device is having atleast one UPI app installed. Otherwise it will unable to process the payments.
This API is in beta, there are lot of improvements are still needed.

Demo

1. Start 2. Select UPI App 3. Complete Payment 4. Finish

Implementation

You can clone this repository and just import this project in Android Studio. Sample app is in /app directory.

Gradle

In your build.gradle file of app module, add below dependency to import this library

    dependencies {
      implementation 'com.shreyaspatil:EasyUpiPayment:1.1'
    }

Setting Up Activity

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

Initializing EasyUpiPayment :

You can see below code, these are minimum and mandatory calls to enable payment processing. If any of it is missed then error will generated.

        final EasyUpiPayment easyUpiPayment = new EasyUpiPayment.Builder()
                .with(this)
                .setPayeeVpa("EXAMPLE@VPA")
                .setPayeeName("PAYEE_NAME")
                .setTransactionId("UNIQUE_TRANSACTION_ID")
                .setTransactionRefId("UNIQUE_TRANSACTION_REF_ID")
                .setDescription("DESCRIPTION_OR_SMALL_NOT")
                .setAmount("AMOUNT_IN_DECIMAL_XX.XX")
                .build();

Calls and Descriptions :

Method Mandatory Description
with() ✔️ This call takes Activity as a parameter where Payment is to be implemented
setPayeeVpa() ✔️ It takes VPA address of payee for e.g. shreyas@upi
setTransactionId() ✔️ This field is used in Merchant Payments generated by PSPs.
setTransactionRefId() ✔️ 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
setDescription() ✔️ To provide a valid small note or description about payment. for e.g.
For Food
setAmount() ✔️ 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.

Proceed to Payment

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

      easyUpiPayment.startPayment();

Event Callback Listeners

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 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.
  • onTransactionSubmitted() - Invoked when Payment is partially done/In waiting/Submitted/Pending.
  • onTransactionFailed() - Invoked when Payment is unsuccessful/failed.
  • onTransactionCancelled() - Invoked when Payment cancelled (User pressed back button or any reason).
    @Override
    public void onTransactionCompleted(TransactionDetails transactionDetails) {
        // Transaction Completed
        Log.d("TransactionDetails", transactionDetails.toString());
        statusView.setText(transactionDetails.toString());
    }

    @Override
    public void onTransactionSuccess() {
        // Payment Success
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        imageView.setImageResource(R.drawable.ic_success);
    }

    @Override
    public void onTransactionSubmitted() {
        // Payment Pending
        Toast.makeText(this, "Pending | Submitted", Toast.LENGTH_SHORT).show();
        imageView.setImageResource(R.drawable.ic_success);
    }

    @Override
    public void onTransactionFailed() {
        // Payment Failed
        Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
        imageView.setImageResource(R.drawable.ic_failed);
    }

    @Override
    public void onTransactionCancelled() {
        // Payment Cancelled by User
        Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
        imageView.setImageResource(R.drawable.ic_failed);
    }

Removing Listener

To remove listeners, you can invoke detachListener() after the transaction is completed or you haven’t to do with payment callbacks.

easyUpiPayment.detachListener();

Getting Transaction Details

To get details about transactions, we have callback method onTransactionCompleted() with parameter of TransactionDetails. TransactionDetails instance includes details about previously completed transaction.
To get details, below method of TransactionDetails are useful :

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

Hurrah.... We have successfully implemented UPI integration in our Android app. Thank You!

Contribute

We can collaboratively make it happen. So if you have any issues, new ideas about implementations then just raise issue and we are open for Pull Requests. Improve and make it happen. See Contributing Guidelines.

License

Project is published under the Apache 2.0 license. Feel free to clone and modify repo as you want, but don't forget to add reference to authors :)