File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed
tests/Omnipay/Common/Message Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 ' ));
You can’t perform that action at this time.
0 commit comments