Skip to content

Commit c29d0bb

Browse files
author
Stanislav Idolov
committed
MAGETWO-38647: credit card validation issue
-- fixes after CR
1 parent ff1786c commit c29d0bb

File tree

8 files changed

+66
-67
lines changed

8 files changed

+66
-67
lines changed

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define(
1313
function (utils, luhn10, creditCardTypes) {
1414
'use strict';
1515

16-
function result(card, isPotentiallyValid, isValid) {
16+
function resultWrapper(card, isPotentiallyValid, isValid) {
1717
return {
1818
card: card,
1919
isValid: isValid,
@@ -29,21 +29,21 @@ define(
2929
maxLength;
3030

3131
if (utils.isEmpty(value)) {
32-
return result(null, false, false);
32+
return resultWrapper(null, false, false);
3333
}
3434

3535
value = value.replace(/\-|\s/g, '');
3636

3737
if (!/^\d*$/.test(value)) {
38-
return result(null, false, false);
38+
return resultWrapper(null, false, false);
3939
}
4040

4141
potentialTypes = creditCardTypes.getCardTypes(value);
4242

4343
if (potentialTypes.length === 0) {
44-
return result(null, false, false);
44+
return resultWrapper(null, false, false);
4545
} else if (potentialTypes.length !== 1) {
46-
return result(null, true, false);
46+
return resultWrapper(null, true, false);
4747
}
4848

4949
cardType = potentialTypes[0];
@@ -56,17 +56,17 @@ define(
5656

5757
for (i = 0; i < cardType.lengths.length; i++) {
5858
if (cardType.lengths[i] === value.length) {
59-
return result(cardType, valid, valid);
59+
return resultWrapper(cardType, valid, valid);
6060
}
6161
}
6262

6363
maxLength = Math.max.apply(null, cardType.lengths);
6464

6565
if (value.length < maxLength) {
66-
return result(cardType, true, false);
66+
return resultWrapper(cardType, true, false);
6767
}
6868

69-
return result(cardType, false, false);
69+
return resultWrapper(cardType, false, false);
7070
};
7171
}
7272
);

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/credit-card-type.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define(
1313
'use strict';
1414
var types = [
1515
{
16-
niceType: 'Visa',
16+
title: 'Visa',
1717
type: 'VI',
1818
pattern: '^4\\d*$',
1919
gaps: [4, 8, 12],
@@ -24,7 +24,7 @@ define(
2424
}
2525
},
2626
{
27-
niceType: 'MasterCard',
27+
title: 'MasterCard',
2828
type: 'MC',
2929
pattern: '^5([1-5]\\d*)?$',
3030
gaps: [4, 8, 12],
@@ -35,7 +35,7 @@ define(
3535
}
3636
},
3737
{
38-
niceType: 'American Express',
38+
title: 'American Express',
3939
type: 'AE',
4040
pattern: '^3([47]\\d*)?$',
4141
isAmex: true,
@@ -47,7 +47,7 @@ define(
4747
}
4848
},
4949
{
50-
niceType: 'Diners',
50+
title: 'Diners',
5151
type: 'DN',
5252
pattern: '^3((0([0-5]\\d*)?)|[689]\\d*)?$',
5353
gaps: [4, 10],
@@ -58,7 +58,7 @@ define(
5858
}
5959
},
6060
{
61-
niceType: 'Discover',
61+
title: 'Discover',
6262
type: 'DI',
6363
pattern: '^6(0|01|011\\d*|5\\d*|4|4[4-9]\\d*)?$',
6464
gaps: [4, 8, 12],
@@ -69,7 +69,7 @@ define(
6969
}
7070
},
7171
{
72-
niceType: 'JCB',
72+
title: 'JCB',
7373
type: 'JC',
7474
pattern: '^((2|21|213|2131\\d*)|(1|18|180|1800\\d*)|(3|35\\d*))$',
7575
gaps: [4, 8, 12],
@@ -80,7 +80,7 @@ define(
8080
}
8181
},
8282
{
83-
niceType: 'UnionPay',
83+
title: 'UnionPay',
8484
type: 'UN',
8585
pattern: '^6(2\\d*)?$',
8686
gaps: [4, 8, 12],
@@ -91,9 +91,24 @@ define(
9191
}
9292
},
9393
{
94-
niceType: 'Maestro',
94+
title: 'Maestro',
9595
type: 'SM',
96-
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}$))',
96+
pattern: '(^(5[0678])[0-9]{11,18}$)' +
97+
'|(^(6[^05])[0-9]{11,18}$)' +
98+
'|(^(601)[^1][0-9]{9,16}$)' +
99+
'|(^(6011)[0-9]{9,11}$)' +
100+
'|(^(6011)[0-9]{13,16}$)' +
101+
'|(^(65)[0-9]{11,13}$)' +
102+
'|(^(65)[0-9]{15,18}$)' +
103+
'|(^(49030)[2-9]([0-9]{10}$' +
104+
'|[0-9]{12,13}$))' +
105+
'|(^(49033)[5-9]([0-9]{10}$' +
106+
'|[0-9]{12,13}$))' +
107+
'|(^(49110)[1-2]([0-9]{10}$' +
108+
'|[0-9]{12,13}$))' +
109+
'|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))' +
110+
'|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))' +
111+
'|(^(4936)([0-9]{12}$|[0-9]{14,15}$))',
97112
gaps: [4, 8, 12],
98113
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
99114
code: {

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ define(
88
[],
99
function() {
1010
'use strict';
11-
return function(a,b,c,d,e) {
12-
for(d = +a[b = a.length-1], e=0; b--;) {
11+
/**
12+
* Luhn algorithm verification
13+
*/
14+
return function(a, b, c, d, e) {
15+
for(d = +a[b = a.length-1], e = 0; b--;) {
1316
c = +a[b];
1417
d += ++e % 2 ? 2 * c % 10 + (c > 4) : c;
1518
}

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/cvv-validator.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@ define(
99
function() {
1010
'use strict';
1111

12-
function result(isValid, isPotentiallyValid) {
12+
function resultWrapper(isValid, isPotentiallyValid) {
1313
return {
1414
isValid: isValid,
1515
isPotentiallyValid: isPotentiallyValid
1616
};
1717
}
1818

19+
/**
20+
* CVV number validation
21+
* validate digit count fot CVV code
22+
*/
1923
return function(value, maxLength) {
2024
var DEFAULT_LENGTH = 3;
2125
maxLength = maxLength || DEFAULT_LENGTH;
2226

2327
if (!/^\d*$/.test(value)) {
24-
return result(false, false);
28+
return resultWrapper(false, false);
2529
}
2630
if (value.length === maxLength) {
27-
return result(true, true);
31+
return resultWrapper(true, true);
2832
}
2933
if (value.length < maxLength) {
30-
return result(false, true);
34+
return resultWrapper(false, true);
3135
}
3236
if (value.length > maxLength) {
33-
return result(false, false);
37+
return resultWrapper(false, false);
3438
}
35-
36-
return result(true, true);
3739
};
3840
}
3941
);

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ define(
1414
function(utils, parseDate, expirationMonth, expirationYear) {
1515
'use strict';
1616

17-
function result(isValid, isPotentiallyValid, month, year) {
17+
function resultWrapper(isValid, isPotentiallyValid, month, year) {
1818
return {
1919
isValid: isValid,
2020
isPotentiallyValid: isPotentiallyValid,
@@ -29,7 +29,7 @@ define(
2929
yearValid;
3030

3131
if (utils.isEmpty(value)) {
32-
return result(false, false, null, null);
32+
return resultWrapper(false, false, null, null);
3333
}
3434

3535
value = value.replace(/^(\d\d) (\d\d(\d\d)?)$/, '$1/$2');
@@ -38,14 +38,14 @@ define(
3838
yearValid = expirationYear(date.year);
3939

4040
if (monthValid.isValid && yearValid.isValid) {
41-
return result(true, true, date.month, date.year);
41+
return resultWrapper(true, true, date.month, date.year);
4242
}
4343

4444
if (monthValid.isPotentiallyValid && yearValid.isPotentiallyValid) {
45-
return result(false, true, null, null);
45+
return resultWrapper(false, true, null, null);
4646
}
4747

48-
return result(false, false, null, null);
48+
return resultWrapper(false, false, null, null);
4949
}
5050
}
5151
);

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-month-validator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define(
99
function () {
1010
'use strict';
1111

12-
function result(isValid, isPotentiallyValid) {
12+
function resultWrapper(isValid, isPotentiallyValid) {
1313
return {
1414
isValid: isValid,
1515
isPotentiallyValid: isPotentiallyValid
@@ -21,21 +21,21 @@ define(
2121
monthValid;
2222

2323
if ((value.replace(/\s/g, '') === '') || (value === '0')) {
24-
return result(false, true);
24+
return resultWrapper(false, true);
2525
}
2626

2727
if (!/^\d*$/.test(value)) {
28-
return result(false, false);
28+
return resultWrapper(false, false);
2929
}
3030

3131
if (isNaN(value)) {
32-
return result(false, false);
32+
return resultWrapper(false, false);
3333
}
3434

3535
month = parseInt(value, 10);
3636
monthValid = month > 0 && month < 13;
3737

38-
return result(monthValid, monthValid);
38+
return resultWrapper(monthValid, monthValid);
3939
};
4040
}
4141
);

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/expiration-year-validator.js

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,34 @@ define(
99
function() {
1010
'use strict';
1111

12-
function result(isValid, isPotentiallyValid) {
12+
function resultWrapper(isValid, isPotentiallyValid) {
1313
return {
1414
isValid: isValid,
1515
isPotentiallyValid: isPotentiallyValid
1616
};
1717
}
1818

1919
return function(value) {
20-
var currentFirstTwo,
21-
currentYear = new Date().getFullYear(),
22-
firstTwo,
20+
var currentYear = new Date().getFullYear(),
2321
len = value.length,
24-
twoDigitYear,
2522
valid,
26-
maxYear = 19;
23+
expMaxLifetime = 19;
2724

2825
if (value.replace(/\s/g, '') === '') {
29-
return result(false, true);
26+
return resultWrapper(false, true);
3027
}
3128

3229
if (!/^\d*$/.test(value)) {
33-
return result(false, false);
30+
return resultWrapper(false, false);
3431
}
3532

36-
if (len < 2) {
37-
return result(false, true);
38-
}
39-
40-
if (len === 3) {
41-
// 20x === 20x
42-
firstTwo = value.slice(0, 2);
43-
currentFirstTwo = String(currentYear).slice(0, 2);
44-
return result(false, firstTwo === currentFirstTwo);
45-
}
46-
47-
if (len > 4) {
48-
return result(false, false);
33+
if (len !== 4) {
34+
return resultWrapper(false, true);
4935
}
5036

5137
value = parseInt(value, 10);
52-
twoDigitYear = Number(String(currentYear).substr(2, 2));
53-
54-
if (len === 2) {
55-
valid = value >= twoDigitYear && value <= twoDigitYear + maxYear;
56-
} else if (len === 4) {
57-
valid = value >= currentYear && value <= currentYear + maxYear;
58-
}
59-
60-
return result(valid, valid);
38+
valid = value >= currentYear && value <= currentYear + expMaxLifetime;
39+
return resultWrapper(valid, valid);
6140
};
6241
}
6342
);

app/code/Magento/Payment/view/frontend/web/js/model/credit-card-validation/expiration-date-validator/parse-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define(
2020
};
2121
}
2222

23-
len = value[0] === '0' || value.length > 5 || value.length === 4 || value.length === 3 ? 2 : 1;
23+
len = (value[0] === '0' || value.length > 5 || value.length === 4 || value.length === 3) ? 2 : 1;
2424
month = value.substr(0, len);
2525

2626
return {

0 commit comments

Comments
 (0)