Skip to content

Instantly share code, notes, and snippets.

@linakis
Created February 8, 2018 07:41
Show Gist options
  • Save linakis/022230ee29eb8339a6936ea9a84d775d to your computer and use it in GitHub Desktop.
Save linakis/022230ee29eb8339a6936ea9a84d775d to your computer and use it in GitHub Desktop.
Paysafe Check Digit calculation/validation based on Apache Commons validator
import org.apache.commons.validator.routines.checkdigit.CheckDigit;
import org.apache.commons.validator.routines.checkdigit.CheckDigitException;
import org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit;
/**
* Modulus 10 <b>Paysafe</b> Check Digit calculation/validation.
* <p>
* Check digit calculation is based on <i>modulus 10</i> with digits in
* an <i>odd</i> position (from left to right) being weighted 1 and <i>even</i>
* position digits being weighted 3.
* <p>
*
* Created by Nikos Linakis on 11/12/2017.
*/
public class PaysafeCheckDigit extends ModulusCheckDigit {
/** Singleton Paysafe Check Digit instance */
public static final CheckDigit PAYSAFE_CHECK_DIGIT = new PaysafeCheckDigit();
/** weighting given to digits depending on their left position */
private static final int[] POSITION_WEIGHT = new int[] {3, 1};
/**
* Construct a modulus 10 Check Digit routine
*/
public PaysafeCheckDigit() {
super(10); // CHECKSTYLE IGNORE MagicNumber
}
/**
* <p>Calculates the <i>weighted</i> value of a character in the
* code at a specified position.</p>
*
* <p>For Paysafe (from left to right) <b>odd</b> digits are weighted
* with a factor of <b>one</b> and <b>even</b> digits with a factor
* of <b>three</b>.</p>
*
* @param charValue The numeric value of the character.
* @param leftPos The position of the character in the code, counting from left to right
* @param rightPos The position of the character in the code, counting from right to left
* @return The weighted value of the character.
*/
@Override
protected int weightedValue(int charValue, int leftPos, int rightPos) {
int weight = POSITION_WEIGHT[leftPos % 2];
return charValue * weight;
}
@Override
public boolean isValid(String code) {
if (code == null || code.length() == 0) {
return false;
}
try {
// last character digit is our check digit
int checkDigit = Integer.parseInt(code.substring(code.length() - 1));
// calculate the mod result of the code sans the check digit using the weightedValue logic
int modulusResult = calculateModulus(code.substring(0, code.length() - 1), false);
// the difference of the modulus result from 10 is the check digit.
// In case the modulus result is 0 then our remainder is 0.
// Because 0 IS a multiple of 10 (10 * 0 = 0).
int remainder = modulusResult == 0 ? 0 : 10 - modulusResult;
return (remainder == checkDigit);
} catch (CheckDigitException ex) {
return false;
}
}
}
@linakis
Copy link
Author

linakis commented Feb 8, 2018

Usage:

PaysafeCheckDigit.PAYSAFE_CHECK_DIGIT.isValid("0000000000000000")

@linakis
Copy link
Author

linakis commented Feb 8, 2018

Add Apache Commons validator for Android

dependencies {
    compile 'commons-validator:commons-validator:1.6'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment