我有一个收集信用卡数据的应用程序。在将信息发送给支付实体之前,我试图确保输入的信息至少有效。我已经计算出卡号和cvv号码,但是我不确定到期日。我得到的信息格式是MMYY。所以我正在做的是:
-- Simple function to get current date and times
function getdatetime(tz)
local tz = tz or 'America/New_York';
local luatz = require 'luatz';
local function ts2tt(ts)
return luatz.timetable.new_from_timestamp(ts);
end
local utcnow = luatz.time();
local time_zone = luatz.get_tz(tz);
local datetime_raw = tostring(ts2tt(time_zone:localise(utcnow)));
local year, month, day, hour, min, sec, time_reminder = string.match(datetime_raw, "^(%d%d%d%d)%-(%d%d)%-(%d%d)[Tt](%d%d%.?%d*):(%d%d):(%d%d)()");
return year, month, day, hour, min, sec;
end
local current_year, current_month = getdatetime() -- Get current year/Month
local card_expiry_date = 'YYMM'; -- In the app this actually get a value eg: 2204, 2301, 2010, etc.
local card_exp_year = string.sub(card_expiry_date , 3, 4)
local card_exp_month = string.sub(card_expiry_date , 1, 2)
-- Extract the last two digits of the Year
current_year = string.sub(current_year , 3, 4)
-- Check month is valid
if(card_exp_month < '01' or card_exp_month > '12')then
print("This is not a valid month")
else
-- Check date is this month or after
if((card_exp_year < current_year) or (card_exp_year == current_year and card_exp_month < current_month))then
print("Date cannot be before this month.")
else
print("All is good.")
end
end
我不知道这是否是最优雅的解决方案,但是可以。但是它有一个巨大的错误:它将在本世纪末失效。由于我只知道到期日期年份的最后两位数字,例如,如果一张卡在2102年到期,而我们在2099年,我的逻辑将错误地拒绝该日期(02小于99)。
我非常清楚,到那时我可能不会再使用我的简单应用程序了,但是让我这样留下来却很麻烦。
任何人都可以提出正确的验证方法吗?
谢谢!
威尔玛
信用卡通常会在几年内过期。根据一些快速的网络搜索,平均为3年。同样,可以安全地假定只有一个世纪卡的所有者已经死亡,其卡帐户也是如此。
因此,当您在2099年获得02时,只有一个合理的选择。
计算两个差异并选择较小的一个。
类似于local expiresIn = math.min(math.abs(99-2), math.abs(99-102))