Skip to content

Commit b90b693

Browse files
committed
Issue #112 check all mandatory fields are present before Form direct.
1 parent 56174cf commit b90b693

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/Message/AbstractRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function getAddressData($type = 'Billing')
114114
{
115115
$card = $this->getCard();
116116

117-
// Mapping is Sage Pay name => Omnipay Nname
117+
// Mapping is Sage Pay name => Omnipay Name
118118

119119
$mapping = [
120120
'Firstnames' => 'FirstName',

src/Message/DirectAuthorizeRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected function getBaseAuthorizeData()
4747
$data = $this->getBaseData();
4848

4949
$data['Description'] = $this->getDescription();
50+
5051
// Money formatted as major unit decimal.
5152
$data['Amount'] = $this->getAmount();
5253
$data['Currency'] = $this->getCurrency();

src/Message/Form/AuthorizeRequest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
*/
88

99
use Omnipay\SagePay\Message\DirectAuthorizeRequest;
10+
use Omnipay\Common\Exception\InvalidRequestException;
1011

1112
class AuthorizeRequest extends DirectAuthorizeRequest
1213
{
1314
/**
1415
* Fields accepted by the Form API.
15-
* "true" fields are mandatory.
16+
* "true" fields are mandatory, "false" fields are optional.
17+
* The DeliveryState is conditionally mandatory.
1618
*/
1719
protected $validFields = [
1820
'VendorTxCode' => true,
@@ -89,20 +91,43 @@ public function getCryptData()
8991

9092
$data = array_intersect_key($data, $this->validFields);
9193

92-
// TODO: throw exception if any mandatory fields are missing.
94+
// Throw exception if any mandatory fields are missing.
9395
// We need to catch it here before sending the user to an
94-
// unexpected on the gateway site.
96+
// generic (and useless) error on the gateway site.
97+
98+
foreach ($this->validFields as $fieldName => $mandatoryFlag) {
99+
if ($mandatoryFlag && ! isset($data[$fieldName])) {
100+
throw new InvalidRequestException(sprintf(
101+
'The %s parameter is required',
102+
$fieldName
103+
));
104+
}
105+
}
106+
107+
// Two conditional checks on the "state" fields.
108+
// We don't check if it is a valid two-character state code.
95109

96-
// ...
110+
if ($data['BillingCountry'] === 'US' && empty ($data['BillingState'])
111+
|| $data['DeliveryCountry'] === 'US' && empty ($data['DeliveryState'])
112+
) {
113+
throw new InvalidRequestException(
114+
'Missing state code for billing or shipping address'
115+
);
116+
}
97117

98118
return $data;
99119
}
100120

101121
/**
102122
* Add the Form-specific details to the base data.
123+
* @reurn array
103124
*/
104125
public function getData()
105126
{
127+
$this->validate('currency', 'description');
128+
129+
// The test mode is included to determine the redirect URL.
130+
106131
return [
107132
'VPSProtocol' => $this->VPSProtocol,
108133
'TxType' => $this->getTxType(),
@@ -114,6 +139,8 @@ public function getData()
114139

115140
/**
116141
* Generate the crypt field from the source data.
142+
* @param array $data the name/value pairs to be encrypted
143+
* @return string encrypted data
117144
*/
118145
public function generateCrypt(array $data)
119146
{

src/Message/Form/Response.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,36 @@ class Response extends AbstractResponse implements RedirectResponseInterface, Co
2020

2121
/**
2222
* Always a redirect, so not yet successful.
23+
* @return @inherit
2324
*/
2425
public function isSuccessful()
2526
{
2627
return false;
2728
}
2829

30+
/**
31+
* @return @inherit
32+
*/
2933
public function isRedirect()
3034
{
3135
return true;
3236
}
3337

38+
/**
39+
* @return @inherit
40+
*/
3441
public function getRedirectMethod()
3542
{
3643
return 'POST';
3744
}
3845

46+
/**
47+
* @return @inherit
48+
*/
3949
public function getRedirectData()
4050
{
51+
// Pull out just these four fields from the data supplied.
52+
4153
return array_intersect_key(
4254
$this->getData(),
4355
array_flip([
@@ -61,6 +73,9 @@ public function getRedirectUrl()
6173
return $this->liveEndpoint;
6274
}
6375

76+
/**
77+
* @return @inherit
78+
*/
6479
public function getTestMode()
6580
{
6681
$data = $this->getData();

tests/FormGatewayTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public function setUp()
1717
$this->getHttpRequest()
1818
);
1919
$this->gateway->setVendor('example');
20+
$this->gateway->setCurrency('EUR');
2021

2122
$this->purchaseOptions = array(
2223
'amount' => '10.00',
2324
'transactionId' => '123',
2425
'card' => $this->getValidCard(),
2526
'returnUrl' => 'https://www.example.com/return',
2627
'encryptionKey' => '12345678abcdeabc',
28+
'description' => 'Some message',
2729
);
2830

2931
$this->captureOptions = array(

0 commit comments

Comments
 (0)