Skip to content

Commit 03e4b79

Browse files
committed
Clean up Sage Pay card brand mapping and add tests. Refs #114 Closes #93 Closes #91
1 parent 0908a1e commit 03e4b79

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/Omnipay/SagePay/Message/DirectAuthorizeRequest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
class DirectAuthorizeRequest extends AbstractRequest
1818
{
1919
protected $action = 'DEFERRED';
20+
protected $cardBrandMap = array(
21+
'mastercard' => 'mc',
22+
'diners_club' => 'dc'
23+
);
2024

2125
protected function getBaseAuthorizeData()
2226
{
@@ -62,23 +66,11 @@ public function getData()
6266
$data = $this->getBaseAuthorizeData();
6367
$this->getCard()->validate();
6468

65-
$cardType = $this->getCard()->getBrand();
66-
67-
// list of brands SagePay names differently
68-
$brands = array(
69-
'mastercard' => 'mc',
70-
'diners_club' => 'dc'
71-
);
72-
73-
if (isset($brands[$cardType])) {
74-
$cardType = $brands[$cardType];
75-
}
76-
7769
$data['CardHolder'] = $this->getCard()->getName();
7870
$data['CardNumber'] = $this->getCard()->getNumber();
7971
$data['CV2'] = $this->getCard()->getCvv();
8072
$data['ExpiryDate'] = $this->getCard()->getExpiryDate('my');
81-
$data['CardType'] = $cardType;
73+
$data['CardType'] = $this->getCardBrand();
8274

8375
if ($this->getCard()->getStartMonth() and $this->getCard()->getStartYear()) {
8476
$data['StartDate'] = $this->getCard()->getStartDate('my');
@@ -95,4 +87,15 @@ public function getService()
9587
{
9688
return 'vspdirect-register';
9789
}
90+
91+
protected function getCardBrand()
92+
{
93+
$brand = $this->getCard()->getBrand();
94+
95+
if (isset($this->cardBrandMap[$brand])) {
96+
return $this->cardBrandMap[$brand];
97+
}
98+
99+
return $brand;
100+
}
98101
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Omnipay package.
5+
*
6+
* (c) Adrian Macneil <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Omnipay\SagePay\Message;
13+
14+
use Omnipay\TestCase;
15+
16+
class DirectAuthorizeRequestTest extends TestCase
17+
{
18+
public function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->request = new DirectAuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
23+
$this->request->initialize(
24+
array(
25+
'amount' => '12.00',
26+
'transactionId' => '123',
27+
'card' => $this->getValidCard(),
28+
)
29+
);
30+
}
31+
32+
public function testGetDataVisa()
33+
{
34+
$this->request->getCard()->setNumber('4929000000006');
35+
$data = $this->request->getData();
36+
37+
$this->assertSame('visa', $data['CardType']);
38+
}
39+
40+
public function testGetDataMastercard()
41+
{
42+
$this->request->getCard()->setNumber('5404000000000001');
43+
$data = $this->request->getData();
44+
45+
$this->assertSame('mc', $data['CardType']);
46+
}
47+
48+
public function testGetDataDinersClub()
49+
{
50+
$this->request->getCard()->setNumber('30569309025904');
51+
$data = $this->request->getData();
52+
53+
$this->assertSame('dc', $data['CardType']);
54+
}
55+
}

0 commit comments

Comments
 (0)