From c29d0bb850352dd80fa1c325024d347de9d5dcc6 Mon Sep 17 00:00:00 2001
From: Stanislav Idolov <sidolov@ebay.com>
Date: Tue, 30 Jun 2015 13:18:28 +0300
Subject: [PATCH] MAGETWO-38647: credit card validation issue -- fixes after CR

---
 .../credit-card-number-validator.js           | 16 ++++----
 .../credit-card-type.js                       | 33 +++++++++++-----
 .../luhn10-validator.js                       |  7 +++-
 .../credit-card-validation/cvv-validator.js   | 16 ++++----
 .../expiration-date-validator.js              | 10 ++---
 .../expiration-month-validator.js             | 10 ++---
 .../expiration-year-validator.js              | 39 +++++--------------
 .../expiration-date-validator/parse-date.js   |  2 +-
 8 files changed, 66 insertions(+), 67 deletions(-)

diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator.js
index 018c0cbb6b1..1f13a5af7c6 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator.js
@@ -13,7 +13,7 @@ define(
     function (utils, luhn10, creditCardTypes) {
         'use strict';
 
-        function result(card, isPotentiallyValid, isValid) {
+        function resultWrapper(card, isPotentiallyValid, isValid) {
             return {
                 card: card,
                 isValid: isValid,
@@ -29,21 +29,21 @@ define(
                 maxLength;
 
             if (utils.isEmpty(value)) {
-                return result(null, false, false);
+                return resultWrapper(null, false, false);
             }
 
             value = value.replace(/\-|\s/g, '');
 
             if (!/^\d*$/.test(value)) {
-                return result(null, false, false);
+                return resultWrapper(null, false, false);
             }
 
             potentialTypes = creditCardTypes.getCardTypes(value);
 
             if (potentialTypes.length === 0) {
-                return result(null, false, false);
+                return resultWrapper(null, false, false);
             } else if (potentialTypes.length !== 1) {
-                return result(null, true, false);
+                return resultWrapper(null, true, false);
             }
 
             cardType = potentialTypes[0];
@@ -56,17 +56,17 @@ define(
 
             for (i = 0; i < cardType.lengths.length; i++) {
                 if (cardType.lengths[i] === value.length) {
-                    return result(cardType, valid, valid);
+                    return resultWrapper(cardType, valid, valid);
                 }
             }
 
             maxLength = Math.max.apply(null, cardType.lengths);
 
             if (value.length < maxLength) {
-                return result(cardType, true, false);
+                return resultWrapper(cardType, true, false);
             }
 
-            return result(cardType, false, false);
+            return resultWrapper(cardType, false, false);
         };
     }
 );
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/credit-card-type.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/credit-card-type.js
index f0685c6d67c..71e0e57c5c1 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/credit-card-type.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/credit-card-type.js
@@ -13,7 +13,7 @@ define(
         'use strict';
         var types = [
             {
-                niceType: 'Visa',
+                title: 'Visa',
                 type: 'VI',
                 pattern: '^4\\d*$',
                 gaps: [4, 8, 12],
@@ -24,7 +24,7 @@ define(
                 }
             },
             {
-                niceType: 'MasterCard',
+                title: 'MasterCard',
                 type: 'MC',
                 pattern: '^5([1-5]\\d*)?$',
                 gaps: [4, 8, 12],
@@ -35,7 +35,7 @@ define(
                 }
             },
             {
-                niceType: 'American Express',
+                title: 'American Express',
                 type: 'AE',
                 pattern: '^3([47]\\d*)?$',
                 isAmex: true,
@@ -47,7 +47,7 @@ define(
                 }
             },
             {
-                niceType: 'Diners',
+                title: 'Diners',
                 type: 'DN',
                 pattern: '^3((0([0-5]\\d*)?)|[689]\\d*)?$',
                 gaps: [4, 10],
@@ -58,7 +58,7 @@ define(
                 }
             },
             {
-                niceType: 'Discover',
+                title: 'Discover',
                 type: 'DI',
                 pattern: '^6(0|01|011\\d*|5\\d*|4|4[4-9]\\d*)?$',
                 gaps: [4, 8, 12],
@@ -69,7 +69,7 @@ define(
                 }
             },
             {
-                niceType: 'JCB',
+                title: 'JCB',
                 type: 'JC',
                 pattern: '^((2|21|213|2131\\d*)|(1|18|180|1800\\d*)|(3|35\\d*))$',
                 gaps: [4, 8, 12],
@@ -80,7 +80,7 @@ define(
                 }
             },
             {
-                niceType: 'UnionPay',
+                title: 'UnionPay',
                 type: 'UN',
                 pattern: '^6(2\\d*)?$',
                 gaps: [4, 8, 12],
@@ -91,9 +91,24 @@ define(
                 }
             },
             {
-                niceType: 'Maestro',
+                title: 'Maestro',
                 type: 'SM',
-                pattern: '(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|(^(4936)([0-9]{12}$|[0-9]{14,15}$))',
+                pattern: '(^(5[0678])[0-9]{11,18}$)' +
+                '|(^(6[^05])[0-9]{11,18}$)' +
+                '|(^(601)[^1][0-9]{9,16}$)' +
+                '|(^(6011)[0-9]{9,11}$)' +
+                '|(^(6011)[0-9]{13,16}$)' +
+                '|(^(65)[0-9]{11,13}$)' +
+                '|(^(65)[0-9]{15,18}$)' +
+                '|(^(49030)[2-9]([0-9]{10}$' +
+                '|[0-9]{12,13}$))' +
+                '|(^(49033)[5-9]([0-9]{10}$' +
+                '|[0-9]{12,13}$))' +
+                '|(^(49110)[1-2]([0-9]{10}$' +
+                '|[0-9]{12,13}$))' +
+                '|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))' +
+                '|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))' +
+                '|(^(4936)([0-9]{12}$|[0-9]{14,15}$))',
                 gaps: [4, 8, 12],
                 lengths: [12, 13, 14, 15, 16, 17, 18, 19],
                 code: {
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator.js
index 22d5f8acdd9..38e93f084a2 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator.js
@@ -8,8 +8,11 @@ define(
     [],
     function() {
         'use strict';
-        return function(a,b,c,d,e) {
-            for(d = +a[b = a.length-1], e=0; b--;) {
+        /**
+         * Luhn algorithm verification
+         */
+        return function(a, b, c, d, e) {
+            for(d = +a[b = a.length-1], e = 0; b--;) {
                 c = +a[b];
                 d += ++e % 2 ? 2 * c % 10 + (c > 4) : c;
             }
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/cvv-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/cvv-validator.js
index 812ee62b363..45733c9f8ff 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/cvv-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/cvv-validator.js
@@ -9,31 +9,33 @@ define(
     function() {
         'use strict';
 
-        function result(isValid, isPotentiallyValid) {
+        function resultWrapper(isValid, isPotentiallyValid) {
             return {
                 isValid: isValid,
                 isPotentiallyValid: isPotentiallyValid
             };
         }
 
+        /**
+         * CVV number validation
+         * validate digit count fot CVV code
+         */
         return function(value, maxLength) {
             var DEFAULT_LENGTH = 3;
             maxLength = maxLength || DEFAULT_LENGTH;
 
             if (!/^\d*$/.test(value)) {
-                return result(false, false);
+                return resultWrapper(false, false);
             }
             if (value.length === maxLength) {
-                return result(true, true);
+                return resultWrapper(true, true);
             }
             if (value.length < maxLength) {
-                return result(false, true);
+                return resultWrapper(false, true);
             }
             if (value.length > maxLength) {
-                return result(false, false);
+                return resultWrapper(false, false);
             }
-
-            return result(true, true);
         };
     }
 );
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator.js
index 1dda53d0721..f7266a28274 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator.js
@@ -14,7 +14,7 @@ define(
     function(utils, parseDate, expirationMonth, expirationYear) {
         'use strict';
 
-        function result(isValid, isPotentiallyValid, month, year) {
+        function resultWrapper(isValid, isPotentiallyValid, month, year) {
             return {
                 isValid: isValid,
                 isPotentiallyValid: isPotentiallyValid,
@@ -29,7 +29,7 @@ define(
                 yearValid;
 
             if (utils.isEmpty(value)) {
-                return result(false, false, null, null);
+                return resultWrapper(false, false, null, null);
             }
 
             value = value.replace(/^(\d\d) (\d\d(\d\d)?)$/, '$1/$2');
@@ -38,14 +38,14 @@ define(
             yearValid = expirationYear(date.year);
 
             if (monthValid.isValid && yearValid.isValid) {
-                return result(true, true, date.month, date.year);
+                return resultWrapper(true, true, date.month, date.year);
             }
 
             if (monthValid.isPotentiallyValid && yearValid.isPotentiallyValid) {
-                return result(false, true, null, null);
+                return resultWrapper(false, true, null, null);
             }
 
-            return result(false, false, null, null);
+            return resultWrapper(false, false, null, null);
         }
     }
 );
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-month-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-month-validator.js
index d9f9729a7c6..c41ad14a459 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-month-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-month-validator.js
@@ -9,7 +9,7 @@ define(
     function () {
         'use strict';
 
-        function result(isValid, isPotentiallyValid) {
+        function resultWrapper(isValid, isPotentiallyValid) {
             return {
                 isValid: isValid,
                 isPotentiallyValid: isPotentiallyValid
@@ -21,21 +21,21 @@ define(
                 monthValid;
 
             if ((value.replace(/\s/g, '') === '') || (value === '0')) {
-                return result(false, true);
+                return resultWrapper(false, true);
             }
 
             if (!/^\d*$/.test(value)) {
-                return result(false, false);
+                return resultWrapper(false, false);
             }
 
             if (isNaN(value)) {
-                return result(false, false);
+                return resultWrapper(false, false);
             }
 
             month = parseInt(value, 10);
             monthValid = month > 0 && month < 13;
 
-            return result(monthValid, monthValid);
+            return resultWrapper(monthValid, monthValid);
         };
     }
 );
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-year-validator.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-year-validator.js
index 5b0c1da544b..132c8dc42e9 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-year-validator.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-year-validator.js
@@ -9,7 +9,7 @@ define(
     function() {
         'use strict';
 
-        function result(isValid, isPotentiallyValid) {
+        function resultWrapper(isValid, isPotentiallyValid) {
             return {
                 isValid: isValid,
                 isPotentiallyValid: isPotentiallyValid
@@ -17,47 +17,26 @@ define(
         }
 
         return function(value) {
-            var currentFirstTwo,
-                currentYear = new Date().getFullYear(),
-                firstTwo,
+            var currentYear = new Date().getFullYear(),
                 len = value.length,
-                twoDigitYear,
                 valid,
-                maxYear = 19;
+                expMaxLifetime = 19;
 
             if (value.replace(/\s/g, '') === '') {
-                return result(false, true);
+                return resultWrapper(false, true);
             }
 
             if (!/^\d*$/.test(value)) {
-                return result(false, false);
+                return resultWrapper(false, false);
             }
 
-            if (len < 2) {
-                return result(false, true);
-            }
-
-            if (len === 3) {
-                // 20x === 20x
-                firstTwo = value.slice(0, 2);
-                currentFirstTwo = String(currentYear).slice(0, 2);
-                return result(false, firstTwo === currentFirstTwo);
-            }
-
-            if (len > 4) {
-                return result(false, false);
+            if (len !== 4) {
+                return resultWrapper(false, true);
             }
 
             value = parseInt(value, 10);
-            twoDigitYear = Number(String(currentYear).substr(2, 2));
-
-            if (len === 2) {
-                valid = value >= twoDigitYear && value <= twoDigitYear + maxYear;
-            } else if (len === 4) {
-                valid = value >= currentYear && value <= currentYear + maxYear;
-            }
-
-            return result(valid, valid);
+            valid = value >= currentYear && value <= currentYear + expMaxLifetime;
+            return resultWrapper(valid, valid);
         };
     }
 );
diff --git a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/parse-date.js b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/parse-date.js
index 56c281a516e..119bfa69873 100644
--- a/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/parse-date.js
+++ b/app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/parse-date.js
@@ -20,7 +20,7 @@ define(
                 };
             }
 
-            len = value[0] === '0' || value.length > 5 || value.length === 4 || value.length === 3 ? 2 : 1;
+            len = (value[0] === '0' || value.length > 5 || value.length === 4 || value.length === 3) ? 2 : 1;
             month = value.substr(0, len);
 
             return {
-- 
GitLab