5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 Contribution functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contribute
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $
42 * Add or update a contribution
44 * @param array $params (reference ) input parameters
46 * @return array Api result array
49 * @example ContributionCreate.php
50 * {@getfields Contribution_create}
52 function civicrm_api3_contribution_create(&$params) {
55 _civicrm_api3_contribute_format_params($params, $values);
57 _civicrm_api3_custom_format_params($params, $values, 'Contribution');
58 $values["contact_id"] = CRM_Utils_Array
::value('contact_id', $params);
59 $values["source"] = CRM_Utils_Array
::value('source', $params);
62 if (CRM_Utils_Array
::value('id', $params)) {
63 $ids['contribution'] = $params['id'];
65 if (CRM_Utils_Array
::value('contribution_status_id', $params)) {
67 //throw error for invalid status change
68 CRM_Contribute_BAO_Contribution
::checkStatusValidation(NULL, $params, $error);
69 if (array_key_exists('contribution_status_id', $error)) {
70 return civicrm_api3_create_error($error['contribution_status_id']);
75 $contribution = CRM_Contribute_BAO_Contribution
::create($values, $ids);
77 if (is_a($contribution, 'CRM_Core_Error')) {
78 return civicrm_api3_create_error($contribution->_errors
[0]['message']);
80 _civicrm_api3_object_to_array($contribution, $contributeArray[$contribution->id
]);
82 return civicrm_api3_create_success($contributeArray, $params, 'contribution', 'create', $contribution);
86 * Adjust Metadata for Create action
88 * The metadata is used for setting defaults, documentation & validation
89 * @param array $params array or parameters determined by getfields
91 function _civicrm_api3_contribution_create_spec(&$params) {
92 $params['contact_id']['api.required'] = 1;
93 $params['total_amount']['api.required'] = 1;
94 $params['payment_processor'] = array(
95 'name' => 'payment_processor',
96 'title' => 'Payment Processor ID',
97 'description' => 'ID of payment processor used for this contribution',
98 // field is called payment processor - not payment processor id but can only be one id so
99 // it seems likely someone will fix it up one day to be more consistent - lets alias it from the start
100 'api.aliases' => array('payment_processor_id'),
102 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
103 $params['financial_type_id']['api.required'] = 1;
104 $params['note'] = array(
106 'uniqueName' => 'contribution_note',
109 'description' => 'Associated Note in the notes table',
111 $params['soft_credit_to'] = array(
112 'name' => 'soft_credit_to',
113 'title' => 'Soft Credit contact ID',
115 'description' => 'ID of Contact to be Soft credited to',
116 'FKClassName' => 'CRM_Contact_DAO_Contact',
118 // note this is a recommended option but not adding as a default to avoid
119 // creating unecessary changes for the dev
120 $params['skipRecentView'] = array(
121 'name' => 'skipRecentView',
122 'title' => 'Skip adding to recent view',
124 'description' => 'Do not add to recent view (setting this improves performance)',
126 $params['skipLineItem'] = array(
127 'name' => 'skipLineItem',
128 'title' => 'Skip adding line items',
131 'description' => 'Do not add line items by default (if you wish to add your own)',
136 * Delete a contribution
138 * @param array $params (reference ) input parameters
140 * @return boolean true if success, else false
143 * {@getfields Contribution_delete}
144 * @example ContributionDelete.php
146 function civicrm_api3_contribution_delete($params) {
148 $contributionID = CRM_Utils_Array
::value('contribution_id', $params) ?
$params['contribution_id'] : $params['id'];
149 if (CRM_Contribute_BAO_Contribution
::deleteContribution($contributionID)) {
150 return civicrm_api3_create_success(array($contributionID => 1));
153 return civicrm_api3_create_error('Could not delete contribution');
158 * modify metadata. Legacy support for contribution_id
160 function _civicrm_api3_contribution_delete_spec(&$params) {
161 $params['id']['api.aliases'] = array('contribution_id');
165 * Retrieve a set of contributions, given a set of input params
167 * @param array $params (reference ) input parameters
168 * @param array $returnProperties Which properties should be included in the
169 * returned Contribution object. If NULL, the default
170 * set of properties will be included.
172 * @return array (reference ) array of contributions, if error an array with an error id and error message
175 * {@getfields Contribution_get}
176 * @example ContributionGet.php
178 function civicrm_api3_contribution_get($params) {
180 $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get');
181 $sort = CRM_Utils_Array
::value('sort', $options, NULL);
182 $offset = CRM_Utils_Array
::value('offset', $options);
183 $rowCount = CRM_Utils_Array
::value('limit', $options);
184 $smartGroupCache = CRM_Utils_Array
::value('smartGroupCache', $params);
185 $inputParams = CRM_Utils_Array
::value('input_params', $options, array());
186 $returnProperties = CRM_Utils_Array
::value('return', $options, NULL);
187 require_once 'CRM/Contribute/BAO/Query.php';
188 require_once 'CRM/Contact/BAO/Query.php';
189 if (empty($returnProperties)) {
190 $returnProperties = CRM_Contribute_BAO_Query
::defaultReturnProperties(CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
193 $newParams = CRM_Contact_BAO_Query
::convertFormValues($inputParams);
194 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
195 FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
197 list($select, $from, $where, $having) = $query->query();
199 $sql = "$select $from $where $having";
202 $sql .= " ORDER BY $sort ";
204 $sql .= " LIMIT $offset, $rowCount ";
205 $dao = CRM_Core_DAO
::executeQuery($sql);
207 $contribution = array();
208 while ($dao->fetch()) {
210 $contribution_details = $query->store ( $dao );
211 $soft_params = array('contribution_id' => $dao->contribution_id
);
212 $soft_contribution = CRM_Contribute_BAO_Contribution
::getSoftContribution ( $soft_params , true);
213 $contribution [$dao->contribution_id
] = array_merge($contribution_details, $soft_contribution);
215 return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao);
219 * Adjust Metadata for Get action
221 * The metadata is used for setting defaults, documentation & validation
222 * @param array $params array or parameters determined by getfields
224 function _civicrm_api3_contribution_get_spec(&$params) {
225 $params['contribution_test']['api.default'] = 0;
226 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
227 $params['contact_id'] = $params['contribution_contact_id'];
228 $params['contact_id']['api.aliases'] = array('contribution_contact_id');
229 unset($params['contribution_contact_id']);
233 * take the input parameter list as specified in the data model and
234 * convert it into the same format that we use in QF and BAO object
236 * @param array $params Associative array of property name/value
237 * pairs to insert in new contact.
238 * @param array $values The reformatted properties that we can use internally
241 * @return array|CRM_Error
244 function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) {
245 //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
246 require_once 'api/v3/utils.php';
247 _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
249 foreach ($params as $key => $value) {
250 // ignore empty values or empty arrays etc
251 if (CRM_Utils_System
::isNull($value)) {
254 // note that this is legacy handling - these should be handled at the api layer
256 case 'financial_type' :// should be dealt with either api pseudoconstant in validate_integer fn
257 $contributionTypeId = CRM_Utils_Array
::key ( ucfirst ( $value ), CRM_Contribute_PseudoConstant
::financialType() );
258 if ($contributionTypeId) {
259 if (CRM_Utils_Array
::value('financial_type_id', $values) && $contributionTypeId != $values['financial_type_id']) {
260 throw new Exception("Mismatched Financial Type and Financial Type Id");
262 $values ['financial_type_id'] = $contributionTypeId;
265 throw new Exception("Invalid Financial Type");
269 case 'soft_credit_to':// should be dealt with by validate integer
270 if (!CRM_Utils_Rule
::integer($value)) {
271 return civicrm_api3_create_error("$key not a valid Id: $value");
273 $values['soft_credit_to'] = $value;
285 * Process a transaction and record it against the contact.
287 * @param array $params (reference ) input parameters
289 * @return array (reference ) contribution of created or updated record (or a civicrm error)
294 function civicrm_api3_contribution_transact($params) {
295 $required = array('amount');
296 foreach ($required as $key) {
297 if (!isset($params[$key])) {
298 return civicrm_api3_create_error("Missing parameter $key: civicrm_contribute_transact() requires a parameter '$key'.");
302 // allow people to omit some values for convenience
303 // 'payment_processor_id' => NULL /* we could retrieve the default processor here, but only if it's missing to avoid an extra lookup */
305 'payment_processor_mode' => 'live',
307 $params = array_merge($defaults, $params);
309 // clean up / adjust some values which
310 if (!isset($params['total_amount'])) {
311 $params['total_amount'] = $params['amount'];
313 if (!isset($params['net_amount'])) {
314 $params['net_amount'] = $params['amount'];
316 if (!isset($params['receive_date'])) {
317 $params['receive_date'] = date('Y-m-d');
319 if (!isset($params['invoiceID']) && isset($params['invoice_id'])) {
320 $params['invoiceID'] = $params['invoice_id'];
323 require_once 'CRM/Financial/BAO/PaymentProcessor.php';
324 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($params['payment_processor_id'], $params['payment_processor_mode']);
325 if (civicrm_error($paymentProcessor)) {
326 return $paymentProcessor;
329 require_once 'CRM/Core/Payment.php';
330 $payment = &CRM_Core_Payment
::singleton($params['payment_processor_mode'], $paymentProcessor);
331 if (civicrm_error($payment)) {
335 $transaction = $payment->doDirectPayment($params);
336 if (civicrm_error($transaction)) {
340 // but actually, $payment->doDirectPayment() doesn't return a
341 // CRM_Core_Error by itself
342 if (get_class($transaction) == 'CRM_Core_Error') {
343 $errs = $transaction->getErrors();
345 $last_error = array_shift($errs);
346 return CRM_Core_Error
::createApiError($last_error['message']);
350 $contribution = civicrm_api('contribution', 'create', $params);
351 return $contribution['values'];
354 * Send a contribution confirmation (receipt or invoice)
355 * The appropriate online template will be used (the existence of related objects
356 * (e.g. memberships ) will affect this selection
357 * @param array $params input parameters
358 * {@getfields Contribution_sendconfirmation}
359 * @return array Api result array
364 function civicrm_api3_contribution_sendconfirmation($params) {
365 $contribution = new CRM_Contribute_BAO_Contribution();
366 $contribution->id
= $params['id'];
367 if (! $contribution->find(true)) {
368 throw new Exception('Contribution does not exist');
370 $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']);
371 $contribution->loadRelatedObjects($input, $ids, FALSE, true);
372 $contribution->composeMessageArray($input, $ids, $cvalues, false, false);
376 * Adjust Metadata for Create action
378 * The metadata is used for setting defaults, documentation & validation
379 * @param array $params array or parameters determined by getfields
381 function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
382 $params['id'] = array(
384 'title' => 'Contribution ID'
386 $params['receipt_from_email'] = array(
388 'title' => 'From Email (required until someone provides a patch :-)',