From 0193ebdbaaf5fc3716d41d26bee4f7feeb00cd2b Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Wed, 26 Aug 2015 07:04:04 +1200 Subject: [PATCH] CRM-11338 add testing for fee_amount handling to online contribution page --- CRM/Contribute/Form/Contribution/Confirm.php | 120 +++++++++--------- tests/phpunit/CiviTest/CiviUnitTestCase.php | 4 +- tests/phpunit/api/v3/ContributionPageTest.php | 6 + 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 2dda394e91..c705eeb568 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1659,24 +1659,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); $paymentActionResult = $payment->doPayment($form->_params, 'contribute'); - - if (CRM_Utils_Array::value('payment_status_id', $paymentActionResult) == 1) { - // Refer to CRM-16737. Payment processors 'should' return payment_status_id - // to denote the outcome of the transaction. - try { - civicrm_api3('contribution', 'completetransaction', array( - 'id' => $paymentResult['contribution']->id, - 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paymentActionResult, $paymentResult['contribution']->trxn_id), - 'is_transactional' => FALSE, - 'payment_processor_id' => $form->_paymentProcessor['id'], - )); - } - catch (CiviCRM_API3_Exception $e) { - if ($e->getErrorCode() != 'contribution_completed') { - throw new CRM_Core_Exception('Failed to update contribution in database'); - } - } - } + $this->completeTransaction($paymentActionResult, $paymentResult['contribution']->id); // Do not send an email if Recurring transaction is done via Direct Mode // Email will we sent when the IPN is received. return; @@ -1729,19 +1712,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $tempParams['amount'] = $minimumFee; $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE)); - $result = NULL; - if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { - // At the moment our tests are calling this form in a way that leaves 'object' empty. For - // now we compensate here. - if (empty($form->_paymentProcessor['object'])) { - $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); - } - else { - $payment = $form->_paymentProcessor['object']; - } - $result = $payment->doPayment($tempParams, 'contribute'); - } - //assign receive date when separate membership payment //and contribution amount not selected. if ($form->_amount == 0) { @@ -1752,22 +1722,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $form->assign('receive_date', $receiveDate); } - $form->set('membership_trx_id', $result['trxn_id']); $form->set('membership_amount', $minimumFee); - - $form->assign('membership_trx_id', $result['trxn_id']); $form->assign('membership_amount', $minimumFee); // we don't need to create the user twice, so lets disable cms_create_account // irrespective of the value, CRM-2888 $tempParams['cms_create_account'] = 0; - //CRM-16165, scenarios are - // 1) If contribution is_pay_later and if contribution amount is > 0.0 we set pending = TRUE, vice-versa FALSE - // 2) If not pay later but auto-renewal membership is chosen then pending = TRUE as it later triggers - // pending recurring contribution, vice-versa FALSE - $pending = $form->_params['is_pay_later'] ? (($minimumFee > 0.0) ? TRUE : FALSE) : (!empty($form->_params['auto_renew']) ? TRUE : FALSE); - //set this variable as we are not creating pledge for //separate membership payment contribution. //so for differentiating membership contribution from @@ -1790,12 +1751,28 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($form, $tempParams, - $result, + $tempParams, $contributionParams, $financialType, TRUE, $form->_bltID ); + + if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { + // At the moment our tests are calling this form in a way that leaves 'object' empty. For + // now we compensate here. + if (empty($form->_paymentProcessor['object'])) { + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); + } + else { + $payment = $form->_paymentProcessor['object']; + } + $result = $payment->doPayment($tempParams, 'contribute'); + $form->set('membership_trx_id', $result['trxn_id']); + $form->assign('membership_trx_id', $result['trxn_id']); + $this->completeTransaction($result, $membershipContribution->id); + } + return $membershipContribution; } @@ -2271,28 +2248,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr ($this->_mode == 'test') ? 1 : 0 ); - if (!empty($result['is_payment_failure'])) { - return $result; - } - // @todo move premium processing to complete transaction if it truly is an 'after' action. - $this->postProcessPremium($premiumParams, $result['contribution']); - if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { - try { - civicrm_api3('contribution', 'completetransaction', array( - 'id' => $result['contribution']->id, - 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), - 'payment_processor_id' => $this->_paymentProcessor['id'], - 'is_transactional' => FALSE, - 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), - ) - ); - } - catch (CiviCRM_API3_Exception $e) { - if ($e->getErrorCode() != 'contribution_completed') { - throw new CRM_Core_Exception('Failed to update contribution in database'); - } - } - + if (empty($result['is_payment_failure'])) { + // @todo move premium processing to complete transaction if it truly is an 'after' action. + $this->postProcessPremium($premiumParams, $result['contribution']); + } + if (!empty($result['contribution'])) { + // Not quite sure why it would be empty at this stage but tests show it can be ... at least in tests. + $this->completeTransaction($result, $result['contribution']->id); } return $result; } @@ -2400,4 +2362,36 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } } + /** + * Complete transaction if payment has been processed. + * + * Check the result for a success outcome & if paid then complete the transaction. + * + * Completing will trigger update of related entities and emails. + * + * @param array $result + * @param int $contributionID + * + * @throws \CRM_Core_Exception + */ + protected function completeTransaction($result, $contributionID) { + if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { + try { + civicrm_api3('contribution', 'completetransaction', array( + 'id' => $contributionID, + 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), + 'payment_processor_id' => $this->_paymentProcessor['id'], + 'is_transactional' => FALSE, + 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), + ) + ); + } + catch (CiviCRM_API3_Exception $e) { + if ($e->getErrorCode() != 'contribution_completed') { + throw new CRM_Core_Exception('Failed to update contribution in database'); + } + } + } + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 4b0e550136..a8f17c3ea3 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1517,8 +1517,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @param array $processorParams * - * @return \CRM_Core_Payment_Dummy instance of Dummy Payment Processor - * instance of Dummy Payment Processor + * @return \CRM_Core_Payment_Dummy + * Instance of Dummy Payment Processor */ public function dummyProcessorCreate($processorParams = array()) { $paymentProcessor = $this->processorCreate($processorParams); diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index f5caacb335..d167500f79 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -278,6 +278,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { */ public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() { $this->setUpMembershipContributionPage(TRUE); + $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']); + $processor->setDoDirectPaymentResult(array('fee_amount' => .72)); $submitParams = array( 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']), 'id' => (int) $this->_ids['contribution_page'], @@ -303,6 +305,10 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values']))); $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id'])); $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']); + foreach ($contributions['values'] as $contribution) { + $this->assertEquals(.72, $contribution['fee_amount']); + $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']); + } } /** -- 2.25.1