Skip to content

Commit

Permalink
Issue thephpleague#89 Additional tests, docblocks, refactoring.
Browse files Browse the repository at this point in the history
Minor refactoring only done with tests written first.
  • Loading branch information
judgej committed Sep 12, 2017
1 parent adbbcf6 commit 7b929b4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 40 deletions.
99 changes: 65 additions & 34 deletions src/Message/DirectAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
/**
* Sage Pay Direct Authorize Request
*/

class DirectAuthorizeRequest extends AbstractRequest
{
protected $action = 'DEFERRED';
protected $service = 'vspdirect-register';

/**
* @var array Some mapping from Omnipay card brand codes to Sage Pay card branc codes.
*/
protected $cardBrandMap = array(
'mastercard' => 'mc',
'diners_club' => 'dc'
'mastercard' => 'MC',
'diners_club' => 'DC'
);

/**
* The required fields concerning what is being authorised and who
* it is being authorised for.
*
* @return array
*/
protected function getBaseAuthorizeData()
{
Expand Down Expand Up @@ -89,6 +95,8 @@ protected function getBaseAuthorizeData()
/**
* SagePay throws an error if passed an IPv6 address.
* Filter out addresses that are not IPv4 format.
*
* @return string|null The IPv4 IP addess string or null if not available in this format.
*/
public function getClientIp()
{
Expand Down Expand Up @@ -118,6 +126,9 @@ public function getCardholderName()
/**
* If a token or cardReference is being used, then include the details
* of the token in the data.
*
* @param array $data The data collected so far (to be added to).
* @return array
*/
public function getTokenData($data = array())
{
Expand Down Expand Up @@ -148,48 +159,63 @@ public function getTokenData($data = array())
}

/**
* Add the credit card or token details to the data.
* If a credit card is being used, then include the details
* of the card in the data.
*
* @param array $data The data collected so far (to be added to).
* @return array
*/
public function getData()
public function getCardData($data = array())
{
$data = $this->getBaseAuthorizeData();
// Validate the card details (number, date, cardholder name).
$this->getCard()->validate();

if ($this->getToken() || $this->getCardReference()) {
// If using a token, then set that data.
$data = $this->getTokenData($data);
if ($this->getCardholderName()) {
$data['CardHolder'] = $this->getCardholderName();
} else {
// Otherwise, a credit card has to have been provided.
$this->getCard()->validate();
$data['CardHolder'] = $this->getCard()->getName();
}

if ($this->getCardholderName()) {
$data['CardHolder'] = $this->getCardholderName();
} else {
$data['CardHolder'] = $this->getCard()->getName();
}
// Card number should not be provided if token is being provided instead
if (! $this->getToken()) {
$data['CardNumber'] = $this->getCard()->getNumber();
}

// Card number should not be provided if token is being provided instead
if (! $this->getToken()) {
$data['CardNumber'] = $this->getCard()->getNumber();
}
$data['ExpiryDate'] = $this->getCard()->getExpiryDate('my');
$data['CardType'] = $this->getCardBrand();

$data['ExpiryDate'] = $this->getCard()->getExpiryDate('my');
$data['CardType'] = $this->getCardBrand();
if ($this->getCard()->getStartMonth() and $this->getCard()->getStartYear()) {
$data['StartDate'] = $this->getCard()->getStartDate('my');
}

if ($this->getCard()->getStartMonth() and $this->getCard()->getStartYear()) {
$data['StartDate'] = $this->getCard()->getStartDate('my');
}
if ($this->getCard()->getIssueNumber()) {
$data['IssueNumber'] = $this->getCard()->getIssueNumber();
}

if ($this->getCard()->getIssueNumber()) {
$data['IssueNumber'] = $this->getCard()->getIssueNumber();
}
// If we want the card details to be saved on the gateway as a
// token or card reference, then request for that to be done.
$data['CreateToken'] = $this->getCreateToken();

// If we want the card details to be saved on the gateway as a
// token or card reference, then request for that to be done.
$data['CreateToken'] = $this->getCreateToken();
if ($this->getCard()->getCvv() !== null) {
$data['CV2'] = $this->getCard()->getCvv();
}

if ($this->getCard()->getCvv() !== null) {
$data['CV2'] = $this->getCard()->getCvv();
}
return $data;
}

/**
* Add the credit card or token details to the data.
*/
public function getData()
{
$data = $this->getBaseAuthorizeData();

if ($this->getToken() || $this->getCardReference()) {
// If using a token, then set that data.
$data = $this->getTokenData($data);
} else {
// Otherwise, a credit card has to have been provided.
$data = $this->getCardData($data);
}

// A CVV may be supplied whether using a token or credit card details.
Expand All @@ -205,15 +231,20 @@ public function getData()
return $data;
}

/**
* @return string Get the card brand in a format expected by Sage Pay.
*/
protected function getCardBrand()
{
$brand = $this->getCard()->getBrand();

// Some Omnipay-derived card brands will need mapping to new names.

if (isset($this->cardBrandMap[$brand])) {
return $this->cardBrandMap[$brand];
}

return $brand;
return strtoupper($brand);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Response extends AbstractResponse implements RedirectResponseInterface
* @var string
*/
const SAGEPAY_STATUS_OK = 'OK';
const SAGEPAY_STATUS_OK_REPEATED = 'OK REPEATED';
const SAGEPAY_STATUS_PENDING = 'PENDING';
const SAGEPAY_STATUS_NOTAUTHED = 'NOTAUTHED';
const SAGEPAY_STATUS_REJECTED = 'REJECTED';
Expand Down
2 changes: 2 additions & 0 deletions src/Message/ServerAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ServerAuthorizeRequest extends DirectAuthorizeRequest
/**
* Add the optional token details to the base data.
* The returnUrl is supported for legacy applications not using the notifyUrl.
*
* @return array
*/
public function getData()
{
Expand Down
8 changes: 5 additions & 3 deletions src/Message/ServerAuthorizeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public function isSuccessful()

public function isRedirect()
{
return isset($this->data['Status']) &&
in_array($this->data['Status'], array('OK', 'OK REPEATED'));
return in_array(
$this->getStatus(),
array(static::SAGEPAY_STATUS_OK, static::SAGEPAY_STATUS_OK_REPEATED)
);
}

public function getRedirectUrl()
{
return isset($this->data['NextURL']) ? $this->data['NextURL'] : null;
return $this->getDataItem('NextURL');
}

public function getRedirectMethod()
Expand Down
10 changes: 7 additions & 3 deletions tests/Message/DirectAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class DirectAuthorizeRequestTest extends TestCase
{
// VISA incurrs a surcharge of 2.5% when used.
const SURCHARGE_XML = '<surcharges><surcharge><paymentType>VISA</paymentType><percentage>2.50</percentage></surcharge></surcharges>';

/**
Expand Down Expand Up @@ -187,23 +188,26 @@ public function testGetDataVisa()
$this->request->getCard()->setNumber('4929000000006');
$data = $this->request->getData();

$this->assertSame('visa', $data['CardType']);
// The card type to be sent is always upper case.
$this->assertSame('VISA', $data['CardType']);
}

public function testGetDataMastercard()
{
$this->request->getCard()->setNumber('5404000000000001');
$data = $this->request->getData();

$this->assertSame('mc', $data['CardType']);
// The card type to be sent is always upper case.
$this->assertSame('MC', $data['CardType']);
}

public function testGetDataDinersClub()
{
$this->request->getCard()->setNumber('30569309025904');
$data = $this->request->getData();

$this->assertSame('dc', $data['CardType']);
// This card type does not involve any mapping.
$this->assertSame('DC', $data['CardType']);
}

public function testGetDataNullBillingAddress2()
Expand Down

0 comments on commit 7b929b4

Please sign in to comment.