Skip to main content

How to use UPI Deep Linking in Android

 In this Article we will discuss about the UPI Deep linking. By this Deep linking we can perform UPI Payments through intent .Basically it open the All UPI supported Application in our mobile then user choose their preference and initiate the Payment and through this we get the payment credentials like payment id ,transaction id ,etc. without any payment gateway. So lets start with what is UPI?

Unified Payments Interface(UPI) is an instant real-time payment system developed by National Payments Corporation of India facilitating inter-bank transactions. The interface is regulated by the Reserve Bank of India and works by instantly transferring funds between two bank accounts on a mobile platform. (from Wikipedia)

Coding part:

 package com.example.droidmedium;  
 import android.content.Intent;  
 import android.net.Uri;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.Toast;  
 public class MainActivity extends AppCompatActivity {  
   private String TAG = "MainActivity";  
   String payeeAddress = "xxxxxxxxx@upi";  // replace x with payee UPI Address  
   String payeeName = "Droid Medium";    // replace with payee Name  
   String transactionNote = "Test for UPI Deeplinking";  //can pass random txn Id  
   String amount = "1";     // Amount here   
   String currencyUnit = "INR";  
   String transid ="ABX1234";  // Random transaction Id  
   String txnId, responseCode, ApprovalRefNo, Status, txnRef;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     Button btnSubmit = (Button) findViewById(R.id.submit);  
     btnSubmit.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         Uri uri = Uri.parse("upi://pay?pa="+payeeAddress  
            +"&pn="+payeeName  
            +"&tn="+transactionNote  
            + "&mc=0000"   
            +"&tid=" + transid   
            + "&tr=" + transid  
            +"&am="+amount  
            +"&mam=" + null           
            +"&cu="+currencyUnit);  
         Log.d(TAG, "onClick: uri: "+uri);  
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
         startActivityForResult(intent,1);  
       }  
     });  
   }  
   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     Log.d(TAG, "onActivityResult: requestCode: "+requestCode);  
     Log.d(TAG, "onActivityResult: resultCode: "+resultCode);  
     //txnId=UPI20hjha787d8sd8ds7d8sd88sdbd7s8&responseCode=00&ApprovalRefNo=null&Status=SUCCESS&txnRef=undefined  
     //txnId=UPI608f070ee644467aa78d1ccf5c9ce39b&responseCode=ZM&ApprovalRefNo=null&Status=FAILURE&txnRef=undefined  
     if(data!=null) {  
       Log.d(TAG, "onActivityResult: data: " + data.getStringExtra("response"));  
       String res = data.getStringExtra("response");  
         String newstring = res.concat("&");  
         String newewe = newstring.replaceAll("&", " ");  
         //*****For Status Only **********************  
         int statusval = newstring.indexOf("Status");  
         String statusnew = newewe.substring(statusval, newewe.length());  
         Status = statusnew.substring(statusnew.indexOf("=") + 1, statusnew.indexOf(" "));  
         //**********************************************  
         //*****For txnId Only **********************  
         int txnIdval = newstring.indexOf("txnId");  
         String txnidnew = newewe.substring(txnIdval, newewe.length());  
         txnId = txnidnew.substring(txnidnew.indexOf("=") + 1, txnidnew.indexOf(" "));  
         //**********************************************  
         //*****For txnRef Only **********************  
         int txnRefval = newstring.indexOf("txnRef");  
         String txnrefnew = newewe.substring(txnRefval, newewe.length());  
         txnRef = txnrefnew.substring(txnrefnew.indexOf("=") + 1, txnrefnew.indexOf(" "));  
         //**********************************************  
         //*****For RespCode Only **********************  
         int RespCodeval = newstring.indexOf("responseCode");  
         String respcodenew = newewe.substring(RespCodeval, newewe.length());  
         responseCode = respcodenew.substring(respcodenew.indexOf("=") + 1,respcodenew.indexOf(" "));  
         //**********************************************  
      //   Note : Status can be SUCCESS/S/Success/FAILURE/F/SUBMITTED  
       String search = "SUCCESS";  
       if (res.toLowerCase().contains(search.toLowerCase())) {  
         Toast.makeText(this, "Payment Successful", Toast.LENGTH_SHORT).show();  
       } else {  
         Toast.makeText(this, "Payment Failed", Toast.LENGTH_SHORT).show();  
       }  
     }  
   }  
 }  

  That's all .so we can intigrate upi deep linking on our app without any payment gateway. Ref: UPI-DeepLink

  Thank you!!

Comments

Post a Comment

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Solution of Image not Loaded or Auto Suggestions not working in Android Studio

Sometimes we face issue when some images are not loaded or showing in Android Studio . It shows that image not loaded try to open it externally to fix format problem .It cause when some gradle files are corrupted. also if you try auto suggestions not working in xml layout files like  you can try Invalidate Caches and Restart in Android Studio's File Menu option but you will get no result . So here are some steps to follow to remove this type of problem: Step 1: Close your Android Studio Completely. Go to your User Folder - on Windows 7/8 this would be: [SYSDRIVE]:\Users[your username] (ex. C:\Users\DroidMedium\) there you find a folder with name .AndroidStudio3.5  *( i am using Android Studio 3.5.1 version your version may be different according to your Android Studio version) open this folder now you will find two folders Go to System folder  C:\Users\DroidMedium\.AndroidStudio3.5\System\ Step 2: Now under System Folder you will find Caches folder Delete the Folder Com...

How to Implement Item Click Interface in Android?

In Android development, creating interactive lists is essential to most apps. Often, these lists include items that users can click on to trigger actions such as opening a new screen or displaying more information. In this tutorial, we’ll walk through how to implement an item click interface in Android using Java and XML. By the end of this guide, you’ll know how to create a RecyclerView with an item click listener that responds to user taps. This approach is widely used in modern Android development because RecyclerView is both flexible and efficient. Step 1: Set Up Your Android Project First, create a new Android project in Android Studio. Make sure to choose Java as the programming language. Step 2: Add Dependencies Make sure that you have the required dependencies for RecyclerView in your build.gradle file. If not, add them like this: dependencies {     implementation 'androidx.recyclerview:recyclerview:1.2.1' } Sync the project after adding the dependency. Step 3: Define ...