Monday, April 23, 2007

[completeJava] Re: credit card authentication


--- In completeJava@yahoogroups.com, yousuf ahmed <yousuf_edu2005@...> wrote:
>
> hello all,
> i wanted to know how credit card processing is done using java.
> plz reply.
> thanks
>
>
> ---------------------------------
> Check out what you're missing if you're not on Yahoo! Messenger
>

I Think the below code may help you

 

The CreditCardValidator

CreditCardValidator does pre-validation of credit card numbers before actually sending them off for authrorization. It is a very powerful validator that uses the Luhn algorithm to catch mistakes in credit card number input, all the way down to the user simply mistyping a single digit in the card number. As you can see, validation criteria for a validator package can be arbitrarily complex. Let's look at the source code for CreditCardValidator:

   import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JTextField; import java.util.regex.*;   /**  * A class for performing pre-validation on credit cards.  *   * @author Michael Urban  */   public class CreditCardValidator extends AbstractValidator {     public static final int MASTERCARD = 0, VISA = 1;     public static final int AMEX = 2, DISCOVER = 3, DINERS = 4;     private JDialog parent;     private Object form; 	     private static final String[] messages = {         "Not a valid number for MasterCard.",         "Not a valid number for Visa.",         "Not a valid number for American Express.",         "Not a valid number for Discover.",         "Not a valid number for Diner's Club"     }; 	     public CreditCardValidator(JDialog parent, Object form, JComponent c,         String message)      {         super(parent, c, message);         this.parent = parent;         this.form = form;     }       protected boolean validationCriteria(JComponent c) {         String number = ((JTextField)c).getText(); 		         if (number.equals("")) {             setMessage("Field cannnot be blank.");             return false;         } 		         Matcher m = Pattern.compile("[^\\d\\s.-]").matcher(number);                  if (m.find()) {             setMessage("Credit card number can only contain numbers, spaces, \"-\", and \".\"");             return false;         } 		         int type = ((CreditCardValidation)form).validateCreditCardType();         setMessage(messages[type]);         Matcher matcher = Pattern.compile("[\\s.-]").matcher(number);         number = matcher.replaceAll("");         return validate(number, type);     }       // Check that cards start with proper digits for     // selected card type and are also the right length.           private boolean validate(String number, int type) {         switch(type) { 		         case MASTERCARD:             if (number.length() != 16 ||                 Integer.parseInt(number.substring(0, 2)) < 51 ||                 Integer.parseInt(number.substring(0, 2)) > 55)             {                 return false;             }             break; 			         case VISA:             if ((number.length() != 13 && number.length() != 16) ||                     Integer.parseInt(number.substring(0, 1)) != 4)             {                 return false;             }             break; 			         case AMEX:             if (number.length() != 15 ||                 (Integer.parseInt(number.substring(0, 2)) != 34 &&                     Integer.parseInt(number.substring(0, 2)) != 37))             {                 return false;             }             break; 			         case DISCOVER:             if (number.length() != 16 ||                 Integer.parseInt(number.substring(0, 5)) != 6011)             {                 return false;             }             break; 			         case DINERS:             if (number.length() != 14 ||                 ((Integer.parseInt(number.substring(0, 2)) != 36 &&                     Integer.parseInt(number.substring(0, 2)) != 38) &&                     Integer.parseInt(number.substring(0, 3)) < 300 ||                         Integer.parseInt(number.substring(0, 3)) > 305))             {                 return false;             }             break;         }         return luhnValidate(number);     }       // The Luhn algorithm is basically a CRC type     // system for checking the validity of an entry.     // All major credit cards use numbers that will     // pass the Luhn check. Also, all of them are based     // on MOD 10. 	     private boolean luhnValidate(String numberString) {         char[] charArray = numberString.toCharArray();         int[] number = new int[charArray.length];         int total = 0; 		         for (int i=0; i < charArray.length; i++) {             number[i] = Character.getNumericValue(charArray[i]);         } 		         for (int i = number.length-2; i > -1; i-=2) {             number[i] *= 2; 			             if (number[i] > 9)                 number[i] -= 9;         } 		         for (int i=0; i < number.length; i++)             total += number[i]; 		             if (total % 10 != 0)                 return false; 		         return true;     } } 

Because we need to check the credit card type, notice that this validator requires an additional interface so that our validator can retrieve the credit card type from the form. That interface is CreditCardValidation:

   public interface CreditCardValidation {     int validateCreditCardType(); } 

Notice that in this validator, we provide context sensitive pop help messages and check for three different possible problems: Whether the the user left the credit card field blank; whether the user entered illegal characters in the credit card field; and whether or not the credit card type could be a valid credit card number for the card type in question.

CreditCardValidator is mostly just algorithm programming, so I am not going to go into details about how it works. But suffice it to say, it shows that the validation criteria we implement can be arbitrarily complex, and that in CreditCardValidator, all we had to focus on was writing the logic for our validator. We didn't haven't to worry about presenting validation results to the user at all. AbstractValidator we wrote earlier takes care of all those details for us.

 

__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New web site?

Drive traffic now.

Get your business

on Yahoo! search.

Yahoo! Groups

Start a group

in 3 easy steps.

Connect with others.

.

__,_._,___

No comments: