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

Auto read OTP using SMS User Consent API in Android

Some times ago Google Play restricts the use of high risk or sensitive permissions, including the SMS or Call Log permission groups. It means you have to justify why you are taking SMS or other high risk or sensitive permissions for your application .For this you may be required to complete the Permissions Declaration Form and receive approval from Google Play. Before this every app even who does not have any purpose for taking SMS or other high sensitive permissions in their app ,They was taking these permissions from users and this is a kind of security beach.  For solution of this problem Google introduce SMS User Consent API . The SMS User Consent API complements the SMS Retriever API by allowing an app to prompt the user to grant access to the content of a single SMS message. When a user gives consent, the app will then have access to the entire message body to automatically complete SMS verification. If your appeal for SMS permission is rejected by Google team you can use SMS Us

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library. Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser.  So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:- Lets Suppose you have a pdf file url something like   http://yourwebsite.com/files/mydocumentfile.pdf Step 1: So we you this pdf file url to embed in our webview .First of all you have to host yo