From 17f95548e0430d15a53730246cd133e0821a6032 Mon Sep 17 00:00:00 2001 From: Worldpay CSE Mobile Support Date: Fri, 29 Jan 2016 11:03:44 +0000 Subject: [PATCH] Expiry Date Validation Fix --- package.json | 2 +- unittest-js/cse-tests.js | 32 +++++++++++++++++++++++++++++--- worldpay-js/worldpay-cse.js | 10 +++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index edcc9ef..3613612 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/unittest-js/cse-tests.js b/unittest-js/cse-tests.js index b8a4a45..30806d1 100644 --- a/unittest-js/cse-tests.js +++ b/unittest-js/cse-tests.js @@ -159,9 +159,8 @@ 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(); @@ -169,6 +168,33 @@ QUnit.test("fails if expiry date is before this month", function(assert) { 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)); diff --git a/worldpay-js/worldpay-cse.js b/worldpay-js/worldpay-cse.js index 0171136..b541052 100644 --- a/worldpay-js/worldpay-cse.js +++ b/worldpay-js/worldpay-cse.js @@ -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}$")) {