edesigners64 Posted December 21, 2004 Posted December 21, 2004 I found this code with a Google for 'credit card algorithm' - that checks the numerical authenticity of a credit card number before the transaction is completed. Source of code Can anyone write something that incorporates this code? Thanks - Eric //--------------------------------------------------------- // Checks for valid credit card number using Luhn algorithm //--------------------------------------------------------- public abstract class LuhnCheck { //-------------------------------- // Filter out non-digit characters //-------------------------------- private static String getDigitsOnly (String s) { StringBuffer digitsOnly = new StringBuffer (); char c; for (int i = 0; i < s.length (); i++) { c = s.charAt (i); if (Character.isDigit (c)) { digitsOnly.append (c); } } return digitsOnly.toString (); } //------------------- // Perform Luhn check //------------------- public static boolean isValid (String cardNumber) { String digitsOnly = getDigitsOnly (cardNumber); int sum = 0; int digit = 0; int addend = 0; boolean timesTwo = false; for (int i = digitsOnly.length () - 1; i >= 0; i--) { digit = Integer.parseInt (digitsOnly.substring (i, i + 1)); if (timesTwo) { addend = digit * 2; if (addend > 9) { addend -= 9; } } else { addend = digit; } sum += addend; timesTwo = !timesTwo; } int modulus = sum % 10; return modulus == 0; } //----- // Test //----- public static void main (String[] args) { String cardNumber = "4408 0412 3456 7890"; boolean valid = LuhnCheck.isValid (cardNumber); System.out.println (cardNumber + ": " + valid); cardNumber = "4408 0412 3456 7893"; valid = LuhnCheck.isValid (cardNumber); System.out.println (cardNumber + ": " + valid); cardNumber = "4417 1234 5678 9112"; valid = LuhnCheck.isValid (cardNumber); System.out.println (cardNumber + ": " + valid); cardNumber = "4417 1234 5678 9113"; valid = LuhnCheck.isValid (cardNumber); System.out.println (cardNumber + ": " + valid); } } Quote
dreamscape Posted December 21, 2004 Posted December 21, 2004 I found this code with a Google for 'credit card algorithm' - that checks the numerical authenticity of a credit card number before the transaction is completed. <{POST_SNAPBACK}> The default osCommerce credit card module already does this (see the credit card validation class). And any payment gateway module that I have seen also makes use of the validation class to validate several different things, including performing a mod10 check on the number. Any payment module that uses credit cards should use it anyways. But it is already in the core, though the part that tries to identify the card type should be cleaned up because it currently rejects certain valid numbers. Quote The only thing necessary for evil to flourish is for good men to do nothing - Edmund Burke
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.