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!!
https://www.goiapk.in/
ReplyDelete