CRM-13072 upgrade additional test classes including fixing activity.type.delete
[civicrm-core.git] / api / v3 / Contribution.php
index 0d774014158b61f893a2720119d306173d4c4b94..cca3439006f1c2abc42d35f7d9712896edf23708 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-// $Id$
 
 /*
  +--------------------------------------------------------------------+
@@ -57,13 +56,18 @@ function civicrm_api3_contribution_create(&$params) {
   _civicrm_api3_custom_format_params($params, $values, 'Contribution');
   $values["contact_id"] = CRM_Utils_Array::value('contact_id', $params);
   $values["source"] = CRM_Utils_Array::value('source', $params);
-
+  //legacy soft credit handling
+  if(!empty($params['soft_credit_to'])){
+    $values['soft_credit'] = array(array(
+      'contact_id' => $params['soft_credit_to'],
+      'amount' => $params['total_amount']));
+  }
   $ids = array();
   if (CRM_Utils_Array::value('id', $params)) {
     $ids['contribution'] = $params['id'];
     // CRM-12498
     if (CRM_Utils_Array::value('contribution_status_id', $params)) {
-      $error = array(); 
+      $error = array();
       //throw error for invalid status change
       CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error);
       if (array_key_exists('contribution_status_id', $error)) {
@@ -71,7 +75,6 @@ function civicrm_api3_contribution_create(&$params) {
       }
     }
   }
-
   $contribution = CRM_Contribute_BAO_Contribution::create($values, $ids);
 
   if (is_a($contribution, 'CRM_Core_Error')) {
@@ -91,6 +94,7 @@ function civicrm_api3_contribution_create(&$params) {
 function _civicrm_api3_contribution_create_spec(&$params) {
   $params['contact_id']['api.required'] = 1;
   $params['total_amount']['api.required'] = 1;
+  $params['payment_instrument_id']['api.aliases'] = array('payment_instrument');
   $params['payment_processor'] = array(
     'name' => 'payment_processor',
     'title' => 'Payment Processor ID',
@@ -184,8 +188,6 @@ function civicrm_api3_contribution_get($params) {
   $smartGroupCache  = CRM_Utils_Array::value('smartGroupCache', $params);
   $inputParams      = CRM_Utils_Array::value('input_params', $options, array());
   $returnProperties = CRM_Utils_Array::value('return', $options, NULL);
-  require_once 'CRM/Contribute/BAO/Query.php';
-  require_once 'CRM/Contact/BAO/Query.php';
   if (empty($returnProperties)) {
     $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE);
   }
@@ -207,14 +209,30 @@ function civicrm_api3_contribution_get($params) {
   $contribution = array();
   while ($dao->fetch()) {
     //CRM-8662
-    $contribution_details = $query->store ( $dao );
-    $soft_params = array('contribution_id' => $dao->contribution_id);
-    $soft_contribution = CRM_Contribute_BAO_Contribution::getSoftContribution ( $soft_params , true);
-    $contribution [$dao->contribution_id] = array_merge($contribution_details, $soft_contribution);
+    $contribution_details = $query->store($dao);
+    $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dao->contribution_id , TRUE);
+    $contribution[$dao->contribution_id] = array_merge($contribution_details, $softContribution);
+    if(isset($contribution[$dao->contribution_id]['financial_type_id'])){
+      $contribution[$dao->contribution_id]['financial_type_id'] = $contribution[$dao->contribution_id]['financial_type_id'];
+    }
+    // format soft credit for backward compatibility
+    _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]);
   }
   return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao);
 }
 
+/**
+ * This function is used to format the soft credit for backward compatibility
+ * as of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key
+ * but we still return first soft credit as a part of contribution array
+ */
+function _civicrm_api3_format_soft_credit(&$contribution) {
+  if (!empty($contribution['soft_credit'])) {
+    $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id'];
+    $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id'];
+  }
+}
+
 /**
  * Adjust Metadata for Get action
  *
@@ -243,41 +261,7 @@ function _civicrm_api3_contribution_get_spec(&$params) {
  */
 function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) {
 //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
-    require_once 'api/v3/utils.php';
   _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
-
-  foreach ($params as $key => $value) {
-    // ignore empty values or empty arrays etc
-    if (CRM_Utils_System::isNull($value)) {
-      continue;
-    }
-    // note that this is legacy handling - these should be handled at the api layer
-    switch ($key) {
-      case 'financial_type' :// should be dealt with either api pseudoconstant in validate_integer fn
-        $contributionTypeId = CRM_Utils_Array::key ( ucfirst ( $value ), CRM_Contribute_PseudoConstant::financialType() );
-        if ($contributionTypeId) {
-          if (CRM_Utils_Array::value('financial_type_id', $values) && $contributionTypeId != $values['financial_type_id']) {
-            throw new Exception("Mismatched Financial Type and Financial Type Id");
-          }
-          $values ['financial_type_id'] = $contributionTypeId;
-        }
-        else {
-          throw new Exception("Invalid Financial Type");
-        }
-        break;
-
-      case 'soft_credit_to':// should be dealt with by validate integer
-        if (!CRM_Utils_Rule::integer($value)) {
-          return civicrm_api3_create_error("$key not a valid Id: $value");
-        }
-        $values['soft_credit_to'] = $value;
-        break;
-
-      default:
-        break;
-    }
-  }
-
   return array();
 }
 
