Skip to content

Commit

Permalink
Merge pull request #14 from anushr/card-present
Browse files Browse the repository at this point in the history
XOL-2799 Support for card present transactions
  • Loading branch information
rushi committed Mar 21, 2016
2 parents 25f19fc + ea42c0a commit e009033
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 27 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@
"homepage": "https://github.com/thephpleague/omnipay-authorizenet/contributors"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/xola/omnipay-common"
}
],
"autoload": {
"psr-4": { "Omnipay\\AuthorizeNet\\" : "src/" }
},
"require": {
"omnipay/common": "~2.4.2"
"omnipay/common": "~2.5"
},
"require-dev": {
"omnipay/tests": "~2.0"
Expand Down
6 changes: 6 additions & 0 deletions src/Message/AIMAbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,10 @@ protected function addTransactionSettings(\SimpleXMLElement $data)

return $data;
}

protected function isCardPresent()
{
// If the credit card has track data, then consider this a "card present" scenario
return ($card = $this->getCard()) && $card->getTracks();
}
}
26 changes: 23 additions & 3 deletions src/Message/AIMAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function getData()
$this->addPayment($data);
$this->addCustomerIP($data);
$this->addBillingData($data);
$this->addRetail($data);
$this->addTransactionSettings($data);

return $data;
Expand All @@ -30,9 +31,19 @@ protected function addPayment(\SimpleXMLElement $data)
/** @var CreditCard $card */
$card = $this->getCard();
$card->validate();
$data->transactionRequest->payment->creditCard->cardNumber = $card->getNumber();
$data->transactionRequest->payment->creditCard->expirationDate = $card->getExpiryDate('my');
$data->transactionRequest->payment->creditCard->cardCode = $card->getCvv();
if ($card->getTracks()) {
// Card present
if ($track1 = $card->getTrack1()) {
$data->transactionRequest->payment->trackData->track1 = $track1;
} elseif ($track2 = $card->getTrack2()) {
$data->transactionRequest->payment->trackData->track2 = $track2;
}
} else {
// Card not present
$data->transactionRequest->payment->creditCard->cardNumber = $card->getNumber();
$data->transactionRequest->payment->creditCard->expirationDate = $card->getExpiryDate('my');
$data->transactionRequest->payment->creditCard->cardCode = $card->getCvv();
}
}

protected function addCustomerIP(\SimpleXMLElement $data)
Expand All @@ -42,4 +53,13 @@ protected function addCustomerIP(\SimpleXMLElement $data)
$data->transactionRequest->customerIP = $ip;
}
}

protected function addRetail(\SimpleXMLElement $data)
{
if ($this->isCardPresent()) {
// Retail element is required for card present transactions
$data->transactionRequest->retail->marketType = 2;
$data->transactionRequest->retail->deviceType = 1;
}
}
}
45 changes: 28 additions & 17 deletions src/Message/CIMAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,40 @@ class CIMAuthorizeRequest extends AIMAuthorizeRequest
{
protected function addPayment(\SimpleXMLElement $data)
{
$this->validate('cardReference');

/** @var mixed $req */
$req = $data->transactionRequest;
/** @var CardReference $cardRef */
$cardRef = $this->getCardReference(false);
$req->profile->customerProfileId = $cardRef->getCustomerProfileId();
$req->profile->paymentProfile->paymentProfileId = $cardRef->getPaymentProfileId();
if ($shippingProfileId = $cardRef->getShippingProfileId()) {
$req->profile->shippingProfileId = $shippingProfileId;
}
if ($this->isCardPresent()) {
// Prefer the track data if present over the payment profile (better rate)
return parent::addPayment($data);

} else {
$this->validate('cardReference');

/** @var mixed $req */
$req = $data->transactionRequest;
/** @var CardReference $cardRef */
$cardRef = $this->getCardReference(false);
$req->profile->customerProfileId = $cardRef->getCustomerProfileId();
$req->profile->paymentProfile->paymentProfileId = $cardRef->getPaymentProfileId();
if ($shippingProfileId = $cardRef->getShippingProfileId()) {
$req->profile->shippingProfileId = $shippingProfileId;
}

$desc = $this->getDescription();
if (!empty($desc)) {
$req->order->description = $desc;
$desc = $this->getDescription();
if (!empty($desc)) {
$req->order->description = $desc;
}

return $data;
}

return $data;
}

protected function addBillingData(\SimpleXMLElement $data)
{
// Do nothing since billing information is already part of the customer profile
return $data;
if ($this->isCardPresent()) {
return parent::addBillingData($data);
} else {
// Do nothing since billing information is already part of the customer profile
return $data;
}
}
}
17 changes: 17 additions & 0 deletions tests/AIMGatewayIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,21 @@ public function testPurchaseRefundAutoVoid()
$response = $request->send();
$this->assertTrue($response->isSuccessful(), 'Automatic void should succeed');
}

