Skip to content

Commit

Permalink
Add setMoney method (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored May 14, 2018
1 parent 801390e commit e9e6cc9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Common/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ private function getMoney($amount = null)

if ($amount === null) {
return null;
} elseif ($amount instanceof Money) {
$money = $amount;
} elseif (is_integer($amount)) {
$money = new Money($amount, $currency);
} else {
Expand Down Expand Up @@ -401,6 +403,21 @@ public function setAmountInteger($value)
return $this->setParameter('amount', (int) $value);
}

/**
* Sets the payment amount as integer.
*
* @param Money $value
* @return $this
*/
public function setMoney(Money $value)
{
$currency = $value->getCurrency()->getCode();

$this->setCurrency($currency);

return $this->setParameter('amount', $value);
}

/**
* Get the payment currency code.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Omnipay/Common/Message/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Omnipay\Common\Message;

use Mockery as m;
use Money\Currency;
use Money\Money;
use Omnipay\Common\CreditCard;
use Omnipay\Common\ItemBag;
use Omnipay\Tests\TestCase;
Expand Down Expand Up @@ -247,6 +249,18 @@ public function testAmountNegativeFloatThrowsException()
$this->request->getAmount();
}

public function testMoney()
{
$money = new Money(12345, new Currency('EUR'));
$this->assertSame($this->request, $this->request->setMoney($money));

$this->request->validate('amount', 'currency');

$this->assertSame('123.45', $this->request->getAmount());
$this->assertSame(12345, $this->request->getAmountInteger());
$this->assertSame('EUR', $this->request->getCurrency());
}

public function testCurrency()
{
$this->assertSame($this->request, $this->request->setCurrency('USD'));
Expand Down

0 comments on commit e9e6cc9

Please sign in to comment.