Open In App

UPI Payment Integration in Android using Jetpack Compose

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

If you are selling any product or providing any service in your android application, then you should have integrated a feature in your android application where you can allow users to make payments through your application. In this article, we will take a look at the implementation of payment gateway integration in Android. In this article, we will be using the Easy UPI Payment gateway library for adding this feature to our android application using Jetpack Compose

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding dependency in build.gradle file

Navigate to app>Gradle Scripts>build.gradle file and add the below dependency in the dependencies section. 

implementation 'dev.shreyaspatil.EasyUpiPayment:EasyUpiPayment:3.0.3'

After adding this dependency simply sync your project to install it. 

Step 3: Adding a new color in the Color.kt file

Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it. 

Kotlin




package com.example.newcanaryproject.ui.theme
 
import androidx.compose.ui.graphics.Color
 
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
 
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 4: Adding internet permissions in AndroidManifest.xml file

Navigate to app > AndroidManifest.xml and add the below code to it. 

XML




<uses-permission android:name="android.permission.INTERNET" />


Step 5: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
 
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
import dev.shreyaspatil.easyupipayment.EasyUpiPayment
import dev.shreyaspatil.easyupipayment.listener.PaymentStatusListener
import dev.shreyaspatil.easyupipayment.model.PaymentApp
import dev.shreyaspatil.easyupipayment.model.TransactionDetails
import java.text.SimpleDateFormat
import java.util.*
 
class MainActivity : ComponentActivity(), PaymentStatusListener {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying background color for our application
                Surface(
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
 
                    // on the below line we are specifying the theme as the scaffold.
                    Scaffold(
 
                        // in scaffold we are specifying the top bar.
                        topBar = {
 
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
 
                                // along with that we are specifying title for our top bar.
                                title = {
 
                                    // in the top bar we are specifying tile as a text
                                    Text(
 
                                        // on below line we are specifying
                                        // text to display in top app bar.
                                        text = "UPI Payments in Android",
 
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
 
                                        // on below line we are specifying
                                        // text alignment.
                                        textAlign = TextAlign.Center,
 
                                        // on below line we are specifying
                                        // color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling pop window
                        // dialog method to display ui.
                        upiPayments(this)
                    }
                }
            }
        }
    }
 
    override fun onTransactionCancelled() {
        Toast.makeText(this, "Transaction cancelled by user..", Toast.LENGTH_SHORT).show()
    }
 
    override fun onTransactionCompleted(transactionDetails: TransactionDetails) {
        Toast.makeText(this, "Transaction completed by user..", Toast.LENGTH_SHORT).show()
    }
}
 
