Skip to content

Commit

Permalink
Merge pull request #2 from mcred/hotfix/invalid-card-type
Browse files Browse the repository at this point in the history
Hotfix/invalid card type
  • Loading branch information
mcred authored Oct 24, 2017
2 parents 4d32c0e + f74f4ee commit dd2b2f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private function isVisa(string $card) : bool

private function isMasterCard(string $card) : bool
{
return preg_match("/^5$|^5[1-5][0-9]{0,14}$/i", $card);
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
Expand All @@ -23,13 +23,25 @@ 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 function detect(string $card) : string
{
$cardTypes = [
'Visa',
'MasterCard',
'Amex',
'Discover'
'MasterCard',
'Discover',
'JCB',
'DinersClub'
];
foreach ($cardTypes as $cardType) {
$method = 'is' . $cardType;
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ public function setup()
$this->cards = [
'Visa' => '4111111111111111',
'MasterCard' => '5555555555554444',
'MasterCard1' => '2221000000000009',
'MasterCard2' => '2223000048400011',
'MasterCard3' => '2223016768738313',
'MasterCard4' => '5105510551055105',
'Discover' => '6011111111111117',
'Amex' => '378282246310005',
'Amex1' => '371449635398431',
'AmexCorp' => '378734493671000',
'JCB' => '3530111333300000',
'JCB1' => '3566002020360505',
'Diners' => '30569309025904',
'Diners1' => '38520000023237',
'Invalid' => '7877787778777877'
];
}
Expand All @@ -29,6 +39,10 @@ public function testCanInstantiateAssembler()
public function testIsMasterCard()
{
$this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard']));
$this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard1']));
$this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard2']));
$this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard3']));
$this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard4']));
}

public function testIsDiscover()
Expand All @@ -39,10 +53,24 @@ public function testIsDiscover()
public function testIsAmex()
{
$this->assertEquals('Amex', $this->Detector->detect($this->cards['Amex']));
$this->assertEquals('Amex', $this->Detector->detect($this->cards['Amex1']));
$this->assertEquals('Amex', $this->Detector->detect($this->cards['AmexCorp']));
}

public function testIsInvalid()
{
$this->assertEquals('Invalid Card', $this->Detector->detect($this->cards['Invalid']));
}

public function testIsJCB()
{
$this->assertEquals('JCB', $this->Detector->detect($this->cards['JCB']));
$this->assertEquals('JCB', $this->Detector->detect($this->cards['JCB1']));
}

public function testIsDiners()
{
$this->assertEquals('DinersClub', $this->Detector->detect($this->cards['Diners']));
$this->assertEquals('DinersClub', $this->Detector->detect($this->cards['Diners1']));
}
}

0 comments on commit dd2b2f8

Please sign in to comment.