-6

I have customers signing up for a 12 monthly installment option. I need to know if their credit card will be valid for at least the duration of the installment option exactly 12 months.

Using the card.exp_year and card.exp_month from the object received below, how would I work out if this credit card will be valid exactly 1 year from now. I assume I have to use the Date() function.

{
  "id": "tok_xxxxxxxx",
  "object": "token",
  "card": {
    "id": "card_xxxxxxxx",
    "object": "card",
    "address_city": null,
    "address_country": "BE",
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "CA",
    "cvc_check": "unchecked",
    "dynamic_last4": null,
    "exp_month": 7,
    "exp_year": 2019,
    "funding": "credit",
    "last4": "3086",
    "metadata": {},
    "name": "John Doe ",
    "tokenization_method": null
  },
  "client_ip": "149.xxx.xxx.xxx",
  "created": 1530xxxxxx,
  "livemode": true,
  "type": "card",
  "used": false
}
alemac852
  • 99
  • 1
  • 11
  • 2
    What have you tried? If you should do this on paper, how would you do in real life? – sjahan Jul 16 '18 at 11:24
  • Possible duplicate of [How do I get the current date in JavaScript?](https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript) – Saransh Kataria Jul 16 '18 at 11:25
  • take a date that's the current date with the year incremented once and compare the month and year of the card's expiry date – Luca Kiebel Jul 16 '18 at 11:25
  • @Luca good thinking, thanks for the direction, possibly saved me some time – alemac852 Jul 16 '18 at 11:59

1 Answers1

0

Thanks, Luca! Final working solution!

var cardYear = new Date();
cardYear.setMonth(12-1); // jan = 0 so minus 1
cardYear.setYear(2019);

console.log(cardYear);

var nextYearFromNow = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
console.log(nextYearFromNow);

console.log(compareTime(cardYear, nextYearFromNow));

function compareTime(time1, time2) {
    return new Date(time1) > new Date(time2); // true if time1 is later
}

or

var year = user.attributes.token.card.exp_year;
var month = user.attributes.token.card.exp_month;

var cardYear = new Date(year, month, 0); // returns date according to card and sets day to last day of month becuase credit cards are set to expire on the last day of a month
var nextYearFromNow = new Date(new Date().setFullYear(new Date().getFullYear() + 1));

var expiry = compareTime(cardYear, nextYearFromNow); // if false card is too short

function compareTime(time1, time2) {
    return new Date(time1) > new Date(time2); // true if time1 is later
}
alemac852
  • 99
  • 1
  • 11