From 22e393337109ed68d1a38f5c3cba228f565f90a5 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Wed, 26 Aug 2015 06:12:50 +1200 Subject: [PATCH] CRM-11338 ensure payment processor fee is set in back-office contribution --- CRM/Contribute/Form/Contribution.php | 1 + .../CRM/Contribute/Form/ContributionTest.php | 76 +++++++++++++++++-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 18 ++++- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index fb9eacb3a1..8a4ce9749b 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1258,6 +1258,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP 'trxn_id' => $result['trxn_id'], 'payment_processor_id' => $this->_paymentProcessor['id'], 'is_transactional' => FALSE, + 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), )); // This has now been set to 1 in the DB - declare it here also $contribution->contribution_status_id = 1; diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 29810209ad..133af7e027 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -84,6 +84,13 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { */ protected $paymentProcessor; + /** + * Payment processor ID. + * + * @var int + */ + protected $paymentProcessorID; + /** * Setup function. */ @@ -124,8 +131,9 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { )); $this->products[] = $product1['values'][$product1['id']]; - $this->paymentProcessor = $this->processorCreate(); - + $this->paymentProcessor = $this->dummyProcessorCreate(); + $processor = $this->paymentProcessor->getPaymentProcessor(); + $this->paymentProcessorID = $processor['id']; } /** @@ -229,6 +237,60 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { ), 1); } + /** + * Test the submit function on the contribution page. + */ + public function testSubmitCreditCardFee() { + $form = new CRM_Contribute_Form_Contribution(); + print_r($this->paymentProcessor); + $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 0, 'trxn_id' => 'tx', 'fee_amount' => .08)); + $form->_mode = 'Live'; + $form->testSubmit(array( + 'total_amount' => 50, + 'financial_type_id' => 1, + 'receive_date' => '04/21/2015', + 'receive_date_time' => '11:27PM', + 'contact_id' => $this->_individualId, + 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), + 'contribution_status_id' => 1, + 'credit_card_number' => 4444333322221111, + 'cvv2' => 123, + 'credit_card_exp_date' => array( + 'M' => 9, + 'Y' => 2025, + ), + 'credit_card_type' => 'Visa', + 'billing_first_name' => 'Junko', + 'billing_middle_name' => '', + 'billing_last_name' => 'Adams', + 'billing_street_address-5' => '790L Lincoln St S', + 'billing_city-5' => 'Maryknoll', + 'billing_state_province_id-5' => 1031, + 'billing_postal_code-5' => 10545, + 'billing_country_id-5' => 1228, + 'frequency_interval' => 1, + 'frequency_unit' => 'month', + 'installments' => '', + 'hidden_AdditionalDetail' => 1, + 'hidden_Premium' => 1, + 'from_email_address' => '"civi45" ', + 'receipt_date' => '', + 'receipt_date_time' => '', + 'payment_processor_id' => $this->paymentProcessorID, + 'currency' => 'USD', + 'source' => '', + ), CRM_Core_Action::ADD); + + $contribution = $this->callAPISuccessGetSingle('Contribution', array( + 'contact_id' => $this->_individualId, + 'contribution_status_id' => 'Completed', + )); + $this->assertEquals('50', $contribution['total_amount']); + $this->assertEquals(.08, $contribution['fee_amount']); + $this->assertEquals(49.92, $contribution['net_amount']); + $this->assertEquals('tx', $contribution['trxn_id']); + } + /** * Test the submit function with an invalid payment. * @@ -252,7 +314,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), - 'payment_processor_id' => $this->paymentProcessor->id, + 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012), 'credit_card_number' => '411111111111111', ), CRM_Core_Action::ADD, @@ -292,7 +354,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), - 'payment_processor_id' => $this->paymentProcessor->id, + 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025), 'credit_card_number' => '411111111111111', 'billing_city-5' => 'Vancouver', @@ -321,7 +383,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), - 'payment_processor_id' => $this->paymentProcessor->id, + 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025), 'credit_card_number' => '411111111111111', ), CRM_Core_Action::ADD, @@ -448,7 +510,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'fulfilled_date' => '', 'is_email_receipt' => TRUE, 'from_email_address' => 'test@test.com', - 'payment_processor_id' => $this->paymentProcessor->id, + 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2026), 'credit_card_number' => '411111111111111', ), CRM_Core_Action::ADD, @@ -558,7 +620,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { */ protected function getCreditCardParams() { return array( - 'payment_processor_id' => $this->paymentProcessor->id, + 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012), 'credit_card_number' => '411111111111111', ); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 5694673e56..4b0e550136 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1494,7 +1494,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * Create Payment Processor. * * @return CRM_Financial_DAO_PaymentProcessor - * instance of Payment Processsor + * instance of Payment Processor */ public function processorCreate() { $processorParams = array( @@ -1509,8 +1509,20 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { 'url_recur' => 'http://dummy.com', 'billing_mode' => 1, ); - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($processorParams); - return $paymentProcessor; + return CRM_Financial_BAO_PaymentProcessor::create($processorParams); + } + + /** + * Create Payment Processor. + * + * @param array $processorParams + * + * @return \CRM_Core_Payment_Dummy instance of Dummy Payment Processor + * instance of Dummy Payment Processor + */ + public function dummyProcessorCreate($processorParams = array()) { + $paymentProcessor = $this->processorCreate($processorParams); + return Civi\Payment\System::singleton()->getById($paymentProcessor->id); } /** -- 2.25.1