Skip to content

Commit 7b1f5a3

Browse files
committed
Add back methods formatting currencies, using Money
1 parent 33f377c commit 7b1f5a3

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Common/Message/AbstractRequest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ protected function getCurrencies()
308308
* @return null|Money
309309
* @throws InvalidRequestException
310310
*/
311-
public function getMoney()
311+
public function getMoney($amount = null)
312312
{
313-
$amount = $this->getParameter('amount');
313+
$amount = $amount !== null ? $amount : $this->getParameter('amount');
314314

315315
if ($amount instanceof Money) {
316316
return $amount;
@@ -430,6 +430,36 @@ public function getCurrencyNumeric()
430430
}
431431
}
432432

433+
/**
434+
* Get the number of decimal places in the payment currency.
435+
*
436+
* @return integer
437+
*/
438+
public function getCurrencyDecimalPlaces()
439+
{
440+
if ($this->getCurrency()) {
441+
$currency = new Currency($this->getCurrency());
442+
if ($this->getCurrencies()->contains($currency)) {
443+
return $this->getCurrencies()->subunitFor($currency);
444+
}
445+
}
446+
447+
return 2;
448+
}
449+
450+
/**
451+
* Format an amount for the payment currency.
452+
*
453+
* @return string
454+
*/
455+
public function formatCurrency($amount)
456+
{
457+
$money = $this->getMoney($amount);
458+
$formatter = new DecimalMoneyFormatter($this->getCurrencies());
459+
460+
return $formatter->format($money);
461+
}
462+
433463
/**
434464
* Get the request description.
435465
*

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ public function testCurrencyNumericUnkown()
273273
$this->assertSame(null, $this->request->getCurrencyNumeric());
274274
}
275275

276+
public function testCurrencyDecimals()
277+
{
278+
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
279+
$this->assertSame(0, $this->request->getCurrencyDecimalPlaces());
280+
}
281+
public function testFormatCurrency()
282+
{
283+
$this->assertSame('1234.00', $this->request->formatCurrency(1234));
284+
}
285+
public function testFormatCurrencyNoDecimals()
286+
{
287+
$this->request->setCurrency('JPY');
288+
$this->assertSame('1234', $this->request->formatCurrency(1234));
289+
}
290+
276291
public function testDescription()
277292
{
278293
$this->assertSame($this->request, $this->request->setDescription('Cool product'));

0 commit comments

Comments
 (0)