diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 27c254fbca43..c7b8fa175448 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -132,6 +132,7 @@ public static function add(&$params, $ids = array()) { else { // @todo put a deprecated here - this should be done in the form layer. $params['skipCleanMoney'] = FALSE; + Civi::log()->warning('Deprecated code path. Money should always be clean before it hits the BAO.', array('civi.tag' => 'deprecated')); } foreach ($moneyFields as $field) { diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 7789e7ac10c9..cefe91fc8b9c 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -221,6 +221,15 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { */ public $billingFieldSets = array(); + /** + * Monetary fields that may be submitted. + * + * These should get a standardised format in the beginPostProcess function. + * + * @var array + */ + protected $submittableMoneyFields = []; + /** * Pre process function with common actions. */ @@ -567,6 +576,11 @@ protected function beginPostProcess() { $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); self::formatCreditCardDetails($this->_params); + foreach ($this->submittableMoneyFields as $moneyField) { + if (isset($this->_params[$moneyField])) { + $this->_params[$moneyField] = CRM_Utils_Rule::cleanMoney($this->_params[$moneyField]); + } + } } /** diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 773f80a4a514..a433d880175d 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -203,6 +203,13 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP */ protected $statusMessageTitle; + /** + * Monetary fields that may be submitted. + * + * These should get a standardised format in the beginPostProcess function. + */ + protected $submittableMoneyFields = ['total_amount', 'net_amount', 'non_deductible_amount', 'fee_amount']; + /** * Explicitly declare the form context. */ @@ -1396,6 +1403,9 @@ protected function submit($submittedValues, $action, $pledgePaymentID) { // would cause breakage for negative values in some cases. $submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues); } + else { + Civi::log()->warning('Deprecated code path. Price set id should always be set.', array('civi.tag' => 'deprecated')); + } if ($this->_id) { if ($this->_compId) { if ($this->_context == 'participant') { diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 005ca08bf482..60e6d2cecd5e 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -143,11 +143,16 @@ public function tearDown() { /** * Test the submit function on the contribution page. + * + * @param string $thousandSeparator + * + * @dataProvider getThousandSeparators */ - public function testSubmit() { + public function testSubmit($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); $form = new CRM_Contribute_Form_Contribution(); $form->testSubmit(array( - 'total_amount' => 50, + 'total_amount' => $this->formatMoneyInput(1234), 'financial_type_id' => 1, 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Check', $this->paymentInstruments), @@ -156,6 +161,8 @@ public function testSubmit() { CRM_Core_Action::ADD); $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId)); $this->assertEmpty($contribution['amount_level']); + $this->assertEquals(1234, $contribution['total_amount']); + $this->assertEquals(1234, $contribution['net_amount']); } /** diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index c1216626ed1c..d137c12700ad 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -119,9 +119,14 @@ public function testSubmitUpaidPriceChangeWhileStillPending() { /** * Initial test of submit function. * + * @param string $thousandSeparator + * + * @dataProvider getThousandSeparators + * * @throws \Exception */ - public function testSubmitWithPayment() { + public function testSubmitWithPayment($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); $form = $this->getForm(array('is_monetary' => 1, 'financial_type_id' => 1)); $form->_mode = 'Live'; $form->_quickConfig = TRUE; @@ -156,8 +161,8 @@ public function testSubmitWithPayment() { 13 => 1, ), 'amount_level' => 'Too much', - 'fee_amount' => 55, - 'total_amount' => 55, + 'fee_amount' => $this->formatMoneyInput(1550.55), + 'total_amount' => $this->formatMoneyInput(1550.55), 'from_email_address' => 'abc@gmail.com', 'send_receipt' => 1, 'receipt_text' => '', @@ -165,7 +170,7 @@ public function testSubmitWithPayment() { $participants = $this->callAPISuccess('Participant', 'get', array()); $this->assertEquals(1, $participants['count']); $contribution = $this->callAPISuccessGetSingle('Contribution', array()); - $this->assertEquals(55, $contribution['total_amount']); + $this->assertEquals(1550.55, $contribution['total_amount']); $this->assertEquals('Debit Card', $contribution['payment_instrument']); }