Skip to content

Commit 27c8518

Browse files
author
Vladislav Veselinov
committed
Merge remote-tracking branch 'upstream/master'
2 parents c7348ee + 36cbf70 commit 27c8518

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,34 @@ public function setCardReference($value)
163163

164164
public function getAmount()
165165
{
166-
return $this->getParameter('amount');
166+
$amount = $this->getParameter('amount');
167+
if ($amount) {
168+
if (!is_float($amount) &&
169+
$this->getCurrencyDecimalPlaces() > 0 &&
170+
false === strpos((string) $amount, '.')) {
171+
throw new InvalidRequestException(
172+
'Please specify amount as a string or float, ' .
173+
'with decimal places (e.g. \'10.00\' to represent $10.00).'
174+
);
175+
}
176+
177+
return number_format(
178+
$amount,
179+
$this->getCurrencyDecimalPlaces(),
180+
'.',
181+
''
182+
);
183+
}
167184
}
168185

169186
public function setAmount($value)
170187
{
171-
return $this->setParameter('amount', (int) $value);
188+
return $this->setParameter('amount', $value);
172189
}
173190

174-
public function getAmountDecimal()
191+
public function getAmountInteger()
175192
{
176-
return number_format(
177-
$this->getAmount() / $this->getCurrencyDecimalFactor(),
178-
$this->getCurrencyDecimalPlaces(),
179-
'.',
180-
''
181-
);
193+
return (int) round($this->getAmount() * $this->getCurrencyDecimalFactor());
182194
}
183195

184196
public function getCurrency()

src/Omnipay/Common/Message/AbstractResponse.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ public function getTransactionReference()
6767
* redirection page, just call the getRedirectUrl() and getRedirectData() methods directly.
6868
*/
6969
public function redirect()
70+
{
71+
$this->getRedirectResponse()->send();
72+
exit;
73+
}
74+
75+
public function getRedirectResponse()
7076
{
7177
if (!$this instanceof RedirectResponseInterface || !$this->isRedirect()) {
7278
throw new RuntimeException('This response does not support redirection.');
7379
}
7480

7581
if ('GET' === $this->getRedirectMethod()) {
76-
HttpRedirectResponse::create($this->getRedirectUrl())->send();
77-
exit;
82+
return HttpRedirectResponse::create($this->getRedirectUrl());
7883
} elseif ('POST' === $this->getRedirectMethod()) {
7984
$hiddenFields = '';
8085
foreach ($this->getRedirectData() as $key => $value) {
@@ -102,8 +107,7 @@ public function redirect()
102107
</html>';
103108
$output = sprintf($output, htmlspecialchars($this->getRedirectUrl(), ENT_QUOTES, 'UTF-8'), $hiddenFields);
104109

105-
HttpResponse::create($output)->send();
106-
exit;
110+
return HttpResponse::create($output);
107111
}
108112

109113
throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".');

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function setUp()
2525

2626
public function testInitializeWithParams()
2727
{
28-
$this->assertSame($this->request, $this->request->initialize(array('amount' => 123)));
29-
$this->assertSame(123, $this->request->getAmount());
28+
$this->assertSame($this->request, $this->request->initialize(array('amount' => '1.23')));
29+
$this->assertSame('1.23', $this->request->getAmount());
3030
}
3131

3232
public function testCard()
@@ -61,27 +61,67 @@ public function testCardReference()
6161

6262
public function testAmount()
6363
{
64-
$this->assertSame($this->request, $this->request->setAmount(200));
65-
$this->assertSame(200, $this->request->getAmount());
64+
$this->assertSame($this->request, $this->request->setAmount('2.00'));
65+
$this->assertSame('2.00', $this->request->getAmount());
6666
}
6767

68-
public function testAmountCastsToInteger()
68+
public function testAmountWithFloat()
6969
{
70-
$this->assertSame($this->request, $this->request->setAmount('6.1'));
71-
$this->assertSame(6, $this->request->getAmount());
70+
$this->assertSame($this->request, $this->request->setAmount(2.0));
71+
$this->assertSame('2.00', $this->request->getAmount());
7272
}
7373

74-
public function testGetAmountDecimal()
74+
public function testAmountWithEmpty()
7575
{
76-
$this->assertSame($this->request, $this->request->setAmount(1366));
77-
$this->assertSame('13.66', $this->request->getAmountDecimal());
76+
$this->assertSame($this->request, $this->request->setAmount(null));
77+
$this->assertSame(null, $this->request->getAmount());
7878
}
7979

80-
public function testGetAmountDecimalNoDecimals()
80+
public function testGetAmountNoDecimals()
8181
{
8282
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
83-
$this->assertSame($this->request, $this->request->setAmount(1366));
84-
$this->assertSame('1366', $this->request->getAmountDecimal());
83+
$this->assertSame($this->request, $this->request->setAmount('1366'));
84+
$this->assertSame('1366', $this->request->getAmount());
85+
}
86+
87+
public function testGetAmountNoDecimalsRounding()
88+
{
89+
$this->assertSame($this->request, $this->request->setAmount('136.5'));
90+
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
91+
$this->assertSame('137', $this->request->getAmount());
92+
}
93+
94+
/**
95+
* @expectedException Omnipay\Common\Exception\InvalidRequestException
96+
*/
97+
public function testAmountWithIntThrowsException()
98+
{
99+
// ambiguous value, avoid errors upgrading from v0.9
100+
$this->assertSame($this->request, $this->request->setAmount(10));
101+
$this->request->getAmount();
102+
}
103+
104+
/**
105+
* @expectedException Omnipay\Common\Exception\InvalidRequestException
106+
*/
107+
public function testAmountWithIntStringThrowsException()
108+
{
109+
// ambiguous value, avoid errors upgrading from v0.9
110+
$this->assertSame($this->request, $this->request->setAmount('10'));
111+
$this->request->getAmount();
112+
}
113+
114+
public function testGetAmountInteger()
115+
{
116+
$this->assertSame($this->request, $this->request->setAmount('13.66'));
117+
$this->assertSame(1366, $this->request->getAmountInteger());
118+
}
119+
120+
public function testGetAmountIntegerNoDecimals()
121+
{
122+
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
123+
$this->assertSame($this->request, $this->request->setAmount('1366'));
124+
$this->assertSame(1366, $this->request->getAmountInteger());
85125
}
86126

87127
public function testCurrency()
@@ -143,4 +183,10 @@ public function testCancelUrl()
143183
$this->assertSame($this->request, $this->request->setCancelUrl('https://www.example.com/cancel'));
144184
$this->assertSame('https://www.example.com/cancel', $this->request->getCancelUrl());
145185
}
186+
187+
public function testNotifyUrl()
188+
{
189+
$this->assertSame($this->request, $this->request->setNotifyUrl('https://www.example.com/notify'));
190+
$this->assertSame('https://www.example.com/notify', $this->request->getNotifyUrl());
191+
}
146192
}

0 commit comments

Comments
 (0)