Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
card validation and final changes
- Loading branch information
Roshan Koshy Thomas
committed
Aug 26, 2022
1 parent
a588e68
commit ba108e7ffc522d21f221033ef9ae4a6d6930e285
Showing
39 changed files
with
689 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/nbproject/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,55 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>computer-for-you</groupId> | ||
<artifactId>e-cart</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.23</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<release>14</release> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.2.3</version> | ||
<configuration> | ||
<warSourceDirectory>WebContent</warSourceDirectory> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>computer-for-you</groupId> | ||
<artifactId>e-cart</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.23</version> | ||
|
||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>2.0.2-beta</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.8.2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<release>14</release> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.2.3</version> | ||
<configuration> | ||
<warSourceDirectory>WebContent</warSourceDirectory> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.19.1</version> | ||
<configuration> | ||
<testFailureIgnore>true</testFailureIgnore> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package cfy.connection; | ||
|
||
public class ValidateCard { | ||
public static final int INVALID = -1; | ||
public static final int VISA = 0; | ||
public static final int MASTERCARD = 1; | ||
public static final int AMERICAN_EXPRESS = 2; | ||
public static final int EN_ROUTE = 3; | ||
public static final int DINERS_CLUB = 4; | ||
|
||
private static final String[] cardNames = { "Visa", "Mastercard", "American Express", "En Route", | ||
"Diner's CLub/Carte Blanche", }; | ||
|
||
/** | ||
* Valid a Credit Card number | ||
*/ | ||
public boolean validCC(String number) throws Exception { | ||
int CardID; | ||
if ((CardID = getCardID(number)) != -1) | ||
return validCCNumber(number); | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the Card type returns the credit card type INVALID = -1; VISA = 0; | ||
* MASTERCARD = 1; AMERICAN_EXPRESS = 2; EN_ROUTE = 3; DINERS_CLUB = 4; | ||
*/ | ||
public int getCardID(String number) { | ||
int valid = INVALID; | ||
|
||
String digit1 = number.substring(0, 1); | ||
String digit2 = number.substring(0, 2); | ||
String digit3 = number.substring(0, 3); | ||
String digit4 = number.substring(0, 4); | ||
|
||
if (isNumber(number)) { | ||
/* | ||
* ---- VISA prefix=4 ---- length=13 or 16 (can be 15 too!?! maybe) | ||
*/ | ||
if (digit1.equals("4")) { | ||
if (number.length() == 13 || number.length() == 16) | ||
valid = VISA; | ||
} | ||
/* | ||
* ---------- MASTERCARD prefix= 51 ... 55 ---------- length= 16 | ||
*/ | ||
else if (digit2.compareTo("51") >= 0 && digit2.compareTo("55") <= 0) { | ||
if (number.length() == 16) | ||
valid = MASTERCARD; | ||
} | ||
|
||
|
||
|
||
/* | ||
* ---- AMEX prefix=34 or 37 ---- length=15 | ||
*/ | ||
else if (digit2.equals("34") || digit2.equals("37")) { | ||
if (number.length() == 15) | ||
valid = AMERICAN_EXPRESS; | ||
} | ||
/* | ||
* ----- ENROU prefix=2014 or 2149 ----- length=15 | ||
*/ | ||
else if (digit4.equals("2014") || digit4.equals("2149")) { | ||
if (number.length() == 15) | ||
valid = EN_ROUTE; | ||
} | ||
/* | ||
* ----- DCLUB prefix=300 ... 305 or 36 or 38 ----- length=14 | ||
*/ | ||
else if (digit2.equals("36") || digit2.equals("38") | ||
|| (digit3.compareTo("300") >= 0 && digit3.compareTo("305") <= 0)) { | ||
if (number.length() == 14) | ||
valid = DINERS_CLUB; | ||
} | ||
} | ||
return valid; | ||
|
||
/* | ||
* ---- DISCOVER card prefix = 60 -------- lenght = 16 left as an exercise ... | ||
*/ | ||
|
||
} | ||
|
||
public boolean isNumber(String n) { | ||
try { | ||
double d = Double.valueOf(n).doubleValue(); | ||
return true; | ||
} catch (NumberFormatException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
public String getCardName(int id) { | ||
return (id > -1 && id < cardNames.length ? cardNames[id] : ""); | ||
} | ||
|
||
public boolean validCCNumber(String n) { | ||
try { | ||
/* | ||
** known as the LUHN Formula (mod10) | ||
*/ | ||
int j = n.length(); | ||
|
||
String[] s1 = new String[j]; | ||
for (int i = 0; i < n.length(); i++) | ||
s1[i] = "" + n.charAt(i); | ||
|
||
int checksum = 0; | ||
|
||
for (int i = s1.length - 1; i >= 0; i -= 2) { | ||
int k = 0; | ||
|
||
if (i > 0) { | ||
k = Integer.valueOf(s1[i - 1]).intValue() * 2; | ||
if (k > 9) { | ||
String s = "" + k; | ||
k = Integer.valueOf(s.substring(0, 1)).intValue() + Integer.valueOf(s.substring(1)).intValue(); | ||
} | ||
checksum += Integer.valueOf(s1[i]).intValue() + k; | ||
} else | ||
checksum += Integer.valueOf(s1[0]).intValue(); | ||
} | ||
return ((checksum % 10) == 0); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.