CRM-16367 (Backoffice Contribution Form) consolidate premium processing to one place
authorEileen McNaughton <eileen@fuzion.co.nz>
Sun, 3 May 2015 11:13:14 +0000 (23:13 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Sun, 3 May 2015 11:13:14 +0000 (23:13 +1200)
CRM/Contribute/Form/Contribution.php
CRM/Core/Payment.php
CRM/Financial/BAO/PaymentProcessor.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php

index a8baae0ab6262158b7c57730392aab81a2aa0816..0352c12d0238ceb449efb8cd2b79b13bd402e9e3 100644 (file)
@@ -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));
 
   }
index 81774bdfc2eb13f335d418ab8f53458d7ad4560d..63c2ce9033cf3feb07c51cdb39f9451f54ca02c7 100644 (file)
@@ -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)) {
index f8ec81f0659796a799fda39845d2ceb992e8ec1f..43b47f30357705d759dd119961c8380eb4d88c19 100644 (file)
@@ -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;
   }
index df93819b764420659df36fa2a10ecc1a0c6297a4..0208520707d258d2951b5232a6285b7d9c53ed58 100644 (file)
@@ -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();
+  }
+
 }