@Composable
fun upiPayments(mainActivity: MainActivity) {
    val ctx = LocalContext.current
    val activity = (LocalContext.current as? Activity)
 
    val amount = remember {
        mutableStateOf(TextFieldValue())
    }
    val upiId = remember {
        mutableStateOf(TextFieldValue())
    }
    val name = remember {
        mutableStateOf(TextFieldValue())
    }
    val description = remember {
        mutableStateOf(TextFieldValue())
    }
 
 
    // on the below line we are creating a column.
    Column(
        // on below line we are adding a modifier to it
        // and setting max size, max height and max width
        modifier = Modifier
            .fillMaxSize()
            .fillMaxHeight()
            .fillMaxWidth(),
 
        // on below line we are adding vertical
        // arrangement and horizontal alignment.
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
 
        // on below line we are creating a text
        Text(
            // on below line we are specifying text as
            // Session Management in Android.
            text = "UPI Payments in Android",
 
            // on below line we are specifying text color.
            color = greenColor,
 
            // on below line we are specifying font family
            fontFamily = FontFamily.Default,
 
            // on below line we are adding font weight
            // and alignment for our text
            fontWeight = FontWeight.Bold, textAlign = TextAlign.Center
        )
 
        // on below line we are adding spacer
        Spacer(modifier = Modifier.height(5.dp))
 
        // on below line we are creating a text field for our email.
        TextField(
            // on below line we are specifying
            // value for our email text field.
            value = amount.value,
 
            // on below line we are adding on
            // value change for text field.
            onValueChange = { amount.value = it },
 
            // on below line we are adding place holder
            // as text as "Enter your email"
            placeholder = { Text(text = "Enter amount to be paid") },
 
            // on below line we are adding modifier to it
            // and adding padding to it and filling max width
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
 
            // on below line we are adding text style
            // specifying color and font size to it.
            textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
             
            // on below line we are adding
            // single line to it.
            singleLine = true,
 
            )
 
        // on below line we are adding spacer
        Spacer(modifier = Modifier.height(5.dp))
 
        // on below line we are creating a text field for our email.
        TextField(
            // on below line we are specifying value
            // for our email text field.
            value = upiId.value,
 
            // on below line we are adding on value
            // change for text field.
            onValueChange = { upiId.value = it },
 
            // on below line we are adding place holder as
            // text as "Enter your email"
            placeholder = { Text(text = "Enter UPI Id") },
 
            // on below line we are adding modifier to it
            // and adding padding to it and filling max width
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
 
            // on below line we are adding text style
            // specifying color and font size to it.
            textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
             
            // on below line we are adding single line to it.
            singleLine = true,
        )
 
        // on below line we are adding spacer
        Spacer(modifier = Modifier.height(5.dp))
 
        // on below line we are creating a text field for our email.
        TextField(
            // on below line we are specifying value
            // for our email text field.
            value = name.value,
 
            // on below line we are adding on value
            // change for text field.
            onValueChange = { name.value = it },
 
            // on below line we are adding place holder
            // as text as "Enter your email"
            placeholder = { Text(text = "Enter name") },
 
            // on below line we are adding modifier to it
            // and adding padding to it and filling max width
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
 
            // on below line we are adding text style
            // specifying color and font size to it.
            textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
             
            // on below line we are adding
            // single line to it.
            singleLine = true,
        )
 
        // on below line we are adding spacer
        Spacer(modifier = Modifier.height(5.dp))
 
        // on below line we are creating a text field for our email.
        TextField(
            // on below line we are specifying value
            // for our email text field.
            value = description.value,
 
            // on below line we are adding on value change for text field.
            onValueChange = { description.value = it },
 
            // on below line we are adding place holder
            // as text as "Enter your email"
            placeholder = { Text(text = "Enter Description") },
 
            // on below line we are adding modifier to it
            // and adding padding to it and filling max width
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
 
            // on below line we are adding text style
            // specifying color and font size to it.
            textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
             
            // on below line we are adding single line to it.
            singleLine = true,
        )
 
        Spacer(modifier = Modifier.height(10.dp))
 
        Button(
            onClick = {
                // on below line we are getting date and then we
                // are setting this date as transaction id.
                val c: Date = Calendar.getInstance().getTime()
                val df = SimpleDateFormat("ddMMyyyyHHmmss", Locale.getDefault())
                val transcId: String = df.format(c)
 
                // on below line we are calling make
                // payment method to make payment.
                makePayment(
                    amount.value.text,
                    upiId.value.text,
                    name.value.text,
                    description.value.text,
                    transcId,
                    ctx,
                    activity!!,
                    mainActivity
                )
            },
            // on below line we are adding modifier to our button.
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            // on below line we are adding text for our button
            Text(text = "Make Payment", modifier = Modifier.padding(8.dp))
        }
    }
}
 
// on below line we are creating
// a make payment method to make payment.
private fun makePayment(
    amount: String,
    upi: String,
    name: String,
    desc: String,
    transcId: String, ctx: Context, activity: Activity, mainActivity: PaymentStatusListener
) {
    try {
        // START PAYMENT INITIALIZATION
        val easyUpiPayment = EasyUpiPayment(activity) {
            this.paymentApp = PaymentApp.ALL
            this.payeeVpa = upi
            this.payeeName = name
            this.transactionId = transcId
            this.transactionRefId = transcId
            this.payeeMerchantCode = transcId
            this.description = desc
            this.amount = amount
        }
        // END INITIALIZATION
 
        // Register Listener for Events
        easyUpiPayment.setPaymentStatusListener(mainActivity)
 
        // Start payment / transaction
        easyUpiPayment.startPayment()
    } catch (e: Exception) {
        // on below line we are handling exception.
        e.printStackTrace()
        Toast.makeText(ctx, e.message, Toast.LENGTH_SHORT).show()
    }
}


Now run your application to see the output of it. 

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads