How to make UPI to UPI payment in android and get confirmation status, without any payment gateway integration.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.parthdarji812;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import com.wlc.utils.AppUtils;
import java.util.ArrayList;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;

public class HomeActivity extends AppCompatActivity  {

    int UPI_PAYMENT = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
// For application development contact to - parthdarji812@gmail.com
      //  Contact no : +919033278089
      // Alternet contact no : +918160151681
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //Getting the values from the EditTexts
                String amount = "1000";
                String note = "WLC Test";
                String name = "Parth Darji";
                String upiId = "9033278089@upi";
                payUsingUpi(amount, upiId, name, note);
            }
        },10000);
    }

    public void payUsingUpi( String amount, String upiId, String name, String note) {

        Uri uri = Uri.parse("upi://pay").buildUpon()
                .appendQueryParameter("pa", upiId)
                .appendQueryParameter("pn", name)
                .appendQueryParameter("tn", note)
                .appendQueryParameter("am", amount)
                .appendQueryParameter("cu", "INR")
                .build();

        Intent upiPayIntent = new Intent(Intent.ACTION_VIEW);
        upiPayIntent.setData(uri);

        // will always show a dialog to user to choose an app
        Intent chooser = Intent.createChooser(upiPayIntent, "Pay with");

        // check if intent resolves
        if (null != chooser.resolveActivity(getPackageManager())) {
            startActivityForResult(chooser, UPI_PAYMENT);
        } else {
            Toast.makeText(this, "No UPI app found, please install one to continue", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == UPI_PAYMENT && resultCode== RESULT_OK || resultCode == 11){
            if (data != null) {
                String trxt = data.getStringExtra("response");
                Log.e("UPI", "onActivityResult: $trxt");
                ArrayList<String> dataList = new ArrayList<String>();
                dataList.add(trxt);
                upiPaymentDataOperation(dataList);
            } else {
                Log.e("UPI", "onActivityResult: " + "Return data is null");
                ArrayList<String> dataList = new ArrayList<String>();
                dataList.add("nothing");
                upiPaymentDataOperation(dataList);
            }
        }else{
            Log.e("UPI", "onActivityResult: " + "Return data is null"); //when user simply back without payment
            ArrayList<String> dataList = new ArrayList<String>();
            dataList.add("nothing");
            upiPaymentDataOperation(dataList);
        }
    }


    private void upiPaymentDataOperation(ArrayList<String> data) {
        if (AppUtils.isNetworkConnected(this)) {
            String str = data.get(0);
            Log.e("UPIPAY", "upiPaymentDataOperation: " + str);
            String paymentCancel = "";
            if (str == null) str = "discard";
            String status = "";
            String approvalRefNo = "";
            String txnRef = "";
            String responseCode = "";
            String txnId = "";

            String[] response = str.split("&");
            for (String res : response) {
                String[] equalStr = res.split("=");
                if (equalStr.length >= 2) {
                    if (equalStr[0].equalsIgnoreCase("Status")) {
                        status = equalStr[1].toLowerCase();
                    }
                    if (equalStr[0].equalsIgnoreCase("ApprovalRefNo")) {
                        approvalRefNo = equalStr[1];
                    }
                    if (equalStr[0].equalsIgnoreCase("txnRef")) {
                        txnRef = equalStr[1];
                    } if (equalStr[0].equalsIgnoreCase("txnId")) {
                        txnId = equalStr[1];
                    }
                }else{
                    paymentCancel = "Payment cancelled by user.";
                }
            }

            if (status.equalsIgnoreCase("success")) {
                //Code to handle successful transaction here.
                Toast.makeText(this, "Transaction successful.", Toast.LENGTH_SHORT).show();
                 Log.e("UPI", "txnId: "+txnId);
                 Log.e("UPI", "responseStr: "+approvalRefNo);
                 Log.e("UPI", "txnRef: "+txnRef);
            } else if (paymentCancel.equalsIgnoreCase("Payment cancelled by user.")) {
                Toast.makeText(this, "Payment cancelled by user.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Transaction failed.Please try again", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "Internet connection is not available. Please check and try again", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onBackPressed() {
            super.onBackPressed();
    }


}

Comments