More comment fixes
[civicrm-core.git] / api / v3 / Contribution.php
index 749ec6ce7080d3427d51aa131793c5de23e5b6e9..5c696661f583e983a27a22cbc18ba42e2e1c052f 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
@@ -24,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * File for the CiviCRM APIv3 Contribution functions
  *
  * @copyright CiviCRM LLC (c) 2004-2014
  * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $
- *
  */
 
 /**
- * Add or update a contribution
+ * Add or update a contribution.
  *
  * @param array $params
- *   (reference ) input parameters.
+ *   Input parameters.
  *
  * @throws API_Exception
- * @return array  Api result array
- * @static void
- * @access public
- * @example ContributionCreate.php
- * {@getfields Contribution_create}
+ * @return array
+ *   Api result array
  */
 function civicrm_api3_contribution_create(&$params) {
   $values = array();
@@ -68,16 +63,17 @@ function civicrm_api3_contribution_create(&$params) {
 
   _civicrm_api3_contribution_create_legacy_support_45($params);
 
-  // make sure tax calculation is handled via api
+  // Make sure tax calculation is handled via api.
   $params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params);
 
   return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution');
 }
 
 /**
- * Adjust Metadata for Create action
+ * Adjust Metadata for Create action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
  *
- * The metadata is used for setting defaults, documentation & validation
  * @param array $params
  *   Array or parameters determined by getfields.
  */