public function testPurchaseCardPresent()
{
$amount = rand(10, 100) . '.' . rand(0, 99);
$card = array(
'number' => '4242424242424242',
'expiryMonth' => rand(1, 12),
'expiryYear' => gmdate('Y') + rand(1, 5),
'tracks' => '%B4242424242424242^SMITH/JOHN ^2511126100000000000000444000000?;4242424242424242=25111269999944401?'
);
$request = $this->gateway->purchase(array(
'amount' => $amount,
'card' => $card
));
$response = $request->send();
$this->assertTrue($response->isSuccessful());
}
}
42 changes: 42 additions & 0 deletions tests/Message/AIMAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function testGetData()
$setting = $data->transactionRequest->transactionSettings->setting[0];
$this->assertEquals('testRequest', $setting->settingName);
$this->assertEquals('false', $setting->settingValue);
$this->assertObjectNotHasAttribute('trackData', $data->transactionRequest->payment);
$this->assertObjectNotHasAttribute('retail', $data->transactionRequest);
}

public function testGetDataTestMode()
Expand All @@ -54,4 +56,44 @@ public function testShouldIncludeDuplicateWindowSetting()
$this->assertEquals('duplicateWindow', $setting->settingName);
$this->assertEquals('0', $setting->settingValue);
}

public function testGetDataCardPresentTrack1()
{
$card = $this->getValidCard();
$card['tracks'] = '%B4242424242424242^SMITH/JOHN ^2511126100000000000000444000000?;4242424242424242=25111269999944401?';
$this->request->initialize(array(
'amount' => '12.12',
'card' => $card
));

$data = $this->request->getData();

$this->assertEquals('12.12', $data->transactionRequest->amount);
$this->assertEquals(
'%B4242424242424242^SMITH/JOHN ^2511126100000000000000444000000?',
$data->transactionRequest->payment->trackData->track1);
$this->assertObjectNotHasAttribute('creditCard', $data->transactionRequest->payment);
$this->assertEquals('2', $data->transactionRequest->retail->marketType);
$this->assertEquals('1', $data->transactionRequest->retail->deviceType);
}

public function testGetDataCardPresentTrack2()
{
$card = $this->getValidCard();
$card['tracks'] = ';4242424242424242=25111269999944401?';
$this->request->initialize(array(
'amount' => '12.12',
'card' => $card
));

$data = $this->request->getData();

$this->assertEquals('12.12', $data->transactionRequest->amount);
$this->assertEquals(
';4242424242424242=25111269999944401?',
$data->transactionRequest->payment->trackData->track2);
$this->assertObjectNotHasAttribute('creditCard', $data->transactionRequest->payment);
$this->assertEquals('2', $data->transactionRequest->retail->marketType);
$this->assertEquals('1', $data->transactionRequest->retail->deviceType);
}
}
15 changes: 15 additions & 0 deletions tests/Message/CIMAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ public function testGetData()
$this->assertEquals('27057151', $data->transactionRequest->profile->shippingProfileId);
$this->assertEquals('Test authorize transaction', $data->transactionRequest->order->description);
}

public function testShouldUseTrackDataIfCardPresent()
{
$card = $this->getValidCard();
$card['tracks'] = '%B4242424242424242^SMITH/JOHN ^2511126100000000000000444000000?;4242424242424242=25111269999944401?';
$this->request->initialize(array(
'card' => $card,
'amount' => 21.00
));

$data = $this->request->getData();

$this->assertObjectNotHasAttribute('profile', $data->transactionRequest);
$this->assertObjectHasAttribute('trackData', $data->transactionRequest->payment);
}
}

0 comments on commit e009033

Please sign in to comment.