Technology9/28/2020

UPI Payment Integration Android

Sponsored Intelligence Dispatch

                                



In this tutorial we will learn how to integrate upi payment gateway in our app.




In gradle file add this dependency:-


// EasyUpiPayment Library

implementation 'com.shreyaspatil:EasyUpiPayment:3.0.0'


Note:- Minimum Sdk must not be below 19


In MainActivity.java class file add these lines of codes:-


package com.deftskill.upiintegration;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.View;

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;


import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements PaymentStatusListener {

    private EasyUpiPayment easyUpiPayment;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        findViewById(R.id.id_pay_using_upi_app).setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                startUpiPayment();

            }

        });

    }



    private void startUpiPayment() {

        String transactionId = "TID" + System.currentTimeMillis();

        String transactionRefId = "TREFID" + System.currentTimeMillis();


        // START PAYMENT INITIALIZATION

        EasyUpiPayment.Builder builder = new EasyUpiPayment.Builder(this)

                .with(PaymentApp.ALL)

                .setPayeeVpa("salil*****@okhdfcbank")

                .setPayeeName("Salil Jha")

                .setTransactionId(transactionId)

                .setTransactionRefId(transactionRefId)

                .setDescription("Demo")

                .setAmount("100.00");

        // END INITIALIZATION


        try {

            // Build instance

            easyUpiPayment = builder.build();


            // Register Listener for Events

            easyUpiPayment.setPaymentStatusListener(MainActivity.this);


            // Start payment / transaction

            easyUpiPayment.startPayment();

        } catch (Exception exception) {

            exception.printStackTrace();

            Toast.makeText(this, ""+exception.getMessage(), Toast.LENGTH_SHORT).show();

        }

    }


    @Override

    public void onTransactionCancelled() {

        Toast.makeText(this, "Cancelled by user", Toast.LENGTH_SHORT).show();

    }


    @Override

    public void onTransactionCompleted(TransactionDetails transactionDetails) {

        Toast.makeText(this, "Transaction Completed", Toast.LENGTH_SHORT).show();

    }

}


In activity_main.xml file add these lines of code:-

<?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:orientation="vertical"

    android:padding="16dp">


    <Button

        android:id="@+id/id_pay_using_upi_app"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Pay Using UPI App"

        android:layout_gravity="center"

        android:padding="16dp"

        android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>


</FrameLayout>


After adding these lines of code run this application and see the output.


Note:- Your device must have atleast one upi payment app. or real device to test this application.




Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.

Sponsored Infrastructure

Industrial Analysis Active