Skip to content

Commit

Permalink
Ensure comma not formatted into amount
Browse files Browse the repository at this point in the history
Probably the function doesn't add anything as params['amount']
should be correctly formatted - however,
it shouldn't add a comma - per
eileenmcnaughton/nz.co.fuzion.omnipaymultiprocessor#227
  • Loading branch information
eileenmcnaughton committed May 5, 2022
1 parent 62937e7 commit c372344
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions CRM/Core/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1155,20 +1155,20 @@ protected function getCurrency($params = []) {
}

/**
* Legacy. Better for a method to work on its own PropertyBag,
* but also, this function does not do very much.
* Get the submitted amount, padded to 2 decimal places, if needed.
*
* @param array $params
*
* @return string
* @throws \CRM_Core_Exception
*/
protected function getAmount($params = []) {
if (!CRM_Utils_Rule::numeric($params['amount'])) {
CRM_Core_Error::deprecatedWarning('Passing Amount value that is not numeric is deprecated please report this in gitlab');
return CRM_Utils_Money::formatUSLocaleNumericRounded(filter_var($params['amount'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 2);
}
return CRM_Utils_Money::formatUSLocaleNumericRounded($params['amount'], 2);
// Amount is already formatted to a machine-friendly format but may NOT have
// decimal places - eg. it could be 1000.1 so this would return 1000.10.
return Civi::format()->moneyNumber($params['amount'], 'USD', 'en_US');
}

/**
Expand Down
22 changes: 20 additions & 2 deletions Civi/Core/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,26 @@ public function moneyNumber($amount, string $currency, $locale): string {
return '';
}
$formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter::DECIMAL);
$money = Money::of($amount, $currency, NULL, RoundingMode::HALF_UP);
return $money->formatWith($formatter);
return Money::of($amount, $currency, NULL, RoundingMode::HALF_UP)->formatWith($formatter);
}

/**
* Get a number formatted to a machine format with decimal placed padded by currency.
*
* Most currencies format to 2 decimal places so the default of 'USD' will
* achieve that.
*
* For example an input of 1000.1 will return 1000.10.
*
* This will ensure that
*
* @param string|float|int $amount
* @param string $currency
*
* @return string
*/
public function machineNumber($amount, string $currency = 'USD'): string {
return Civi::format()->moneyNumber($amount, $currency, 'en_US');
}

/**
Expand Down

0 comments on commit c372344

Please sign in to comment.