INFRA 32 - Batch #17
[civicrm-core.git] / api / v3 / Contribution.php
index c553702a6889b3c9f58779e4777dac30723fbdb7..a9874d4dd05e0ba3cfc0415c80210f3479e847f1 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
 /**
  * Add or update a contribution
  *
- * @param  array $params (reference ) input parameters
+ * @param array $params
+ *   (reference ) 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();
@@ -77,7 +74,8 @@ function civicrm_api3_contribution_create(&$params) {
  * Adjust Metadata for Create action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_contribution_create_spec(&$params) {
   $params['contact_id']['api.required'] = 1;
@@ -145,25 +143,26 @@ function _civicrm_api3_contribution_create_spec(&$params) {
 }
 
 /**
-* 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.
-*/
-function _civicrm_api3_contribution_create_legacy_support_45(&$params){
+ * 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
-  if(!empty($params['soft_credit_to'])){
+  if (!empty($params['soft_credit_to'])) {
     $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'])){
+  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')),
     );
   }
 }
@@ -171,13 +170,11 @@ function _civicrm_api3_contribution_create_legacy_support_45(&$params){
 /**
  * Delete a contribution
  *
- * @param  array   $params           (reference ) input parameters
+ * @param array $params
+ *   (reference ) input parameters.
  *
- * @return boolean        true if success, else false
- * @static void
- * @access public
- * {@getfields Contribution_delete}
- * @example ContributionDelete.php
+ * @return bool
+ *   true if success, else false
  */
 function civicrm_api3_contribution_delete($params) {
 
@@ -192,6 +189,7 @@ function civicrm_api3_contribution_delete($params) {
 
 /**
  * modify metadata. Legacy support for contribution_id
+ * @param array $params
  */
 function _civicrm_api3_contribution_delete_spec(&$params) {
   $params['id']['api.aliases'] = array('contribution_id');
@@ -200,13 +198,11 @@ function _civicrm_api3_contribution_delete_spec(&$params) {
 /**
  * Retrieve a set of contributions, given a set of input params
  *
- * @param  array $params (reference ) input parameters
+ * @param array $params
+ *   (reference ) 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) {
 
@@ -218,11 +214,8 @@ function civicrm_api3_contribution_get($params) {
   while ($dao->fetch()) {
     //CRM-8662
     $contribution_details = $query->store($dao);
-    $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dao->contribution_id , TRUE);
+    $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]);
   }
@@ -233,6 +226,7 @@ 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
  * 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'])) {
@@ -245,7 +239,8 @@ function _civicrm_api3_format_soft_credit(&$contribution) {
  * Adjust Metadata for Get action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_contribution_get_spec(&$params) {
   $params['contribution_test']['api.default'] = 0;
@@ -260,18 +255,15 @@ 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
  *
- * @param array $params Associative array of property name/value
- * pairs to insert in new contact.
- * @param array $values The reformatted properties that we can use internally
- * '
- *
- * @param bool $create
+ * @param array $params
+ *   property name/value  pairs to insert in new contact.
+ * @param array $values
+ *   The reformatted properties that we can use internally.
  *
- * @return array|CRM_Error
- * @access public
+ * @return array
  */
-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
+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();
 }
@@ -280,7 +272,8 @@ function _civicrm_api3_contribute_format_params($params, &$values, $create = FAL
  * Adjust Metadata for Transact action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_contribution_transact_spec(&$params) {
   $fields = civicrm_api3('contribution', 'getfields', array('action' => 'create'));
@@ -291,12 +284,11 @@ 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
+ * @param array $params
+ *   (reference ) 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
@@ -309,6 +301,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;
@@ -342,20 +337,18 @@ function civicrm_api3_contribution_transact($params) {
  * 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}
+ * @param array $params
+ *   Input parameters.
  *
  * @throws Exception
- * @return array  Api result array
- * @static void
- * @access public
+ *   Api result array.
  */
 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);
@@ -365,15 +358,16 @@ function civicrm_api3_contribution_sendconfirmation($params) {
  * Adjust Metadata for sendconfirmation action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @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,
+    'api.required' => 1,
     'title' => 'From Email address (string) required until someone provides a patch :-)',
   );
   $params['receipt_from_name'] = array(
@@ -397,13 +391,12 @@ function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
  * @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
+ * @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) {
 
@@ -411,11 +404,11 @@ function civicrm_api3_contribution_completetransaction(&$params) {
   $contribution = new CRM_Contribute_BAO_Contribution();
   $contribution->id = $params['id'];
   $contribution->find(TRUE);
-  if(!$contribution->id == $params['id']){
+  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)){
+    if (!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)) {
       throw new API_Exception('failed to load related objects');
     }
     elseif ($contribution->contribution_status_id == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) {
@@ -425,9 +418,9 @@ function civicrm_api3_contribution_completetransaction(&$params) {
     $objects['contribution'] = &$contribution;
     $input['component'] = $contribution->_component;
     $input['is_test'] = $contribution->is_test;
-    $input['trxn_id']= !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id;
+    $input['trxn_id'] = !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id;
     $input['amount'] = $contribution->total_amount;
-    if(isset($params['is_email_receipt'])){
+    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