From 1a90e51d313e1f97a061fa3547e3b957abb74a9f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 4 May 2022 19:14:19 +1200 Subject: [PATCH] Ensure comma not formatted into amount Probably the function doesn't add anything as params['amount'] should be correctly formatted - however, it shouldn't add a comma - per https://github.com/eileenmcnaughton/nz.co.fuzion.omnipaymultiprocessor/issues/227 --- CRM/Core/Payment.php | 8 ++--- Civi/Core/Format.php | 30 +++++++++++++++++-- .../CRM/Core/Payment/PayflowProTest.php | 4 +-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 7658750b99..34d6363005 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -1155,20 +1155,20 @@ abstract class CRM_Core_Payment { } /** - * 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()->machineMoney($params['amount']); } /** diff --git a/Civi/Core/Format.php b/Civi/Core/Format.php index b3076d0e8d..2f127ee332 100644 --- a/Civi/Core/Format.php +++ b/Civi/Core/Format.php @@ -93,8 +93,34 @@ class Format { 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 padded decimal places. + * + * This is intended to be a machine-friendly format that is also suitable + * for sending out to other systems that might expect 2 digits after the + * decimal point. + * + * 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 + * + * @noinspection PhpDocMissingThrowsInspection + * @noinspection PhpUnhandledExceptionInspection + */ + public function machineMoney($amount, string $currency = 'USD'): string { + $formatter = $this->getMoneyFormatter($currency, 'en_US', NumberFormatter::DECIMAL, [NumberFormatter::GROUPING_USED => FALSE]); + return Money::of($amount, $currency, NULL, RoundingMode::HALF_UP)->formatWith($formatter); } /** diff --git a/ext/payflowpro/tests/phpunit/CRM/Core/Payment/PayflowProTest.php b/ext/payflowpro/tests/phpunit/CRM/Core/Payment/PayflowProTest.php index 2dae42df51..93445de4b8 100644 --- a/ext/payflowpro/tests/phpunit/CRM/Core/Payment/PayflowProTest.php +++ b/ext/payflowpro/tests/phpunit/CRM/Core/Payment/PayflowProTest.php @@ -48,7 +48,7 @@ class CRM_Core_Payment_PayflowProTest extends \PHPUnit\Framework\TestCase implem public function testSinglePayment(): void { $this->setupMockHandler(); $params = $this->getBillingParams(); - $params['amount'] = 20.00; + $params['amount'] = 1020.00; $params['currency'] = 'AUD'; $params['description'] = 'Test Contribution'; $params['invoiceID'] = 'xyz'; @@ -244,7 +244,7 @@ class CRM_Core_Payment_PayflowProTest extends \PHPUnit\Framework\TestCase implem */ public function getExpectedSinglePaymentRequests(): array { return [ - 'USER[4]=test&VENDOR[4]=test&PARTNER[6]=PayPal&PWD[8]=test1234&TENDER[1]=C&TRXTYPE[1]=S&ACCT[16]=4111111111111111&CVV2[3]=123&EXPDATE[4]=1022&ACCTTYPE[4]=Visa&AMT[5]=20.00&CURRENCY[3]=AUD&FIRSTNAME[4]=John&LASTNAME[8]=O\'Connor&STREET[16]=8 Hobbitton Road&CITY[9]=The+Shire&STATE[3]=NSW&ZIP[4]=5010&COUNTRY[3]=AUS&EMAIL[24]=unittesteway@civicrm.org&CUSTIP[9]=127.0.0.1&COMMENT1[4]=4200&COMMENT2[4]=live&INVNUM[3]=xyz&ORDERDESC[17]=Test+Contribution&VERBOSITY[6]=MEDIUM&BILLTOCOUNTRY[3]=AUS', + 'USER[4]=test&VENDOR[4]=test&PARTNER[6]=PayPal&PWD[8]=test1234&TENDER[1]=C&TRXTYPE[1]=S&ACCT[16]=4111111111111111&CVV2[3]=123&EXPDATE[4]=1022&ACCTTYPE[4]=Visa&AMT[7]=1020.00&CURRENCY[3]=AUD&FIRSTNAME[4]=John&LASTNAME[8]=O\'Connor&STREET[16]=8 Hobbitton Road&CITY[9]=The+Shire&STATE[3]=NSW&ZIP[4]=5010&COUNTRY[3]=AUS&EMAIL[24]=unittesteway@civicrm.org&CUSTIP[9]=127.0.0.1&COMMENT1[4]=4200&COMMENT2[4]=live&INVNUM[3]=xyz&ORDERDESC[17]=Test+Contribution&VERBOSITY[6]=MEDIUM&BILLTOCOUNTRY[3]=AUS', ]; } -- 2.25.1