@@ -320,13 +304,11 @@ function civicrm_api3_contribution_transact($params) {
     $params['invoiceID'] = $params['invoice_id'];
   }
 
-  require_once 'CRM/Financial/BAO/PaymentProcessor.php';
   $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor_id'], $params['payment_processor_mode']);
   if (civicrm_error($paymentProcessor)) {
     return $paymentProcessor;
   }
 
-  require_once 'CRM/Core/Payment.php';
   $payment = &CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor);
   if (civicrm_error($payment)) {
     return $payment;
@@ -364,12 +346,12 @@ function civicrm_api3_contribution_transact($params) {
 function civicrm_api3_contribution_sendconfirmation($params) {
   $contribution = new CRM_Contribute_BAO_Contribution();
   $contribution->id = $params['id'];
-  if (! $contribution->find(true)) {
+  if (! $contribution->find(TRUE)) {
     throw new Exception('Contribution does not exist');
 }
   $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']);
-  $contribution->loadRelatedObjects($input, $ids, FALSE, true);
-  $contribution->composeMessageArray($input, $ids, $cvalues, false, false);
+  $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE);
+  $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE);
 }
 
 /**
@@ -389,3 +371,53 @@ function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
 
   );
 }
+
+/**
+ * Complete an existing (pending) transaction, updating related entities (participant, membership, pledge etc)
+ * and taking any complete actions from the contribution page (e.g. send receipt)
+ *
+ * @todo - most of this should live in the BAO layer but as we want it to be an addition
+ * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later
+ *
+ * @param array $params input parameters
+ * {@getfields Contribution_completetransaction}
+ * @return array  Api result array
+ * @static void
+ * @access public
+ *
+ */
+function civicrm_api3_contribution_completetransaction(&$params) {
+
+  $input = $ids = array();
+  $contribution = new CRM_Contribute_BAO_Contribution();
+  $contribution->id = $params['id'];
+  $contribution->find(TRUE);
+  if(!$contribution->id == $params['id']){
+    throw new API_Exception('A valid contribution ID is required', 'invalid_data');
+  }
+  try {
+    if(!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)){
+      throw new API_Exception('failed to load related objects');
+    }
+    $objects = $contribution->_relatedObjects;
+    $objects['contribution'] = &$contribution;
+    $input['component'] = $contribution->_component;
+    $input['is_test'] = $contribution->is_test;
+    $input['trxn_id']= $contribution->trxn_id;
+    $input['amount'] = $contribution->total_amount;
+    if(isset($params['is_email_receipt'])){
+      $input['is_email_receipt'] = $params['is_email_receipt'];
+    }
+    // @todo required for base ipn but problematic as api layer handles this
+    $transaction = new CRM_Core_Transaction();
+    $ipn = new CRM_Core_Payment_BaseIPN();
+    $ipn->completeTransaction($input, $ids, $objects, $transaction);
+  }
+  catch(Exception$e) {
+    throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString());
+  }
+}
+
+function _civicrm_api3_contribution_completetransaction(&$params) {
+
+}