Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added card type detector #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace com\realexpayments\remote\sdk\utils;


use com\realexpayments\remote\sdk\domain\CardType;

class CardTypeDetector
{
private function isVisa(string $card) : bool
{
return preg_match("/^4[0-9]{0,15}$/i", $card);
}
private function isMasterCard(string $card) : bool
{
return preg_match("/^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$/i", $card);
}
private function isAmex(string $card) : bool
{
return preg_match("/^3$|^3[47][0-9]{0,13}$/i", $card);
}
private function isDiscover(string $card) : bool
{
return preg_match("/^6$|^6[05]$|^601[1]?$|^65[0-9][0-9]?$|^6(?:011|5[0-9]{2})[0-9]{0,12}$/i", $card);
}
private function isJCB(string $card) : bool
{
return preg_match("/^(?:2131|1800|35[0-9]{3})[0-9]{3,}$/i", $card);
}
private function isDinersClub(string $card) : bool
{
return preg_match("/^3(?:0[0-5]|[68][0-9])[0-9]{4,}$/i", $card);
}
public static function detect(string $card) : string
{
$cardTypes = [
[
'code' => CardType::VISA,
'methodName' => 'isVisa'
],
[
'code' => CardType::MASTERCARD,
'methodName' => 'isMasterCard'
],
[
'code' => CardType::AMEX,
'methodName' => 'isAmex'
],
[
'code' => CardType::CB,
'methodName' => 'isDiscover'
],
[
'code' => CardType::JCB,
'methodName' => 'isJCB'
],
[
'code' => CardType::DINERS,
'methodName' => 'isDinersClub'
]
];
foreach ($cardTypes as $cardType) {
$method = $cardType['methodName'];
if (self::$method($card)) {
return $cardType['code'];
}
}
return 'Invalid Card';
}

}