Update debugging page text
[civicrm-core.git] / api / v3 / Contribution.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
b081365f 29 * This api exposes CiviCRM Contribution records.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
34/**
9d32e6f7 35 * Add or update a contribution.
6a488035 36 *
cf470720 37 * @param array $params
9d32e6f7 38 * Input parameters.
6a488035 39 *
69dbd1ba 40 * @throws API_Exception
a6c01b45 41 * @return array
72b3a70c 42 * Api result array
6a488035
TO
43 */
44function civicrm_api3_contribution_create(&$params) {
45 $values = array();
6a488035 46 _civicrm_api3_custom_format_params($params, $values, 'Contribution');
504a78f6 47 $params = array_merge($params, $values);
48
8cc574cf 49 if (!empty($params['id']) && !empty($params['contribution_status_id'])) {
504a78f6 50 $error = array();
51 //throw error for invalid status change such as setting completed back to pending
52 //@todo this sort of validation belongs in the BAO not the API - if it is not an OK
53 // action it needs to be blocked there. If it is Ok through a form it needs to be OK through the api
54 CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error);
55 if (array_key_exists('contribution_status_id', $error)) {
56 throw new API_Exception($error['contribution_status_id']);
57 }
6a488035 58 }
6a516bd6
DG
59
60 _civicrm_api3_contribution_create_legacy_support_45($params);
61
9d32e6f7 62 // Make sure tax calculation is handled via api.
b5935203 63 $params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params);
ebf2b57b 64
504a78f6 65 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution');
6a488035 66}
11e09c59
TO
67
68/**
0aa0303c
EM
69 * Adjust Metadata for Create action.
70 *
71 * The metadata is used for setting defaults, documentation & validation.
6a488035 72 *
cf470720 73 * @param array $params
b081365f 74 * Array of parameters determined by getfields.
6a488035
TO
75 */
76function _civicrm_api3_contribution_create_spec(&$params) {
77 $params['contact_id']['api.required'] = 1;
78 $params['total_amount']['api.required'] = 1;
53191813 79 $params['payment_instrument_id']['api.aliases'] = array('payment_instrument');
8757a378 80 $params['receive_date']['api.default'] = 'now';
6a488035
TO
81 $params['payment_processor'] = array(
82 'name' => 'payment_processor',
83 'title' => 'Payment Processor ID',
84 'description' => 'ID of payment processor used for this contribution',
85 // field is called payment processor - not payment processor id but can only be one id so
86 // it seems likely someone will fix it up one day to be more consistent - lets alias it from the start
87 'api.aliases' => array('payment_processor_id'),
88 );
89 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
90 $params['financial_type_id']['api.required'] = 1;
91 $params['note'] = array(
92 'name' => 'note',
93 'uniqueName' => 'contribution_note',
94 'title' => 'note',
95 'type' => 2,
96 'description' => 'Associated Note in the notes table',
97 );
98 $params['soft_credit_to'] = array(
99 'name' => 'soft_credit_to',
33a429d4 100 'title' => 'Soft Credit contact ID (legacy)',
6a488035 101 'type' => 1,
33a429d4 102 'description' => 'ID of Contact to be Soft credited to (deprecated - use contribution_soft api)',
6a488035
TO
103 'FKClassName' => 'CRM_Contact_DAO_Contact',
104 );
6a516bd6
DG
105 $params['honor_contact_id'] = array(
106 'name' => 'honor_contact_id',
33a429d4 107 'title' => 'Honoree contact ID (legacy)',
6a516bd6 108 'type' => 1,
33a429d4 109 'description' => 'ID of honoree contact (deprecated - use contribution_soft api)',
6a516bd6
DG
110 'FKClassName' => 'CRM_Contact_DAO_Contact',
111 );
33a429d4
CW
112 $params['honor_type_id'] = array(
113 'name' => 'honor_type_id',
114 'title' => 'Honoree Type (legacy)',
115 'type' => 1,
116 'description' => 'Type of honoree contact (deprecated - use contribution_soft api)',
117 'pseudoconstant' => TRUE,
118 );
6a488035 119 // note this is a recommended option but not adding as a default to avoid
c206647d 120 // creating unnecessary changes for the dev
6a488035
TO
121 $params['skipRecentView'] = array(
122 'name' => 'skipRecentView',
123 'title' => 'Skip adding to recent view',
0900b21e 124 'type' => CRM_Utils_Type::T_BOOLEAN,
6a488035
TO
125 'description' => 'Do not add to recent view (setting this improves performance)',
126 );
127 $params['skipLineItem'] = array(
128 'name' => 'skipLineItem',
129 'title' => 'Skip adding line items',
130 'type' => 1,
131 'api.default' => 0,
132 'description' => 'Do not add line items by default (if you wish to add your own)',
133 );
599c61ac
E
134 $params['batch_id'] = array(
135 'title' => 'Batch',
136 'type' => 1,
137 'description' => 'Batch which relevant transactions should be added to',
138 );
6a488035
TO
139}
140
6a516bd6 141/**
9d32e6f7
EM
142 * Support for schema changes made in 4.5.
143 *
35671d00
TO
144 * The main purpose of the API is to provide integrators a level of stability not provided by
145 * the core code or schema - this means we have to provide support for api calls (where possible)
146 * across schema changes.
dc64d047 147 *
d0997921 148 * @param array $params
35671d00 149 */
9b873358 150function _civicrm_api3_contribution_create_legacy_support_45(&$params) {
6a516bd6 151 //legacy soft credit handling - recommended approach is chaining
9b873358 152 if (!empty($params['soft_credit_to'])) {
33a429d4 153 $params['soft_credit'][] = array(
6a516bd6
DG
154 'contact_id' => $params['soft_credit_to'],
155 'amount' => $params['total_amount'],
21dfd5f5 156 'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
33a429d4 157 );
6a516bd6 158 }
9b873358 159 if (!empty($params['honor_contact_id'])) {
33a429d4 160 $params['soft_credit'][] = array(
6a516bd6
DG
161 'contact_id' => $params['honor_contact_id'],
162 'amount' => $params['total_amount'],
21dfd5f5 163 'soft_credit_type_id' => CRM_Utils_Array::value('honor_type_id', $params, CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name')),
33a429d4 164 );
6a516bd6
DG
165 }
166}
167
6a488035 168/**
9d32e6f7 169 * Delete a contribution.
6a488035 170 *
cf470720 171 * @param array $params
9d32e6f7 172 * Input parameters.
6a488035 173 *
9d32e6f7 174 * @return array
6a488035
TO
175 */
176function civicrm_api3_contribution_delete($params) {
177
0d8afee2 178 $contributionID = !empty($params['contribution_id']) ? $params['contribution_id'] : $params['id'];
6a488035
TO
179 if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) {
180 return civicrm_api3_create_success(array($contributionID => 1));
181 }
182 else {
183 return civicrm_api3_create_error('Could not delete contribution');
184 }
185}
11e09c59
TO
186
187/**
9d32e6f7
EM
188 * Modify metadata for delete action.
189 *
190 * Legacy support for contribution_id.
191 *
d0997921 192 * @param array $params
6a488035
TO
193 */
194function _civicrm_api3_contribution_delete_spec(&$params) {
195 $params['id']['api.aliases'] = array('contribution_id');
196}
197
198/**
35823763 199 * Retrieve a set of contributions.
6a488035 200 *
cf470720 201 * @param array $params
35823763 202 * Input parameters.
69dbd1ba 203 *
a6c01b45 204 * @return array
16b10e64 205 * Array of contributions, if error an array with an error id and error message
6a488035
TO
206 */
207function civicrm_api3_contribution_get($params) {
208
82f7d8b2
EM
209 $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
210 $entity = 'contribution';
211 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity);
6a488035
TO
212
213 $contribution = array();
214 while ($dao->fetch()) {
215 //CRM-8662
76ca3345 216 $contribution_details = $query->store($dao);
35671d00 217 $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dao->contribution_id, TRUE);
76ca3345 218 $contribution[$dao->contribution_id] = array_merge($contribution_details, $softContribution);
76ca3345
KJ
219 // format soft credit for backward compatibility
220 _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]);
6a488035
TO
221 }
222 return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao);
223}
11e09c59 224
76ca3345 225/**
1747ab99
EM
226 * This function is used to format the soft credit for backward compatibility.
227 *
228 * As of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key
76ca3345 229 * but we still return first soft credit as a part of contribution array
dc64d047 230 *
645ee340 231 * @param $contribution
76ca3345
KJ
232 */
233function _civicrm_api3_format_soft_credit(&$contribution) {
234 if (!empty($contribution['soft_credit'])) {
235 $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id'];
236 $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id'];
237 }
238}
239
11e09c59 240/**
1747ab99 241 * Adjust Metadata for Get action.
6a488035 242 *
0aa0303c
EM
243 * The metadata is used for setting defaults, documentation & validation.
244 *
cf470720 245 * @param array $params
b081365f 246 * Array of parameters determined by getfields.
6a488035
TO
247 */
248function _civicrm_api3_contribution_get_spec(&$params) {
249 $params['contribution_test']['api.default'] = 0;
4c41ecb2 250 $params['contribution_test']['title'] = 'Get Test Contributions?';
6a488035
TO
251 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
252 $params['contact_id'] = $params['contribution_contact_id'];
253 $params['contact_id']['api.aliases'] = array('contribution_contact_id');
254 unset($params['contribution_contact_id']);
255}
256
257/**
1747ab99
EM
258 * Legacy handling for contribution parameters.
259 *
260 * Take the input parameter list as specified in the data model and
261 * convert it into the same format that we use in QF and BAO object.
6a488035 262 *
cf470720 263 * @param array $params
c23f45d3 264 * property name/value pairs to insert in new contact.
cf470720
TO
265 * @param array $values
266 * The reformatted properties that we can use internally.
6a488035 267 *
c23f45d3 268 * @return array
6a488035 269 */
c23f45d3 270function _civicrm_api3_contribute_format_params($params, &$values) {
35671d00 271 //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
6a488035 272 _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
6a488035
TO
273 return array();
274}
275
16c0ec8d 276/**
1747ab99 277 * Adjust Metadata for Transact action.
16c0ec8d 278 *
0aa0303c
EM
279 * The metadata is used for setting defaults, documentation & validation.
280 *
cf470720 281 * @param array $params
b081365f 282 * Array of parameters determined by getfields.
16c0ec8d
CW
283 */
284function _civicrm_api3_contribution_transact_spec(&$params) {
4c41ecb2
EM
285 $fields = civicrm_api3('contribution', 'getfields', array('action' => 'create'));
286 $params = array_merge($params, $fields['values']);
16c0ec8d
CW
287 $params['receive_date']['api.default'] = 'now';
288}
289
6a488035
TO
290/**
291 * Process a transaction and record it against the contact.
292 *
cf470720 293 * @param array $params
1747ab99 294 * Input parameters.
6a488035 295 *
a6c01b45 296 * @return array
c23f45d3 297 * contribution of created or updated record (or a civicrm error)
6a488035
TO
298 */
299function civicrm_api3_contribution_transact($params) {
16c0ec8d 300 // Set some params specific to payment processing
8319cf11
EM
301 // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with
302 // 'is_error' set
303 // also trxn_id is not saved.
304 // but since there is no test it's not desirable to jump in & make the obvious changes.
16c0ec8d
CW
305 $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test';
306 $params['amount'] = $params['total_amount'];
6a488035
TO
307 if (!isset($params['net_amount'])) {
308 $params['net_amount'] = $params['amount'];
309 }
6a488035
TO
310 if (!isset($params['invoiceID']) && isset($params['invoice_id'])) {
311 $params['invoiceID'] = $params['invoice_id'];
312 }
313
be06e507
CW
314 // Some payment processors expect a unique invoice_id - generate one if not supplied
315 $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE)));
316
16c0ec8d 317 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']);
6a488035
TO
318 if (civicrm_error($paymentProcessor)) {
319 return $paymentProcessor;
320 }
321
16c0ec8d 322 $payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor);
6a488035
TO
323 if (civicrm_error($payment)) {
324 return $payment;
325 }
326
8319cf11 327 $transaction = $payment->doPayment($params);
6a488035 328
26ad2b72 329 $params['payment_instrument_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', $paymentProcessor['payment_processor_type_id'], 'payment_type') == 1 ? 'Credit Card' : 'Debit Card';
16c0ec8d 330 return civicrm_api('contribution', 'create', $params);
6a488035 331}
69dbd1ba 332
6a488035 333/**
1747ab99
EM
334 * Send a contribution confirmation (receipt or invoice).
335 *
6a488035
TO
336 * The appropriate online template will be used (the existence of related objects
337 * (e.g. memberships ) will affect this selection
69dbd1ba 338 *
cf470720
TO
339 * @param array $params
340 * Input parameters.
69dbd1ba
EM
341 *
342 * @throws Exception
6a488035
TO
343 */
344function civicrm_api3_contribution_sendconfirmation($params) {
345 $contribution = new CRM_Contribute_BAO_Contribution();
346 $contribution->id = $params['id'];
acb1052e 347 if (!$contribution->find(TRUE)) {
6a488035 348 throw new Exception('Contribution does not exist');
35671d00 349 }
6a488035 350 $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']);
76ca3345
KJ
351 $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE);
352 $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE);
6a488035
TO
353}
354
11e09c59 355/**
1747ab99 356 * Adjust Metadata for sendconfirmation action.
6a488035 357 *
0aa0303c
EM
358 * The metadata is used for setting defaults, documentation & validation.
359 *
cf470720 360 * @param array $params
b081365f 361 * Array of parameters determined by getfields.
6a488035
TO
362 */
363function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
364 $params['id'] = array(
365 'api.required' => 1,
21dfd5f5 366 'title' => 'Contribution ID',
6a488035
TO
367 );
368 $params['receipt_from_email'] = array(
35671d00 369 'api.required' => 1,
6a0cf2c6
CW
370 'title' => 'From Email address (string) required until someone provides a patch :-)',
371 );
372 $params['receipt_from_name'] = array(
373 'title' => 'From Name (string)',
374 );
375 $params['cc_receipt'] = array(
376 'title' => 'CC Email address (string)',
377 );
378 $params['bcc_receipt'] = array(
379 'title' => 'BCC Email address (string)',
380 );
381 $params['receipt_text'] = array(
382 'title' => 'Message (string)',
6a488035
TO
383 );
384}
0efa8efe 385
386/**
1747ab99
EM
387 * Complete an existing (pending) transaction.
388 *
389 * This will update related entities (participant, membership, pledge etc)
390 * and take any complete actions from the contribution page (e.g. send receipt).
0efa8efe 391 *
392 * @todo - most of this should live in the BAO layer but as we want it to be an addition
393 * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later
394 *
cf470720
TO
395 * @param array $params
396 * Input parameters.
69dbd1ba
EM
397 *
398 * @throws API_Exception
acb1052e 399 * Api result array.
0efa8efe 400 */
401function civicrm_api3_contribution_completetransaction(&$params) {
402
403 $input = $ids = array();
404 $contribution = new CRM_Contribute_BAO_Contribution();
405 $contribution->id = $params['id'];
406 $contribution->find(TRUE);
9b873358 407 if (!$contribution->id == $params['id']) {
0efa8efe 408 throw new API_Exception('A valid contribution ID is required', 'invalid_data');
409 }
410 try {
9b873358 411 if (!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)) {
0efa8efe 412 throw new API_Exception('failed to load related objects');
413 }
1ba809f7 414 elseif ($contribution->contribution_status_id == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) {
9e2ee07b
EM
415 throw new API_Exception(ts('Contribution already completed'));
416 }
0efa8efe 417 $objects = $contribution->_relatedObjects;
418 $objects['contribution'] = &$contribution;
419 $input['component'] = $contribution->_component;
420 $input['is_test'] = $contribution->is_test;
35671d00 421 $input['trxn_id'] = !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id;
0efa8efe 422 $input['amount'] = $contribution->total_amount;
9b873358 423 if (isset($params['is_email_receipt'])) {
0efa8efe 424 $input['is_email_receipt'] = $params['is_email_receipt'];
425 }
426 // @todo required for base ipn but problematic as api layer handles this
427 $transaction = new CRM_Core_Transaction();
428 $ipn = new CRM_Core_Payment_BaseIPN();
f6780fb8 429 $ipn->completeTransaction($input, $ids, $objects, $transaction, !empty($contribution->contribution_recur_id));
0efa8efe 430 }
0e37ad72 431 catch(Exception $e) {
0efa8efe 432 throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString());
433 }
434}
435
aa1b1481 436/**
9d32e6f7
EM
437 * Provide function metadata.
438 *
c490a46a 439 * @param array $params
aa1b1481 440 */
3dfbdece 441function _civicrm_api3_contribution_completetransaction_spec(&$params) {
eac25df2
EM
442 $params['id'] = array(
443 'title' => 'Contribution ID',
444 'type' => CRM_Utils_Type::T_INT,
445 'api.required' => TRUE,
446 );
447 $params['trxn_id'] = array(
448 'title' => 'Transaction ID',
449 'type' => CRM_Utils_Type::T_STRING,
450 );
451 $params['is_email_receipt'] = array(
452 'title' => 'Send email Receipt?',
453 'type' => CRM_Utils_Type::T_BOOLEAN,
454 );
0efa8efe 455}