Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from xendit/feature/luhn-validation
Browse files Browse the repository at this point in the history
add luhn validation
  • Loading branch information
hakiemaul authored Aug 31, 2018
2 parents 7c18842 + 9f69aa3 commit 0e2e5a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xendit-js-node",
"version": "0.2.0",
"version": "0.3.0",
"description": "JS client library for tokenizing credit cards as node modules",
"main": "./src/xendit.js",
"repository": {
Expand Down
24 changes: 23 additions & 1 deletion src/utils/credit_card_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ CreditCardUtil.isCreditCardNumberValid = function (creditCardNumber) {
return NUMBER_REGEX.test(creditCardNumber) &&
creditCardNumber.length >= 12 &&
creditCardNumber.length <= 19 &&
CreditCardUtil.getCardType(creditCardNumber) !== null;
CreditCardUtil.getCardType(creditCardNumber) !== null &&
CreditCardUtil.isValidLuhnNumber(creditCardNumber);
};

CreditCardUtil.isCreditCardExpirationDateValid = function (cardExpirationMonth, cardExpirationYear) {
Expand Down Expand Up @@ -51,6 +52,27 @@ CreditCardUtil.isCreditCardCVNValidForCardType = function (creditCardCVN, cardNu
}
};

CreditCardUtil.isValidLuhnNumber = function (cardNumber) {
var sum = 0, bEven = false;
cardNumber = cardNumber.replace(/\D/g, '');

for (var n = cardNumber.length - 1; n >= 0; n--) {
var cDigit = cardNumber.charAt(n);
var nDigit = parseInt(cDigit, 10);

if (bEven) {
if ((nDigit *= 2) > 9) {
nDigit -= 9;
}
}

sum += nDigit;
bEven = !bEven;
}

return (sum % 10) === 0;
};

CreditCardUtil.getCardType = function (cardNumber) {
if (cardNumber.indexOf('4') === 0) {
if (CreditCardUtil._isCardVisaElectron(cardNumber)) {
Expand Down

0 comments on commit 0e2e5a1

Please sign in to comment.