4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * File for the CiviCRM APIv3 Contribution functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_Contribute
35 * @copyright CiviCRM LLC (c) 2004-2013
36 * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $
41 * Add or update a contribution
43 * @param array $params (reference ) input parameters
45 * @return array Api result array
48 * @example ContributionCreate.php
49 * {@getfields Contribution_create}
51 function civicrm_api3_contribution_create(&$params) {
54 _civicrm_api3_contribute_format_params($params, $values);
56 _civicrm_api3_custom_format_params($params, $values, 'Contribution');
57 $values["contact_id"] = CRM_Utils_Array
::value('contact_id', $params);
58 $values["source"] = CRM_Utils_Array
::value('source', $params);
59 //legacy soft credit handling
60 if(!empty($params['soft_credit_to'])){
61 $values['soft_credit'] = array(array(
62 'contact_id' => $params['soft_credit_to'],
63 'amount' => $params['total_amount']));
66 if (CRM_Utils_Array
::value('id', $params)) {
67 $ids['contribution'] = $params['id'];
69 if (CRM_Utils_Array
::value('contribution_status_id', $params)) {
71 //throw error for invalid status change
72 CRM_Contribute_BAO_Contribution
::checkStatusValidation(NULL, $params, $error);
73 if (array_key_exists('contribution_status_id', $error)) {
74 return civicrm_api3_create_error($error['contribution_status_id']);
78 $contribution = CRM_Contribute_BAO_Contribution
::create($values, $ids);
80 if (is_a($contribution, 'CRM_Core_Error')) {
81 return civicrm_api3_create_error($contribution->_errors
[0]['message']);
83 _civicrm_api3_object_to_array($contribution, $contributeArray[$contribution->id
]);
85 return civicrm_api3_create_success($contributeArray, $params, 'contribution', 'create', $contribution);
89 * Adjust Metadata for Create action
91 * The metadata is used for setting defaults, documentation & validation
92 * @param array $params array or parameters determined by getfields
94 function _civicrm_api3_contribution_create_spec(&$params) {
95 $params['contact_id']['api.required'] = 1;
96 $params['total_amount']['api.required'] = 1;
97 $params['payment_instrument_id']['api.aliases'] = array('payment_instrument');
98 $params['payment_processor'] = array(
99 'name' => 'payment_processor',
100 'title' => 'Payment Processor ID',
101 'description' => 'ID of payment processor used for this contribution',
102 // field is called payment processor - not payment processor id but can only be one id so
103 // it seems likely someone will fix it up one day to be more consistent - lets alias it from the start
104 'api.aliases' => array('payment_processor_id'),
106 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
107 $params['financial_type_id']['api.required'] = 1;
108 $params['note'] = array(
110 'uniqueName' => 'contribution_note',
113 'description' => 'Associated Note in the notes table',
115 $params['soft_credit_to'] = array(
116 'name' => 'soft_credit_to',
117 'title' => 'Soft Credit contact ID',
119 'description' => 'ID of Contact to be Soft credited to',
120 'FKClassName' => 'CRM_Contact_DAO_Contact',
122 // note this is a recommended option but not adding as a default to avoid
123 // creating unecessary changes for the dev
124 $params['skipRecentView'] = array(
125 'name' => 'skipRecentView',
126 'title' => 'Skip adding to recent view',
128 'description' => 'Do not add to recent view (setting this improves performance)',
130 $params['skipLineItem'] = array(
131 'name' => 'skipLineItem',
132 'title' => 'Skip adding line items',
135 'description' => 'Do not add line items by default (if you wish to add your own)',
140 * Delete a contribution
142 * @param array $params (reference ) input parameters
144 * @return boolean true if success, else false
147 * {@getfields Contribution_delete}
148 * @example ContributionDelete.php
150 function civicrm_api3_contribution_delete($params) {
152 $contributionID = CRM_Utils_Array
::value('contribution_id', $params) ?
$params['contribution_id'] : $params['id'];
153 if (CRM_Contribute_BAO_Contribution
::deleteContribution($contributionID)) {
154 return civicrm_api3_create_success(array($contributionID => 1));
157 return civicrm_api3_create_error('Could not delete contribution');
162 * modify metadata. Legacy support for contribution_id
164 function _civicrm_api3_contribution_delete_spec(&$params) {
165 $params['id']['api.aliases'] = array('contribution_id');
169 * Retrieve a set of contributions, given a set of input params
171 * @param array $params (reference ) input parameters
172 * @param array $returnProperties Which properties should be included in the
173 * returned Contribution object. If NULL, the default
174 * set of properties will be included.
176 * @return array (reference ) array of contributions, if error an array with an error id and error message
179 * {@getfields Contribution_get}
180 * @example ContributionGet.php
182 function civicrm_api3_contribution_get($params) {
184 $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get');
185 $sort = CRM_Utils_Array
::value('sort', $options, NULL);
186 $offset = CRM_Utils_Array
::value('offset', $options);
187 $rowCount = CRM_Utils_Array
::value('limit', $options);
188 $smartGroupCache = CRM_Utils_Array
::value('smartGroupCache', $params);
189 $inputParams = CRM_Utils_Array
::value('input_params', $options, array());
190 $returnProperties = CRM_Utils_Array
::value('return', $options, NULL);
191 if (empty($returnProperties)) {
192 $returnProperties = CRM_Contribute_BAO_Query
::defaultReturnProperties(CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
195 $newParams = CRM_Contact_BAO_Query
::convertFormValues($inputParams);
196 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
197 FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
199 list($select, $from, $where, $having) = $query->query();
201 $sql = "$select $from $where $having";
204 $sql .= " ORDER BY $sort ";
206 $sql .= " LIMIT $offset, $rowCount ";
207 $dao = CRM_Core_DAO
::executeQuery($sql);
209 $contribution = array();
210 while ($dao->fetch()) {
212 $contribution_details = $query->store($dao);
213 $softContribution = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($dao->contribution_id
, TRUE);
214 $contribution[$dao->contribution_id
] = array_merge($contribution_details, $softContribution);
215 if(isset($contribution[$dao->contribution_id
]['financial_type_id'])){
216 $contribution[$dao->contribution_id
]['financial_type_id'] = $contribution[$dao->contribution_id
]['financial_type_id'];
218 // format soft credit for backward compatibility
219 _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id
]);
221 return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao);
225 * This function is used to format the soft credit for backward compatibility
226 * as of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key
227 * but we still return first soft credit as a part of contribution array
229 function _civicrm_api3_format_soft_credit(&$contribution) {
230 if (!empty($contribution['soft_credit'])) {
231 $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id'];
232 $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id'];
237 * Adjust Metadata for Get action
239 * The metadata is used for setting defaults, documentation & validation
240 * @param array $params array or parameters determined by getfields
242 function _civicrm_api3_contribution_get_spec(&$params) {
243 $params['contribution_test']['api.default'] = 0;
244 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
245 $params['contact_id'] = $params['contribution_contact_id'];
246 $params['contact_id']['api.aliases'] = array('contribution_contact_id');
247 unset($params['contribution_contact_id']);
251 * take the input parameter list as specified in the data model and
252 * convert it into the same format that we use in QF and BAO object
254 * @param array $params Associative array of property name/value
255 * pairs to insert in new contact.
256 * @param array $values The reformatted properties that we can use internally
259 * @return array|CRM_Error
262 function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) {
263 //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
264 _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
269 * Process a transaction and record it against the contact.
271 * @param array $params (reference ) input parameters
273 * @return array (reference ) contribution of created or updated record (or a civicrm error)
278 function civicrm_api3_contribution_transact($params) {
279 $required = array('amount');
280 foreach ($required as $key) {
281 if (!isset($params[$key])) {
282 return civicrm_api3_create_error("Missing parameter $key: civicrm_contribute_transact() requires a parameter '$key'.");
286 // allow people to omit some values for convenience
287 // 'payment_processor_id' => NULL /* we could retrieve the default processor here, but only if it's missing to avoid an extra lookup */
289 'payment_processor_mode' => 'live',
291 $params = array_merge($defaults, $params);
293 // clean up / adjust some values which
294 if (!isset($params['total_amount'])) {
295 $params['total_amount'] = $params['amount'];
297 if (!isset($params['net_amount'])) {
298 $params['net_amount'] = $params['amount'];
300 if (!isset($params['receive_date'])) {
301 $params['receive_date'] = date('Y-m-d');
303 if (!isset($params['invoiceID']) && isset($params['invoice_id'])) {
304 $params['invoiceID'] = $params['invoice_id'];
307 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($params['payment_processor_id'], $params['payment_processor_mode']);
308 if (civicrm_error($paymentProcessor)) {
309 return $paymentProcessor;
312 $payment = &CRM_Core_Payment
::singleton($params['payment_processor_mode'], $paymentProcessor);
313 if (civicrm_error($payment)) {
317 $transaction = $payment->doDirectPayment($params);
318 if (civicrm_error($transaction)) {
322 // but actually, $payment->doDirectPayment() doesn't return a
323 // CRM_Core_Error by itself
324 if (get_class($transaction) == 'CRM_Core_Error') {
325 $errs = $transaction->getErrors();
327 $last_error = array_shift($errs);
328 return CRM_Core_Error
::createApiError($last_error['message']);
332 $contribution = civicrm_api('contribution', 'create', $params);
333 return $contribution['values'];
336 * Send a contribution confirmation (receipt or invoice)
337 * The appropriate online template will be used (the existence of related objects
338 * (e.g. memberships ) will affect this selection
339 * @param array $params input parameters
340 * {@getfields Contribution_sendconfirmation}
341 * @return array Api result array
346 function civicrm_api3_contribution_sendconfirmation($params) {
347 $contribution = new CRM_Contribute_BAO_Contribution();
348 $contribution->id
= $params['id'];
349 if (! $contribution->find(TRUE)) {
350 throw new Exception('Contribution does not exist');
352 $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']);
353 $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE);
354 $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE);
358 * Adjust Metadata for Create action
360 * The metadata is used for setting defaults, documentation & validation
361 * @param array $params array or parameters determined by getfields
363 function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
364 $params['id'] = array(
366 'title' => 'Contribution ID'
368 $params['receipt_from_email'] = array(
370 'title' => 'From Email (required until someone provides a patch :-)',
376 * Complete an existing (pending) transaction, updating related entities (participant, membership, pledge etc)
377 * and taking any complete actions from the contribution page (e.g. send receipt)
379 * @todo - most of this should live in the BAO layer but as we want it to be an addition
380 * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later
382 * @param array $params input parameters
383 * {@getfields Contribution_completetransaction}
384 * @return array Api result array
389 function civicrm_api3_contribution_completetransaction(&$params) {
391 $input = $ids = array();
392 $contribution = new CRM_Contribute_BAO_Contribution();
393 $contribution->id
= $params['id'];
394 $contribution->find(TRUE);
395 if(!$contribution->id
== $params['id']){
396 throw new API_Exception('A valid contribution ID is required', 'invalid_data');
399 if(!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)){
400 throw new API_Exception('failed to load related objects');
402 $objects = $contribution->_relatedObjects
;
403 $objects['contribution'] = &$contribution;
404 $input['component'] = $contribution->_component
;
405 $input['is_test'] = $contribution->is_test
;
406 $input['trxn_id']= $contribution->trxn_id
;
407 $input['amount'] = $contribution->total_amount
;
408 if(isset($params['is_email_receipt'])){
409 $input['is_email_receipt'] = $params['is_email_receipt'];
411 // @todo required for base ipn but problematic as api layer handles this
412 $transaction = new CRM_Core_Transaction();
413 $ipn = new CRM_Core_Payment_BaseIPN();
414 $ipn->completeTransaction($input, $ids, $objects, $transaction);
417 throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString());
421 function _civicrm_api3_contribution_completetransaction(&$params) {