Skip to content

Commit 9fbb6ea

Browse files
committed
Pass amount as decimal instead of integer
1 parent 70e98ec commit 9fbb6ea

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,31 @@ 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("Please specify amount as a string or float, with decimal places (e.g. '10.00' to represent $10.00).");
172+
}
173+
174+
return number_format(
175+
$amount,
176+
$this->getCurrencyDecimalPlaces(),
177+
'.',
178+
''
179+
);
180+
}
167181
}
168182

169183
public function setAmount($value)
170184
{
171-
return $this->setParameter('amount', (int) $value);
185+
return $this->setParameter('amount', $value);
172186
}
173187

174-
public function getAmountDecimal()
188+
public function getAmountInteger()
175189
{
176-
return number_format(
177-
$this->getAmount() / $this->getCurrencyDecimalFactor(),
178-
$this->getCurrencyDecimalPlaces(),
179-
'.',
180-
''
181-
);
190+
return (int) round($this->getAmount() * $this->getCurrencyDecimalFactor());
182191
}
183192

184193
public function getCurrency()

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 53 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()

0 commit comments

Comments
 (0)