Skip to content

Commit

Permalink
Expiry Date Validation Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Worldpay CSE Mobile Support committed Jan 29, 2016
1 parent 4ac68b2 commit 17f9554
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "worldpay-cse",
"version": "1.0.0",
"version": "1.0.1",
"description": "WorldPay Client Side Encryption client library",
"devDependencies": {
"grunt": "^0.4.5",
Expand Down
32 changes: 29 additions & 3 deletions unittest-js/cse-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,42 @@ QUnit.test("fails if year fails regex", function(assert) {
});

QUnit.test("fails if expiry date is before this month", function(assert) {
var now = new Date();
testHelper.expiryMonth.val(('0' + now.getMonth()).slice(-2)); //January = 0 so this will be last month, 0 padded
testHelper.expiryYear.val(now.getFullYear()); //will always be 4 digits anyway (until year 10000 when this test should be updated anyway :D)
testHelper.expiryMonth.val('12'); //January = 0 so this will be last month, 0 padded
testHelper.expiryYear.val('2015'); //will always be 4 digits anyway (until year 10000 when this test should be updated anyway :D)

testHelper.submit();

assert.notOk(testHelper.getEncryptedData(), "should fail if date is before this month");
assert.deepEqual(testHelper.getErrorCodes(), [306], "correct error code should be supplied");
});

// INC00329847 Start
QUnit.test("succeeds if expiryDate is 1-18 months in the future", function(assert) {
for (var monthsToAdd = 1; monthsToAdd < 19; monthsToAdd++) {
var now = new Date();
var currentMonth = now.getMonth() + 1; // Convert from zero based month
var futureMonth = currentMonth + monthsToAdd;
var year = now.getFullYear();
if (futureMonth > 12) {
futureMonth = futureMonth - 12;
year = year + 1;
}
if (futureMonth > 24) {
futureMonth = futureMonth - 24;
year = year + 2;
}
testHelper.expiryMonth.val(('0' + (futureMonth)).slice(-2));
testHelper.expiryYear.val(year);

testHelper.submit();

assert.ok(testHelper.getEncryptedData(), "should succeed if date is in the future");
assert.notOk(testHelper.getErrorCodes().length, "error codes should be empty");
assert.deepEqual(testHelper.getErrorCodes(), [], "error codes should be empty");
}
});
// INC00329847 End

QUnit.test("encrypts if expiry date is at the end of this month", function(assert) {
var now = new Date();
testHelper.expiryMonth.val(('0' + (now.getMonth() + 1)).slice(-2));
Expand Down
10 changes: 5 additions & 5 deletions worldpay-js/worldpay-cse.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ function validateDate(expiryMonth, expiryYear) {
}

function isFutureDate(expiryMonth, expiryYear) {
var date = new Date(), today = new Date();
date.setMonth(expiryMonth);
date.setFullYear(expiryYear);
date.setUTCMilliseconds(date.getUTCMilliseconds() - 1); //Expire just before midnight on the last day of the month
if (date>=today) { return true; } return false;
var now = new Date();
var month = now.getMonth() + 1; // Date are zero based in JavaScript e.g. Jan = 0. So we convert this here.
var year = now.getFullYear();
return expiryYear > year || (expiryYear == year && expiryMonth >= month);
}

function validateCardHolderName(cardHolderName) {
if(!isEmpty(cardHolderName)) {
if(evaluateRegex(cardHolderName, "^.{1,30}$")) {
Expand Down

0 comments on commit 17f9554

Please sign in to comment.