Skip to content

Commit f7d4171

Browse files
authored
Merge pull request thephpleague#170 from anush/tracks
Support for card present track data
2 parents 37266c2 + 8706887 commit f7d4171

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/Message/AbstractRequest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,30 @@ protected function getCardData()
357357

358358
$data = array();
359359
$data['object'] = 'card';
360+
361+
// If track data is present, only return data relevant to a card present charge
362+
$tracks = $card->getTracks();
363+
$cvv = $card->getCvv();
364+
$postcode = $card->getPostcode();
365+
if (!empty($postcode)) {
366+
$data['address_zip'] = $postcode;
367+
}
368+
if (!empty($cvv)) {
369+
$data['cvc'] = $cvv;
370+
}
371+
if (!empty($tracks)) {
372+
$data['swipe_data'] = $tracks;
373+
return $data;
374+
}
375+
376+
// If we got here, it's a card not present transaction, so include everything we have
360377
$data['number'] = $card->getNumber();
361378
$data['exp_month'] = $card->getExpiryMonth();
362379
$data['exp_year'] = $card->getExpiryYear();
363-
if ($card->getCvv()) {
364-
$data['cvc'] = $card->getCvv();
365-
}
366380
$data['name'] = $card->getName();
367381
$data['address_line1'] = $card->getAddress1();
368382
$data['address_line2'] = $card->getAddress2();
369383
$data['address_city'] = $card->getCity();
370-
$data['address_zip'] = $card->getPostcode();
371384
$data['address_state'] = $card->getState();
372385
$data['address_country'] = $card->getCountry();
373386
$data['email'] = $card->getEmail();

tests/Message/AuthorizeRequestTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Omnipay\Stripe\Message;
44

5+
use Omnipay\Common\CreditCard;
56
use Omnipay\Common\ItemBag;
67
use Omnipay\Tests\TestCase;
78

@@ -107,6 +108,48 @@ public function testDataWithCard()
107108
$this->assertSame($card['number'], $data['source']['number']);
108109
}
109110

111+
public function testDataWithTracks()
112+
{
113+
$cardData = $this->getValidCard();
114+
$tracks = "%25B4242424242424242%5ETESTLAST%2FTESTFIRST%5E1505201425400714000000%3F";
115+
$cardData['tracks'] = $tracks;
116+
unset($cardData['cvv']);
117+
unset($cardData['billingPostcode']);
118+
$this->request->setCard(new CreditCard($cardData));
119+
$data = $this->request->getData();
120+
121+
$this->assertSame($tracks, $data['source']['swipe_data']);
122+
$this->assertCount(2, $data['source'], "Swipe data should be present. All other fields are not required");
123+
124+
// If there is any mismatch between the track data and the parsed data, Stripe rejects the transaction, so it's
125+
// best to suppress fields that is already present in the track data.
126+
$this->assertArrayNotHasKey('number', $data, 'Should not send card number for card present charge');
127+
$this->assertArrayNotHasKey('exp_month', $data, 'Should not send expiry month for card present charge');
128+
$this->assertArrayNotHasKey('exp_year', $data, 'Should not send expiry year for card present charge');
129+
$this->assertArrayNotHasKey('name', $data, 'Should not send name for card present charge');
130+
131+
// Billing address is not accepted for card present transactions.
132+
$this->assertArrayNotHasKey('address_line1', $data, 'Should not send billing address for card present charge');
133+
$this->assertArrayNotHasKey('address_line2', $data, 'Should not send billing address for card present charge');
134+
$this->assertArrayNotHasKey('address_city', $data, 'Should not send billing address for card present charge');
135+
$this->assertArrayNotHasKey('address_state', $data, 'Should not send billing address for card present charge');
136+
137+
}
138+
139+
public function testDataWithTracksAndZipCVVManuallyEntered()
140+
{
141+
$cardData = $this->getValidCard();
142+
$tracks = "%25B4242424242424242%5ETESTLAST%2FTESTFIRST%5E1505201425400714000000%3F";
143+
$cardData['tracks'] = $tracks;
144+
$this->request->setCard(new CreditCard($cardData));
145+
$data = $this->request->getData();
146+
147+
$this->assertSame($tracks, $data['source']['swipe_data']);
148+
$this->assertSame($cardData['cvv'], $data['source']['cvc']);
149+
$this->assertSame($cardData['billingPostcode'], $data['source']['address_zip']);
150+
$this->assertCount(4, $data['source'], "Swipe data, cvv and zip code should be present");
151+
}
152+
110153
public function testSendSuccess()
111154
{
112155
$this->setMockHttpResponse('PurchaseSuccess.txt');

0 commit comments

Comments
 (0)