Skip to content

Commit 0d84af5

Browse files
authored
Merge pull request #81 from aimeos/master
Check for valid credit card object
2 parents 731386e + 63050cd commit 0d84af5

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/Omnipay/Common/AbstractGateway.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Guzzle\Http\Client as HttpClient;
1010
use Symfony\Component\HttpFoundation\ParameterBag;
1111
use Symfony\Component\HttpFoundation\Request as HttpRequest;
12+
use Omnipay\Common\Exception\RuntimeException;
1213

1314
/**
1415
* Base payment gateway class
@@ -138,6 +139,10 @@ public function getParameter($key)
138139
*/
139140
public function setParameter($key, $value)
140141
{
142+
if ($value !== null && !is_scalar($value)) {
143+
throw new RuntimeException('Only scalar values are allowed for parameters');
144+
}
145+
141146
$this->parameters->set($key, $value);
142147

143148
return $this;

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ protected function setParameter($key, $value)
170170
throw new RuntimeException('Request cannot be modified after it has been sent!');
171171
}
172172

173+
if (null !== $value && !is_scalar($value)) {
174+
throw new RuntimeException('Only scalar values are allowed for parameters');
175+
}
176+
173177
$this->parameters->set($key, $value);
174178

175179
return $this;
@@ -222,7 +226,13 @@ public function validate()
222226
*/
223227
public function getCard()
224228
{
225-
return $this->getParameter('card');
229+
$card = $this->getParameter('card');
230+
231+
if (!$card instanceof CreditCard) {
232+
return new CreditCard();
233+
}
234+
235+
return $card;
226236
}
227237

228238
/**
@@ -237,7 +247,13 @@ public function setCard($value)
237247
$value = new CreditCard($value);
238248
}
239249

240-
return $this->setParameter('card', $value);
250+
if (null !== $this->response) {
251+
throw new RuntimeException('Request cannot be modified after it has been sent!');
252+
}
253+
254+
$this->parameters->set('card', $value);
255+
256+
return $this;
241257
}
242258

243259
/**
@@ -522,7 +538,13 @@ public function setItems($items)
522538
$items = new ItemBag($items);
523539
}
524540

525-
return $this->setParameter('items', $items);
541+
if (null !== $this->response) {
542+
throw new RuntimeException('Request cannot be modified after it has been sent!');
543+
}
544+
545+
$this->parameters->set('items', $items);
546+
547+
return $this;
526548
}
527549

528550
/**

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public function testCard()
6363
$this->assertSame($card, $this->request->getCard());
6464
}
6565

66+
public function testGetCard()
67+
{
68+
$this->assertInstanceOf('\Omnipay\Common\CreditCard', $this->request->getCard());
69+
}
70+
6671
public function testSetCardWithArray()
6772
{
6873
// passing array should create CreditCard object
@@ -73,6 +78,15 @@ public function testSetCardWithArray()
7378
$this->assertSame('1234', $card->getNumber());
7479
}
7580

81+
public function testSetCardAfterRequestSent()
82+
{
83+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
84+
$this->request->send();
85+
86+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
87+
$this->request->setCard(array('number' => '1234'));
88+
}
89+
7690
public function testToken()
7791
{
7892
$this->assertSame($this->request, $this->request->setToken('12345'));
@@ -335,6 +349,18 @@ public function testItemsBag()
335349
$this->assertSame($itemBag, $this->request->getItems());
336350
}
337351

352+
public function testSetItemsBagAfterRequestSent()
353+
{
354+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
355+
$this->request->send();
356+
357+
$itemBag = new ItemBag;
358+
$itemBag->add(array('name' => 'Floppy Disk'));
359+
360+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
361+
$this->request->setItems($itemBag);
362+
}
363+
338364
public function testClientIp()
339365
{
340366
$this->assertSame($this->request, $this->request->setClientIp('127.0.0.1'));
@@ -392,6 +418,12 @@ public function testGetParameters()
392418
$this->assertEquals($expected, $this->request->getParameters());
393419
}
394420

421+
public function testSetParameterObject()
422+
{
423+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
424+
$this->request->setParameter('test', new \stdClass());
425+
}
426+
395427
/**
396428
* @expectedException \Omnipay\Common\Exception\RuntimeException
397429
* @expectedExceptionMessage Request cannot be modified after it has been sent!

0 commit comments

Comments
 (0)