From 39f47c0d88de372e4bd860a6552ab59d7b7eb282 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 3 May 2015 23:13:14 +1200 Subject: [PATCH] CRM-16367 (Backoffice Contribution Form) consolidate premium processing to one place --- CRM/Contribute/Form/Contribution.php | 21 ++++++--- CRM/Core/Payment.php | 2 +- CRM/Financial/BAO/PaymentProcessor.php | 2 +- .../CRM/Contribute/Form/ContributionTest.php | 45 ++++++++++++++++++- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index a8baae0ab6..0352c12d02 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1057,7 +1057,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP //Get the require fields value only. $params = $this->_params = $submittedValues; - $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], + $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor_id'], $this->_mode ); @@ -1299,11 +1299,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP CRM_Contribute_Form_AdditionalInfo::processNote($params, $contactID, $contribution->id, NULL); } - //process premium - if ($contribution->id && isset($params['product_name'][0])) { - CRM_Contribute_Form_AdditionalInfo::processPremium($params, $contribution->id, NULL, $this->_options); - } - if ($contribution->id) { array_unshift($this->statusMessage, ts('The contribution record has been processed.')); if (!empty($this->_params['is_email_receipt']) && $sendReceipt) { @@ -1403,8 +1398,11 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP * * @param array $params * @param int $action + * @param string|null $creditCardMode + * + * @throws \CiviCRM_API3_Exception */ - public function testSubmit($params, $action) { + public function testSubmit($params, $action, $creditCardMode = NULL) { $defaults = array( 'soft_credit_contact_id' => array(), 'receipt_date' => '', @@ -1430,8 +1428,17 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $existingContribution ); + if ($creditCardMode) { + $this->_mode = $creditCardMode; + } + + // Required because processCreditCard calls set method on this. + $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->controller = new CRM_Core_Controller(); + CRM_Contribute_Form_AdditionalInfo::buildPremium($this); + $this->_fields = array(); $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params)); } diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 81774bdfc2..63c2ce9033 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -89,7 +89,7 @@ abstract class CRM_Core_Payment { * @return CRM_Core_Payment * @throws \CRM_Core_Exception */ - public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { + public static function singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { // make sure paymentProcessor is not empty // CRM-7424 if (empty($paymentProcessor)) { diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index f8ec81f065..43b47f3035 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -286,7 +286,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name'); - $result['instance'] = $result['object'] =& CRM_Core_Payment::singleton($mode, $result); + $result['instance'] = $result['object'] = CRM_Core_Payment::singleton($mode, $result); return $result; } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index df93819b76..0208520707 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -77,13 +77,21 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { */ protected $products = array(); + /** + * Dummy payment processor. + * + * @var array + */ + protected $paymentProcessor = array(); + /** * Setup function. */ public function setUp() { + $this->_apiversion = 3; parent::setUp(); + $this->createLoggedInUser(); - $this->_apiversion = 3; $this->_individualId = $this->individualCreate(); $paymentProcessor = $this->processorCreate(); $this->_params = array( @@ -127,6 +135,8 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { )); $this->products[] = $product1['values'][$product1['id']]; + $this->paymentProcessor = $this->processorCreate(); + } /** @@ -265,4 +275,37 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { $mut->stop(); } + /** + * Test functions involving premiums. + */ + public function testPremiumUpdateCreditCard() { + $form = new CRM_Contribute_Form_Contribution(); + $mut = new CiviMailUtils($this, TRUE); + $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('Check', $this->paymentInstruments), + 'contribution_status_id' => 1, + 'product_name' => array($this->products[0]['id'], 1), + 'fulfilled_date' => '', + 'is_email_receipt' => TRUE, + 'from_email_address' => 'test@test.com', + 'payment_processor_id' => $this->paymentProcessor->id, + 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012), + 'credit_card_number' => '411111111111111', + ), CRM_Core_Action::ADD, + 'live'); + $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array()); + $this->assertEquals('clumsy smurf', $contributionProduct['product_option']); + $mut->checkMailLog(array( + 'Premium Information', + 'Smurf', + 'clumsy smurf', + )); + $mut->stop(); + } + } -- 2.25.1