Skip to content

Commit ac890e8

Browse files
authored
Allow to set amount as integer (#179)
* Allow to set amount as integer
1 parent cda8afc commit ac890e8

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/Common/Message/AbstractRequest.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,23 @@ protected function getCurrencies()
305305
}
306306

307307
/**
308+
* @param string|int|null $amount
308309
* @return null|Money
309310
* @throws InvalidRequestException
310311
*/
311312
private function getMoney($amount = null)
312313
{
314+
$currencyCode = $this->getCurrency() ?: 'USD';
315+
$currency = new Currency($currencyCode);
316+
313317
$amount = $amount !== null ? $amount : $this->getParameter('amount');
314318

315-
if ($amount !== null) {
319+
if ($amount === null) {
320+
return null;
321+
} elseif (is_integer($amount)) {
322+
$money = new Money($amount, $currency);
323+
} else {
316324
$moneyParser = new DecimalMoneyParser($this->getCurrencies());
317-
$currencyCode = $this->getCurrency() ?: 'USD';
318-
$currency = new Currency($currencyCode);
319325

320326
$number = Number::fromString($amount);
321327

@@ -327,19 +333,19 @@ private function getMoney($amount = null)
327333
}
328334

329335
$money = $moneyParser->parse((string) $number, $currency);
336+
}
330337

331-
// Check for a negative amount.
332-
if (!$this->negativeAmountAllowed && $money->isNegative()) {
333-
throw new InvalidRequestException('A negative amount is not allowed.');
334-
}
335-
336-
// Check for a zero amount.
337-
if (!$this->zeroAmountAllowed && $money->isZero()) {
338-
throw new InvalidRequestException('A zero amount is not allowed.');
339-
}
338+
// Check for a negative amount.
339+
if (!$this->negativeAmountAllowed && $money->isNegative()) {
340+
throw new InvalidRequestException('A negative amount is not allowed.');
341+
}
340342

341-
return $money;
343+
// Check for a zero amount.
344+
if (!$this->zeroAmountAllowed && $money->isZero()) {
345+
throw new InvalidRequestException('A zero amount is not allowed.');
342346
}
347+
348+
return $money;
343349
}
344350

345351
/**
@@ -362,12 +368,12 @@ public function getAmount()
362368
/**
363369
* Sets the payment amount.
364370
*
365-
* @param string $value
371+
* @param string|null $value
366372
* @return AbstractRequest Provides a fluent interface
367373
*/
368374
public function setAmount($value)
369375
{
370-
return $this->setParameter('amount', $value);
376+
return $this->setParameter('amount', $value !== null ? (string) $value : null);
371377
}
372378

373379
/**
@@ -384,6 +390,17 @@ public function getAmountInteger()
384390
}
385391
}
386392

393+
/**
394+
* Sets the payment amount as integer.
395+
*
396+
* @param int $value
397+
* @return AbstractRequest Provides a fluent interface
398+
*/
399+
public function setAmountInteger($value)
400+
{
401+
return $this->setParameter('amount', (int) $value);
402+
}
403+
387404
/**
388405
* Get the payment currency code.
389406
*
@@ -446,11 +463,12 @@ public function getCurrencyDecimalPlaces()
446463
/**
447464
* Format an amount for the payment currency.
448465
*
466+
* @param string $amount
449467
* @return string
450468
*/
451469
public function formatCurrency($amount)
452470
{
453-
$money = $this->getMoney($amount);
471+
$money = $this->getMoney((string) $amount);
454472
$formatter = new DecimalMoneyFormatter($this->getCurrencies());
455473

456474
return $formatter->format($money);

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ public function testGetAmountInteger()
197197
$this->assertSame(1366, $this->request->getAmountInteger());
198198
}
199199

200+
public function testSetAmountInteger()
201+
{
202+
$this->assertSame($this->request, $this->request->setAmountInteger(1366));
203+
$this->assertSame(1366, $this->request->getAmountInteger());
204+
$this->assertSame('13.66', $this->request->getAmount());
205+
}
206+
200207
public function testGetAmountIntegerNoDecimals()
201208
{
202209
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
@@ -410,6 +417,13 @@ public function testCanValidateExistingParameters()
410417
$this->assertNull($this->request->validate('testMode', 'token'));
411418
}
412419

420+
public function testCanValidateAmountInteger()
421+
{
422+
$this->request->setAmountInteger(1);
423+
424+
$this->assertNull($this->request->validate('amount'));
425+
}
426+
413427
/**
414428
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
415429
*/

0 commit comments

Comments
 (0)