Skip to content

Commit ac1ad3b

Browse files
committed
Helper methods to parse out track data
1 parent 0ee3cc4 commit ac1ad3b

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Omnipay/Common/CreditCard.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,40 @@ public function getTracks()
555555
return $this->getParameter('tracks');
556556
}
557557

558+
/**
559+
* Get raw data for track 1 on the credit card magnetic strip.
560+
*
561+
* @return string
562+
*/
563+
public function getTrack1()
564+
{
565+
$track1 = null;
566+
if ($tracks = $this->getTracks()) {
567+
$pattern = '/\%B\d{1,19}\^.{2,26}\^\d{4}\d*\?/';
568+
if (preg_match($pattern, $tracks, $matches) === 1) {
569+
$track1 = $matches[0];
570+
}
571+
}
572+
return $track1;
573+
}
574+
575+
/**
576+
* Get raw data for track 2 on the credit card magnetic strip.
577+
*
578+
* @return string
579+
*/
580+
public function getTrack2()
581+
{
582+
$track2 = null;
583+
if ($tracks = $this->getTracks()) {
584+
$pattern = '/;\d{1,19}=\d{4}\d*\?/';
585+
if (preg_match($pattern, $tracks, $matches) === 1) {
586+
$track2 = $matches[0];
587+
}
588+
}
589+
return $track2;
590+
}
591+
558592
/**
559593
* Sets raw data from all tracks on the credit card magnetic strip. Used by gateways that support card-present
560594
* transactions.

tests/Omnipay/Common/CreditCardTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class CreditCardTest extends TestCase
88
{
9+
/** @var CreditCard */
10+
private $card;
11+
912
public function setUp()
1013
{
1114
$this->card = new CreditCard;
@@ -311,8 +314,22 @@ public function testCvv()
311314

312315
public function testTracks()
313316
{
314-
$this->card->setTracks('%25B4242424242424242%5ETESTLAST%2FTESTFIRST%5E1505201425400714000000%3F%3B4242424242424242%3D150520142547140130%3F');
315-
$this->assertSame('%25B4242424242424242%5ETESTLAST%2FTESTFIRST%5E1505201425400714000000%3F%3B4242424242424242%3D150520142547140130%3F', $this->card->getTracks());
317+
$this->card->setTracks('%B4242424242424242^SMITH/JOHN ^1520126100000000000000444000000?;4242424242424242=15201269999944401?');
318+
$this->assertSame('%B4242424242424242^SMITH/JOHN ^1520126100000000000000444000000?;4242424242424242=15201269999944401?', $this->card->getTracks());
319+
}
320+
321+
public function testShouldReturnTrack1()
322+
{
323+
$this->card->setTracks('%B4242424242424242^SMITH/JOHN ^1520126100000000000000444000000?;4242424242424242=15201269999944401?');
324+
$actual = $this->card->getTrack1();
325+
$this->assertEquals('%B4242424242424242^SMITH/JOHN ^1520126100000000000000444000000?', $actual);
326+
}
327+
328+
public function testShouldReturnTrack2()
329+
{
330+
$this->card->setTracks('%B4242424242424242^SMITH/JOHN ^1520126100000000000000444000000?;4242424242424242=15201269999944401?');
331+
$actual = $this->card->getTrack2();
332+
$this->assertEquals(';4242424242424242=15201269999944401?', $actual);
316333
}
317334

318335
public function testIssueNumber()

0 commit comments

Comments
 (0)