├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── Readme.txt ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── chirag │ │ └── upi_payments │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── chirag │ │ │ └── upi_payments │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── chirag │ └── upi_payments │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | Link to the youtube tutorial : https://www.youtube.com/watch?v=gIr_G6MG0pI 2 | Link to the youtube channel : https://www.youtube.com/channel/UCLIFAN-83cSg5EsKJApAscQ 3 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.example.chirag.upi_payments" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/chirag/upi_payments/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.chirag.upi_payments; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.chirag.upi_payments", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/chirag/upi_payments/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.chirag.upi_payments; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.ConnectivityManager; 6 | import android.net.NetworkInfo; 7 | import android.net.Uri; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.os.Bundle; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.Button; 13 | import android.widget.EditText; 14 | import android.widget.Toast; 15 | 16 | import java.util.ArrayList; 17 | 18 | public class MainActivity extends AppCompatActivity { 19 | 20 | EditText amountEt, noteEt, nameEt, upiIdEt; 21 | Button send; 22 | 23 | final int UPI_PAYMENT = 0; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | 30 | initializeViews(); 31 | 32 | send.setOnClickListener(new View.OnClickListener() { 33 | @Override 34 | public void onClick(View view) { 35 | //Getting the values from the EditTexts 36 | String amount = amountEt.getText().toString(); 37 | String note = noteEt.getText().toString(); 38 | String name = nameEt.getText().toString(); 39 | String upiId = upiIdEt.getText().toString(); 40 | payUsingUpi(amount, upiId, name, note); 41 | } 42 | }); 43 | } 44 | 45 | void initializeViews() { 46 | send = findViewById(R.id.send); 47 | amountEt = findViewById(R.id.amount_et); 48 | noteEt = findViewById(R.id.note); 49 | nameEt = findViewById(R.id.name); 50 | upiIdEt = findViewById(R.id.upi_id); 51 | } 52 | 53 | void payUsingUpi(String amount, String upiId, String name, String note) { 54 | 55 | Uri uri = Uri.parse("upi://pay").buildUpon() 56 | .appendQueryParameter("pa", upiId) 57 | .appendQueryParameter("pn", name) 58 | .appendQueryParameter("tn", note) 59 | .appendQueryParameter("am", amount) 60 | .appendQueryParameter("cu", "INR") 61 | .build(); 62 | 63 | 64 | Intent upiPayIntent = new Intent(Intent.ACTION_VIEW); 65 | upiPayIntent.setData(uri); 66 | 67 | // will always show a dialog to user to choose an app 68 | Intent chooser = Intent.createChooser(upiPayIntent, "Pay with"); 69 | 70 | // check if intent resolves 71 | if(null != chooser.resolveActivity(getPackageManager())) { 72 | startActivityForResult(chooser, UPI_PAYMENT); 73 | } else { 74 | Toast.makeText(MainActivity.this,"No UPI app found, please install one to continue",Toast.LENGTH_SHORT).show(); 75 | } 76 | 77 | } 78 | 79 | @Override 80 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 81 | super.onActivityResult(requestCode, resultCode, data); 82 | 83 | switch (requestCode) { 84 | case UPI_PAYMENT: 85 | if ((RESULT_OK == resultCode) || (resultCode == 11)) { 86 | if (data != null) { 87 | String trxt = data.getStringExtra("response"); 88 | Log.d("UPI", "onActivityResult: " + trxt); 89 | ArrayList dataList = new ArrayList<>(); 90 | dataList.add(trxt); 91 | upiPaymentDataOperation(dataList); 92 | } else { 93 | Log.d("UPI", "onActivityResult: " + "Return data is null"); 94 | ArrayList dataList = new ArrayList<>(); 95 | dataList.add("nothing"); 96 | upiPaymentDataOperation(dataList); 97 | } 98 | } else { 99 | Log.d("UPI", "onActivityResult: " + "Return data is null"); //when user simply back without payment 100 | ArrayList dataList = new ArrayList<>(); 101 | dataList.add("nothing"); 102 | upiPaymentDataOperation(dataList); 103 | } 104 | break; 105 | } 106 | } 107 | 108 | private void upiPaymentDataOperation(ArrayList data) { 109 | if (isConnectionAvailable(MainActivity.this)) { 110 | String str = data.get(0); 111 | Log.d("UPIPAY", "upiPaymentDataOperation: "+str); 112 | String paymentCancel = ""; 113 | if(str == null) str = "discard"; 114 | String status = ""; 115 | String approvalRefNo = ""; 116 | String response[] = str.split("&"); 117 | for (int i = 0; i < response.length; i++) { 118 | String equalStr[] = response[i].split("="); 119 | if(equalStr.length >= 2) { 120 | if (equalStr[0].toLowerCase().equals("Status".toLowerCase())) { 121 | status = equalStr[1].toLowerCase(); 122 | } 123 | else if (equalStr[0].toLowerCase().equals("ApprovalRefNo".toLowerCase()) || equalStr[0].toLowerCase().equals("txnRef".toLowerCase())) { 124 | approvalRefNo = equalStr[1]; 125 | } 126 | } 127 | else { 128 | paymentCancel = "Payment cancelled by user."; 129 | } 130 | } 131 | 132 | if (status.equals("success")) { 133 | //Code to handle successful transaction here. 134 | Toast.makeText(MainActivity.this, "Transaction successful.", Toast.LENGTH_SHORT).show(); 135 | Log.d("UPI", "responseStr: "+approvalRefNo); 136 | } 137 | else if("Payment cancelled by user.".equals(paymentCancel)) { 138 | Toast.makeText(MainActivity.this, "Payment cancelled by user.", Toast.LENGTH_SHORT).show(); 139 | } 140 | else { 141 | Toast.makeText(MainActivity.this, "Transaction failed.Please try again", Toast.LENGTH_SHORT).show(); 142 | } 143 | } else { 144 | Toast.makeText(MainActivity.this, "Internet connection is not available. Please check and try again", Toast.LENGTH_SHORT).show(); 145 | } 146 | } 147 | 148 | public static boolean isConnectionAvailable(Context context) { 149 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 150 | if (connectivityManager != null) { 151 | NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); 152 | if (netInfo != null && netInfo.isConnected() 153 | && netInfo.isConnectedOrConnecting() 154 | && netInfo.isAvailable()) { 155 | return true; 156 | } 157 | } 158 | return false; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 29 | 30 | 40 | 41 | 51 | 52 |