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...

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 ...

Working with Android 11 (Changes and Security Features)

 Hello everyone , I am here with new article which is hot topic nowadays "Android 11" .The stable version of Android.  Android 11 is the eleventh major release and 18th version of Android, the mobile operating system developed by the Open Handset Alliance led by Google. It was released on September 8, 2020.It is comes with many security features and other features as well . And it is now compulsory in play store  to upload new apps with API lavel 30 which is compatible with Android 11 and from November onwards old apps also have to update with API 30 .Some other guidelines you can check out from here . Play Store Guidelines So its clear that we have to update our apps with API level 30 .But Android 11 comes with some changes as well which we have to do in our projects. For example from Android developer site "Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. ". Scoped storage enforcement: Apps...