@@ -147,10 +143,13 @@ function _civicrm_api3_contribution_create_spec(&$params) {
 }
 
 /**
- * Support for schema changes made in 4.5
+ * Support for schema changes made in 4.5.
+ *
  * The main purpose of the API is to provide integrators a level of stability not provided by
  * the core code or schema - this means we have to provide support for api calls (where possible)
  * across schema changes.
+ *
+ * @param array $params
  */
 function _civicrm_api3_contribution_create_legacy_support_45(&$params) {
   //legacy soft credit handling - recommended approach is chaining
@@ -158,29 +157,25 @@ function _civicrm_api3_contribution_create_legacy_support_45(&$params) {
     $params['soft_credit'][] = array(
       'contact_id'          => $params['soft_credit_to'],
       'amount'              => $params['total_amount'],
-      'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type")
+      'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
     );
   }
   if (!empty($params['honor_contact_id'])) {
     $params['soft_credit'][] = array(
       'contact_id'          => $params['honor_contact_id'],
       'amount'              => $params['total_amount'],
-      'soft_credit_type_id' => CRM_Utils_Array::value('honor_type_id', $params, CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'))
+      'soft_credit_type_id' => CRM_Utils_Array::value('honor_type_id', $params, CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name')),
     );
   }
 }
 
 /**
- * Delete a contribution
+ * Delete a contribution.
  *
  * @param array $params
- *   (reference ) input parameters.
+ *   Input parameters.
  *
- * @return boolean        true if success, else false
- * @static void
- * @access public
- * {@getfields Contribution_delete}
- * @example ContributionDelete.php
+ * @return array
  */
 function civicrm_api3_contribution_delete($params) {
 
@@ -194,23 +189,24 @@ function civicrm_api3_contribution_delete($params) {
 }
 
 /**
- * modify metadata. Legacy support for contribution_id
+ * Modify metadata for delete action.
+ *
+ * Legacy support for contribution_id.
+ *
+ * @param array $params
  */
 function _civicrm_api3_contribution_delete_spec(&$params) {
   $params['id']['api.aliases'] = array('contribution_id');
 }
 
 /**
- * Retrieve a set of contributions, given a set of input params
+ * Retrieve a set of contributions.
  *
  * @param array $params
- *   (reference ) input parameters.
+ *  Input parameters.
  *
- * @return array of contributions, if error an array with an error id and error message
- * @static void
- * @access public
- * {@getfields Contribution_get}
- * @example ContributionGet.php
+ * @return array
+ *   Array of contributions, if error an array with an error id and error message
  */
 function civicrm_api3_contribution_get($params) {
 
@@ -224,9 +220,6 @@ function civicrm_api3_contribution_get($params) {
     $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]);
   }
@@ -234,9 +227,12 @@ function civicrm_api3_contribution_get($params) {
 }
 
 /**
- * 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
+ * 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
+ *
+ * @param $contribution
  */
 function _civicrm_api3_format_soft_credit(&$contribution) {
   if (!empty($contribution['soft_credit'])) {
@@ -246,9 +242,10 @@ function _civicrm_api3_format_soft_credit(&$contribution) {
 }
 
 /**
- * Adjust Metadata for Get action
+ * Adjust Metadata for Get action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
  *
- * The metadata is used for setting defaults, documentation & validation
  * @param array $params
  *   Array or parameters determined by getfields.
  */
@@ -262,31 +259,29 @@ function _civicrm_api3_contribution_get_spec(&$params) {
 }
 
 /**
- * take the input parameter list as specified in the data model and
- * convert it into the same format that we use in QF and BAO object
+ * Legacy handling for contribution parameters.
+ *
+ * Take the input parameter list as specified in the data model and
+ * convert it into the same format that we use in QF and BAO object.
  *
  * @param array $params
- *   Associative array of property name/value.
- * pairs to insert in new contact.
+ *   property name/value  pairs to insert in new contact.
  * @param array $values
  *   The reformatted properties that we can use internally.
- * '
  *
- * @param bool $create
- *
- * @return array|CRM_Error
- * @access public
+ * @return array
  */
-function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) {
+function _civicrm_api3_contribute_format_params($params, &$values) {
   //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
   _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
   return array();
 }
 
 /**
- * Adjust Metadata for Transact action
+ * Adjust Metadata for Transact action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
  *
- * The metadata is used for setting defaults, documentation & validation
  * @param array $params
  *   Array or parameters determined by getfields.
  */
@@ -300,15 +295,17 @@ function _civicrm_api3_contribution_transact_spec(&$params) {
  * Process a transaction and record it against the contact.
  *
  * @param array $params
- *   (reference ) input parameters.
- *
- * @return array (reference )        contribution of created or updated record (or a civicrm error)
- * @static void
- * @access public
+ *   Input parameters.
  *
+ * @return array
+ *   contribution of created or updated record (or a civicrm error)
  */
 function civicrm_api3_contribution_transact($params) {
   // Set some params specific to payment processing
+  // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with
+  // 'is_error' set
+  // also trxn_id is not saved.
+  // but since there is no test it's not desirable to jump in & make the obvious changes.
   $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test';
   $params['amount'] = $params['total_amount'];
   if (!isset($params['net_amount'])) {
@@ -318,6 +315,9 @@ function civicrm_api3_contribution_transact($params) {
     $params['invoiceID'] = $params['invoice_id'];
   }
 
+  // Some payment processors expect a unique invoice_id - generate one if not supplied
+  $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE)));
+
   $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']);
   if (civicrm_error($paymentProcessor)) {
     return $paymentProcessor;
@@ -328,42 +328,27 @@ function civicrm_api3_contribution_transact($params) {
     return $payment;
   }
 
-  $transaction = $payment->doDirectPayment($params);
-  if (civicrm_error($transaction)) {
-    return $transaction;
-  }
+  $transaction = $payment->doPayment($params);
 
-  // but actually, $payment->doDirectPayment() doesn't return a
-  // CRM_Core_Error by itself
-  if (is_object($transaction) && get_class($transaction) == 'CRM_Core_Error') {
-    $errs = $transaction->getErrors();
-    if (!empty($errs)) {
-      $last_error = array_shift($errs);
-      return CRM_Core_Error::createApiError($last_error['message']);
-    }
-  }
   $params['payment_instrument_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', $paymentProcessor['payment_processor_type_id'], 'payment_type') == 1 ? 'Credit Card' : 'Debit Card';
   return civicrm_api('contribution', 'create', $params);
 }
 
 /**
- * Send a contribution confirmation (receipt or invoice)
+ * Send a contribution confirmation (receipt or invoice).
+ *
  * The appropriate online template will be used (the existence of related objects
  * (e.g. memberships ) will affect this selection
  *
  * @param array $params
  *   Input parameters.
- * {@getfields Contribution_sendconfirmation}
  *
  * @throws Exception
- * @return array  Api result array
- * @static void
- * @access public
  */
 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']);
@@ -372,16 +357,17 @@ function civicrm_api3_contribution_sendconfirmation($params) {
 }
 
 /**
- * Adjust Metadata for sendconfirmation action
+ * Adjust Metadata for sendconfirmation action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
  *
- * The metadata is used for setting defaults, documentation & validation
  * @param array $params
  *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
   $params['id'] = array(
     'api.required' => 1,
-    'title' => 'Contribution ID'
+    'title' => 'Contribution ID',
   );
   $params['receipt_from_email'] = array(
     'api.required' => 1,
@@ -402,20 +388,19 @@ 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)
+ * Complete an existing (pending) transaction.
+ *
+ * This will update related entities (participant, membership, pledge etc)
+ * and take 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}
  *
  * @throws API_Exception
- * @return array  Api result array
- * @static void
- * @access public
+ *   Api result array.
  */
 function civicrm_api3_contribution_completetransaction(&$params) {
 
@@ -453,7 +438,8 @@ function civicrm_api3_contribution_completetransaction(&$params) {
 }
 
 /**
- * provide function metadata
+ * Provide function metadata.
+ *
  * @param array $params
  */
 function _civicrm_api3_contribution_completetransaction_spec(&$params) {