From f3d49040fc985908169c77de0f2512861557bd22 Mon Sep 17 00:00:00 2001 From: carlos Date: Wed, 9 Oct 2019 21:12:32 -0500 Subject: [PATCH 01/10] Update App to Kotlin & AndroidX & Gradle & gitignore --- .gitignore | 103 +++++++--- .idea/codeStyles/Project.xml | 116 +++++++++++ app/app.iml | 191 +++++++++++------- app/build.gradle | 35 ++-- app/src/main/AndroidManifest.xml | 4 +- .../culqi/culqi_android/Culqi/Card.java | 66 ------ .../android/culqi/culqi_android/Culqi/Card.kt | 7 + .../culqi/culqi_android/Culqi/Config.java | 12 -- .../culqi/culqi_android/Culqi/Config.kt | 6 + .../culqi/culqi_android/Culqi/Token.java | 2 +- .../culqi_android/Culqi/TokenCallback.java | 15 -- .../culqi_android/Culqi/TokenCallback.kt | 11 + .../culqi/culqi_android/MainActivity.java | 165 --------------- .../culqi/culqi_android/MainActivity.kt | 118 +++++++++++ .../culqi_android/Validation/Validation.java | 94 --------- .../culqi_android/validation/Validation.kt | 91 +++++++++ app/src/main/res/layout/activity_main.xml | 158 +++++++-------- app/src/main/res/values/strings.xml | 8 + build.gradle | 7 +- gradle/wrapper/gradle-wrapper.properties | 2 +- local.properties | 9 +- 21 files changed, 665 insertions(+), 555 deletions(-) create mode 100644 .idea/codeStyles/Project.xml delete mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.java create mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.kt delete mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.java create mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.kt delete mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.java create mode 100644 app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.kt delete mode 100644 app/src/main/java/com/android/culqi/culqi_android/MainActivity.java create mode 100644 app/src/main/java/com/android/culqi/culqi_android/MainActivity.kt delete mode 100644 app/src/main/java/com/android/culqi/culqi_android/Validation/Validation.java create mode 100644 app/src/main/java/com/android/culqi/culqi_android/validation/Validation.kt diff --git a/.gitignore b/.gitignore index ea83a5e..dcdc776 100644 --- a/.gitignore +++ b/.gitignore @@ -1,44 +1,83 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +# Built application files +*.apk +*.ap_ +*.aab -# User-specific stuff: +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +# Uncomment the following line in case you need and you don't have the release build type files in your app +# release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml .idea/workspace.xml .idea/tasks.xml -.idea/dictionaries -.idea/vcs.xml -.idea/jsLibraryMappings.xml - -# Sensitive or high-churn files: -.idea/dataSources.ids -.idea/dataSources.xml -.idea/dataSources.local.xml -.idea/sqlDataSources.xml -.idea/dynamic.xml -.idea/uiDesigner.xml - -# Gradle: .idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries .idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml -# Mongo Explorer plugin: -.idea/mongoSettings.xml +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore -## File-based project format: -*.iws +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild -## Plugin-specific files: +# Google Services (e.g. APIs or Firebase) +# google-services.json -# IntelliJ -/out/ +# Freeline +freeline.py +freeline/ +freeline_project_description.json -# mpeltonen/sbt-idea plugin -.idea_modules/ +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md -# JIRA plugin -atlassian-ide-plugin.xml +# Version control +vcs.xml -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 3bdf759..9756f24 100644 --- a/app/app.iml +++ b/app/app.iml @@ -4,6 +4,8 @@ @@ -18,29 +20,68 @@ + + + + + + + + + + + - - - + + + - - + + - - - - - - + + + + + - - - - + + + + @@ -48,6 +89,13 @@ + + + + + + + @@ -62,13 +110,6 @@ - - - - - - - @@ -76,55 +117,65 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index a7f4ed3..a523484 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,19 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' + +apply plugin: 'kotlin-android-extensions' + android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 29 defaultConfig { applicationId "com.android.culqi.culqi_android" - minSdkVersion 15 - targetSdkVersion 25 + minSdkVersion 16 + targetSdkVersion 29 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + multiDexEnabled true } buildTypes { release { @@ -20,11 +24,18 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { - exclude group: 'com.android.support', module: 'support-annotations' - }) - compile 'com.android.support:appcompat-v7:25.1.0' - compile 'com.android.volley:volley:1.0.0' - testCompile 'junit:junit:4.12' + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + //ANDROID X + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + + //RETROFIT + implementation 'com.squareup.retrofit2:retrofit:2.6.1' + + //TEST + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cfe6302..d28efc5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -9,7 +10,8 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/AppTheme"> + android:theme="@style/AppTheme" + tools:ignore="GoogleAppIndexingWarning"> diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.java b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.java deleted file mode 100644 index bcff8d8..0000000 --- a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.android.culqi.culqi_android.Culqi; - -/** - * Created by culqi on 2/7/17. - */ - -public class Card { - - public Card(String card_number, String cvv, int expiration_month, int expiration_year, String email) { - this.card_number = card_number; - this.cvv = cvv; - this.expiration_month = expiration_month; - this.expiration_year = expiration_year; - this.email = email; - } - - private String card_number; - - private String cvv; - - private int expiration_month; - - private int expiration_year; - - private String email; - - public String getCard_number() { - return card_number; - } - - public void setCard_number(String card_number) { - this.card_number = card_number; - } - - public String getCvv() { - return cvv; - } - - public void setCvv(String cvv) { - this.cvv = cvv; - } - - public int getExpiration_month() { - return expiration_month; - } - - public void setExpiration_month(int expiration_month) { - this.expiration_month = expiration_month; - } - - public int getExpiration_year() { - return expiration_year; - } - - public void setExpiration_year(int expiration_year) { - this.expiration_year = expiration_year; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } -} diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.kt b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.kt new file mode 100644 index 0000000..837b47d --- /dev/null +++ b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Card.kt @@ -0,0 +1,7 @@ +package com.android.culqi.culqi_android.culqi + +data class Card(val card_number: String, + val cvv: String, + val expiration_month: Int, + val expiration_year: Int, + val email: String) \ No newline at end of file diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.java b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.java deleted file mode 100644 index e3ba45d..0000000 --- a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.android.culqi.culqi_android.Culqi; - -/** - * Created by culqi on 1/19/17. - */ - -public class Config { - - public String url_base = "https://api.culqi.com/v2"; - public String url_base_secure = "https://secure.culqi.com/v2"; - -} diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.kt b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.kt new file mode 100644 index 0000000..b07f0eb --- /dev/null +++ b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Config.kt @@ -0,0 +1,6 @@ +package com.android.culqi.culqi_android.culqi + +class Config { + var url_base = "https://api.culqi.com/v2" + var url_base_secure = "https://secure.culqi.com/v2" +} \ No newline at end of file diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Token.java b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Token.java index b80dc4e..c35687d 100644 --- a/app/src/main/java/com/android/culqi/culqi_android/Culqi/Token.java +++ b/app/src/main/java/com/android/culqi/culqi_android/Culqi/Token.java @@ -1,4 +1,4 @@ -package com.android.culqi.culqi_android.Culqi; +package com.android.culqi.culqi_android.culqi; import android.content.Context; import android.util.Log; diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.java b/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.java deleted file mode 100644 index 9c4509e..0000000 --- a/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.android.culqi.culqi_android.Culqi; - -import org.json.JSONObject; - -/** - * Created by culqi on 2/7/17. - */ - -public interface TokenCallback { - - public void onSuccess(JSONObject token); - - public void onError(Exception error); - -} diff --git a/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.kt b/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.kt new file mode 100644 index 0000000..901c540 --- /dev/null +++ b/app/src/main/java/com/android/culqi/culqi_android/Culqi/TokenCallback.kt @@ -0,0 +1,11 @@ +package com.android.culqi.culqi_android.culqi + +import org.json.JSONObject + +interface TokenCallback { + + fun onSuccess(token: JSONObject) + + fun onError(error: Exception) + +} \ No newline at end of file diff --git a/app/src/main/java/com/android/culqi/culqi_android/MainActivity.java b/app/src/main/java/com/android/culqi/culqi_android/MainActivity.java deleted file mode 100644 index 3bc2092..0000000 --- a/app/src/main/java/com/android/culqi/culqi_android/MainActivity.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.android.culqi.culqi_android; - -import android.app.ProgressDialog; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.text.Editable; -import android.text.InputFilter; -import android.text.TextWatcher; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; - -import com.android.culqi.culqi_android.Culqi.Card; -import com.android.culqi.culqi_android.Culqi.Token; -import com.android.culqi.culqi_android.Culqi.TokenCallback; -import com.android.culqi.culqi_android.Validation.Validation; - -import org.json.JSONObject; - -public class MainActivity extends AppCompatActivity { - - Validation validation; - - ProgressDialog progress; - - TextView txtcardnumber, txtcvv, txtmonth, txtyear, txtemail, kind_card, result; - Button btnPay; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - validation = new Validation(); - - progress = new ProgressDialog(this); - progress.setMessage("Validando informacion de la tarjeta"); - progress.setCancelable(false); - progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); - - txtcardnumber = (TextView) findViewById(R.id.txt_cardnumber); - - txtcvv = (TextView) findViewById(R.id.txt_cvv); - - txtmonth = (TextView) findViewById(R.id.txt_month); - - txtyear = (TextView) findViewById(R.id.txt_year); - - txtemail = (TextView) findViewById(R.id.txt_email); - - kind_card = (TextView) findViewById(R.id.kind_card); - - result = (TextView) findViewById(R.id.token_id); - - btnPay = (Button) findViewById(R.id.btn_pay); - - txtcvv.setEnabled(false); - - txtcardnumber.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - if(s.length() == 0){ - txtcvv.setEnabled(true); - } - } - - @Override - public void afterTextChanged(Editable s) { - String text = txtcardnumber.getText().toString(); - if(s.length() == 0) { - txtcardnumber.setBackgroundResource(R.drawable.border_error); - } - - if(validation.luhn(text)) { - txtcardnumber.setBackgroundResource(R.drawable.border_sucess); - } else { - txtcardnumber.setBackgroundResource(R.drawable.border_error); - } - - int cvv = validation.bin(text, kind_card); - if(cvv > 0) { - txtcvv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(cvv)}); - txtcvv.setEnabled(true); - } else { - txtcvv.setEnabled(false); - txtcvv.setText(""); - } - } - }); - - txtyear.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - String text = txtyear.getText().toString(); - if(validation.year(text)){ - txtyear.setBackgroundResource(R.drawable.border_error); - } else { - txtyear.setBackgroundResource(R.drawable.border_sucess); - } - } - }); - - txtmonth.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - String text = txtmonth.getText().toString(); - if(validation.month(text)){ - txtmonth.setBackgroundResource(R.drawable.border_error); - } else { - txtmonth.setBackgroundResource(R.drawable.border_sucess); - } - } - }); - - btnPay.setOnClickListener(new View.OnClickListener(){ - public void onClick(View v){ - progress.show(); - - Card card = new Card(txtcardnumber.getText().toString(), txtcvv.getText().toString(), 9, 2020, txtemail.getText().toString()); - - Token token = new Token("pk_test_vzMuTHoueOMlgUPj"); - - token.createToken(getApplicationContext(), card, new TokenCallback() { - @Override - public void onSuccess(JSONObject token) { - try { - result.setText(token.get("id").toString()); - } catch (Exception ex){ - progress.hide(); - } - progress.hide(); - } - - @Override - public void onError(Exception error) { - progress.hide(); - } - }); - - } - }); - - } - -} diff --git a/app/src/main/java/com/android/culqi/culqi_android/MainActivity.kt b/app/src/main/java/com/android/culqi/culqi_android/MainActivity.kt new file mode 100644 index 0000000..7c37e8f --- /dev/null +++ b/app/src/main/java/com/android/culqi/culqi_android/MainActivity.kt @@ -0,0 +1,118 @@ +package com.android.culqi.culqi_android + +import android.app.ProgressDialog +import android.os.Bundle +import android.text.Editable +import android.text.InputFilter +import android.text.TextWatcher +import androidx.appcompat.app.AppCompatActivity +import com.android.culqi.culqi_android.culqi.Card +import com.android.culqi.culqi_android.culqi.Token +import com.android.culqi.culqi_android.culqi.TokenCallback +import com.android.culqi.culqi_android.validation.Validation +import kotlinx.android.synthetic.main.activity_main.* +import org.json.JSONObject + +class MainActivity : AppCompatActivity() { + + internal lateinit var validation: Validation + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + validation = Validation() + + val progress = ProgressDialog(this) + progress.setMessage("Validando informacion de la tarjeta") + progress.setCancelable(false) + progress.setProgressStyle(ProgressDialog.STYLE_SPINNER) + + etMainCardNumber.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + if (s.isEmpty()) { + etMainCVV.isEnabled = true + } + } + + override fun afterTextChanged(s: Editable) { + val text = etMainCardNumber.text.toString() + if (s.length == 0) { + etMainCardNumber.setBackgroundResource(R.drawable.border_error) + } + + if (validation.luhn(text)) { + etMainCardNumber.setBackgroundResource(R.drawable.border_sucess) + } else { + etMainCardNumber.setBackgroundResource(R.drawable.border_error) + } + + val cvv = validation.bin(text, tvMainKindCard) + if (cvv > 0) { + etMainCVV.filters = arrayOf(InputFilter.LengthFilter(cvv)) + etMainCVV.isEnabled = true + } else { + etMainCVV.isEnabled = false + etMainCVV.setText("") + } + } + }) + + etMainYear.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} + + override fun afterTextChanged(s: Editable) { + val text = etMainYear.text.toString() + if (validation.year(text)) { + etMainYear.setBackgroundResource(R.drawable.border_error) + } else { + etMainYear.setBackgroundResource(R.drawable.border_sucess) + } + } + }) + + etMainMonth.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} + + override fun afterTextChanged(s: Editable) { + val text = etMainMonth.text.toString() + if (validation.month(text)) { + etMainMonth.setBackgroundResource(R.drawable.border_error) + } else { + etMainMonth.setBackgroundResource(R.drawable.border_sucess) + } + } + }) + + btMainCreateToken.setOnClickListener { + progress.show() + + val card = Card(etMainCardNumber.text.toString(), etMainCVV.text.toString(), 9, 2020, etMainEmail.text.toString()) + + val token = Token("pk_test_vzMuTHoueOMlgUPj") + + token.createToken(applicationContext, card, object : TokenCallback { + override fun onSuccess(token: JSONObject) { + try { + tvMainTokenId.text = token.get("id").toString() + } catch (ex: Exception) { + progress.hide() + } + + progress.hide() + } + + override fun onError(error: Exception) { + progress.hide() + } + }) + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/android/culqi/culqi_android/Validation/Validation.java b/app/src/main/java/com/android/culqi/culqi_android/Validation/Validation.java deleted file mode 100644 index 83b23f5..0000000 --- a/app/src/main/java/com/android/culqi/culqi_android/Validation/Validation.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.android.culqi.culqi_android.Validation; - -import android.widget.TextView; - -import java.util.Calendar; -import java.util.Date; - -/** - * Created by culqi on 1/19/17. - */ - -public class Validation { - - public static boolean luhn(String number){ - int s1 = 0, s2 = 0; - String reverse = new StringBuffer(number).reverse().toString(); - for(int i = 0 ;i < reverse.length();i++){ - int digit = Character.digit(reverse.charAt(i), 10); - if(i % 2 == 0){//this is for odd digits, they are 1-indexed in the algorithm - s1 += digit; - }else{//add 2 * digit for 0-4, add 2 * digit - 9 for 5-9 - s2 += 2 * digit; - if(digit >= 5){ - s2 -= 9; - } - } - } - return (s1 + s2) % 10 == 0; - } - - public int bin(String bin, final TextView kind_card) { - - if(bin.length() > 1) { - if(Integer.valueOf(bin.substring(0,2)) == 41) { - kind_card.setText("VISA"); - return 3; - } else if (Integer.valueOf(bin.substring(0,2)) == 51) { - kind_card.setText("MasterCard"); - return 3; - } else { - } - } else { - kind_card.setText(""); - } - - if(bin.length() > 1) { - if(Integer.valueOf(bin.substring(0,2)) == 36){ - kind_card.setText("Diners Club"); - return 3; - } else if(Integer.valueOf(bin.substring(0,2)) == 38){ - kind_card.setText("Diners Club"); - return 3; - } else if(Integer.valueOf(bin.substring(0,2)) == 37){ - kind_card.setText("AMEX"); - return 3; - } else { - } - } - - if(bin.length() > 2) { - if(Integer.valueOf(bin.substring(0,3)) == 300){ - kind_card.setText("Diners Club"); - return 3; - } else if(Integer.valueOf(bin.substring(0,3)) == 305){ - kind_card.setText("Diners Club"); - return 3; - } else { - } - } - return 0; - } - - public boolean month(String month) { - if(!month.equals("")){ - if(Integer.valueOf(""+month) > 12) { - return true; - } - } - return false; - } - - public boolean year(String year){ - Date today = new Date(); - Calendar calendar = Calendar.getInstance(); - calendar.setTime(today); - if(!year.equals("")){ - if(Integer.valueOf("20"+year) < calendar.get(Calendar.YEAR)) { - return true; - } - } - return false; - } - -} diff --git a/app/src/main/java/com/android/culqi/culqi_android/validation/Validation.kt b/app/src/main/java/com/android/culqi/culqi_android/validation/Validation.kt new file mode 100644 index 0000000..184d10c --- /dev/null +++ b/app/src/main/java/com/android/culqi/culqi_android/validation/Validation.kt @@ -0,0 +1,91 @@ +package com.android.culqi.culqi_android.validation + +import android.widget.TextView +import java.util.* + +class Validation { + + fun bin(bin: String, kind_card: TextView): Int { + + if (bin.length > 1) { + when (Integer.valueOf(bin.substring(0, 2))) { + 36 -> { + kind_card.text = "Diners Club" + return 3 + } + 38 -> { + kind_card.text = "Diners Club" + return 3 + } + 37 -> { + kind_card.text = "AMEX" + return 3 + } + 41 -> { + kind_card.text = "VISA" + return 3 + } + 51 -> { + kind_card.text = "MasterCard" + return 3 + } + } + } else { + kind_card.text = "" + } + + if (bin.length > 2) { + when (Integer.valueOf(bin.substring(0, 3))) { + 300 -> { + kind_card.text = "Diners Club" + return 3 + } + 305 -> { + kind_card.text = "Diners Club" + return 3 + } + } + } + return 0 + } + + fun month(month: String): Boolean { + if (month != "") { + if (month.toInt() > 12) { + return true + } + } + return false + } + + fun year(year: String): Boolean { + val today = Date() + val calendar = Calendar.getInstance() + calendar.time = today + if (year != "") { + if (Integer.valueOf("20$year") < calendar.get(Calendar.YEAR)) { + return true + } + } + return false + } + + fun luhn(number: String): Boolean { + var s1 = 0 + var s2 = 0 + val reverse = StringBuffer(number).reverse().toString() + for (i in 0 until reverse.length) { + val digit = Character.digit(reverse[i], 10) + if (i % 2 == 0) {//this is for odd digits, they are 1-indexed in the algorithm + s1 += digit + } else {//add 2 * digit for 0-4, add 2 * digit - 9 for 5-9 + s2 += 2 * digit + if (digit >= 5) { + s2 -= 9 + } + } + } + return (s1 + s2) % 10 == 0 + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index cb7f5c1..9388eba 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,123 +1,123 @@ - + android:selectAllOnFocus="false" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + android:layout_marginTop="19dp" + android:background="@drawable/border" + android:ems="10" + android:hint="@string/main_month" + android:inputType="number" + android:maxLength="2" + app:layout_constraintEnd_toStartOf="@+id/etMainYear" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/etMainCardNumber" /> + android:background="@drawable/border" + android:ems="10" + android:hint="@string/main_year" + android:inputType="number" + android:maxLength="2" + app:layout_constraintBaseline_toBaselineOf="@+id/etMainMonth" + app:layout_constraintEnd_toStartOf="@+id/etMainCVV" + app:layout_constraintStart_toEndOf="@+id/etMainMonth" /> + + + android:ems="10" + android:hint="@string/main_email" + android:inputType="textEmailAddress" />