设置并比较日期以检查信用卡的有效性

时间:2018-07-16 11:22:31

标签: javascript date time stripe-payments credit-card

我让客户注册了12个月的分期付款选项。我需要知道他们的信用卡至少在分期付款的整个有效期内是否有效12个月。

使用下面收到的对象中的card.exp_yearcard.exp_month,如果此信用卡从现在起一年后才有效,我将如何计算。我假设我必须使用Date()函数。

{
  "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
}

1 个答案:

答案 0 :(得分:0)

谢谢,卢卡!最终的解决方案!

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
}

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
}