Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * File for the CiviCRM APIv3 Contribution functions | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_Contribute | |
34 | * | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $ |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Add or update a contribution | |
42 | * | |
69dbd1ba | 43 | * @param array $params (reference ) input parameters |
6a488035 | 44 | * |
69dbd1ba | 45 | * @throws API_Exception |
6a488035 TO |
46 | * @return array Api result array |
47 | * @static void | |
48 | * @access public | |
49 | * @example ContributionCreate.php | |
50 | * {@getfields Contribution_create} | |
51 | */ | |
52 | function civicrm_api3_contribution_create(&$params) { | |
53 | $values = array(); | |
6a488035 | 54 | _civicrm_api3_custom_format_params($params, $values, 'Contribution'); |
504a78f6 | 55 | $params = array_merge($params, $values); |
56 | ||
57 | //legacy soft credit handling - recommended approach is chaining | |
502b3d42 | 58 | if(!empty($params['soft_credit_to'])){ |
504a78f6 | 59 | $params['soft_credit'] = array(array( |
502b3d42 | 60 | 'contact_id' => $params['soft_credit_to'], |
61 | 'amount' => $params['total_amount'])); | |
62 | } | |
6a488035 | 63 | |
8cc574cf | 64 | if (!empty($params['id']) && !empty($params['contribution_status_id'])) { |
504a78f6 | 65 | $error = array(); |
66 | //throw error for invalid status change such as setting completed back to pending | |
67 | //@todo this sort of validation belongs in the BAO not the API - if it is not an OK | |
68 | // action it needs to be blocked there. If it is Ok through a form it needs to be OK through the api | |
69 | CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error); | |
70 | if (array_key_exists('contribution_status_id', $error)) { | |
71 | throw new API_Exception($error['contribution_status_id']); | |
72 | } | |
6a488035 | 73 | } |
504a78f6 | 74 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution'); |
6a488035 | 75 | } |
11e09c59 TO |
76 | |
77 | /** | |
6a488035 TO |
78 | * Adjust Metadata for Create action |
79 | * | |
80 | * The metadata is used for setting defaults, documentation & validation | |
81 | * @param array $params array or parameters determined by getfields | |
82 | */ | |
83 | function _civicrm_api3_contribution_create_spec(&$params) { | |
84 | $params['contact_id']['api.required'] = 1; | |
85 | $params['total_amount']['api.required'] = 1; | |
53191813 | 86 | $params['payment_instrument_id']['api.aliases'] = array('payment_instrument'); |
8757a378 | 87 | $params['receive_date']['api.default'] = 'now'; |
6a488035 TO |
88 | $params['payment_processor'] = array( |
89 | 'name' => 'payment_processor', | |
90 | 'title' => 'Payment Processor ID', | |
91 | 'description' => 'ID of payment processor used for this contribution', | |
92 | // field is called payment processor - not payment processor id but can only be one id so | |
93 | // it seems likely someone will fix it up one day to be more consistent - lets alias it from the start | |
94 | 'api.aliases' => array('payment_processor_id'), | |
95 | ); | |
96 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); | |
97 | $params['financial_type_id']['api.required'] = 1; | |
98 | $params['note'] = array( | |
99 | 'name' => 'note', | |
100 | 'uniqueName' => 'contribution_note', | |
101 | 'title' => 'note', | |
102 | 'type' => 2, | |
103 | 'description' => 'Associated Note in the notes table', | |
104 | ); | |
105 | $params['soft_credit_to'] = array( | |
106 | 'name' => 'soft_credit_to', | |
107 | 'title' => 'Soft Credit contact ID', | |
108 | 'type' => 1, | |
109 | 'description' => 'ID of Contact to be Soft credited to', | |
110 | 'FKClassName' => 'CRM_Contact_DAO_Contact', | |
111 | ); | |
112 | // note this is a recommended option but not adding as a default to avoid | |
113 | // creating unecessary changes for the dev | |
114 | $params['skipRecentView'] = array( | |
115 | 'name' => 'skipRecentView', | |
116 | 'title' => 'Skip adding to recent view', | |
0900b21e | 117 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
6a488035 TO |
118 | 'description' => 'Do not add to recent view (setting this improves performance)', |
119 | ); | |
120 | $params['skipLineItem'] = array( | |
121 | 'name' => 'skipLineItem', | |
122 | 'title' => 'Skip adding line items', | |
123 | 'type' => 1, | |
124 | 'api.default' => 0, | |
125 | 'description' => 'Do not add line items by default (if you wish to add your own)', | |
126 | ); | |
599c61ac E |
127 | $params['batch_id'] = array( |
128 | 'title' => 'Batch', | |
129 | 'type' => 1, | |
130 | 'description' => 'Batch which relevant transactions should be added to', | |
131 | ); | |
6a488035 TO |
132 | } |
133 | ||
134 | /** | |
135 | * Delete a contribution | |
136 | * | |
137 | * @param array $params (reference ) input parameters | |
138 | * | |
139 | * @return boolean true if success, else false | |
140 | * @static void | |
141 | * @access public | |
142 | * {@getfields Contribution_delete} | |
143 | * @example ContributionDelete.php | |
144 | */ | |
145 | function civicrm_api3_contribution_delete($params) { | |
146 | ||
0d8afee2 | 147 | $contributionID = !empty($params['contribution_id']) ? $params['contribution_id'] : $params['id']; |
6a488035 TO |
148 | if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) { |
149 | return civicrm_api3_create_success(array($contributionID => 1)); | |
150 | } | |
151 | else { | |
152 | return civicrm_api3_create_error('Could not delete contribution'); | |
153 | } | |
154 | } | |
11e09c59 TO |
155 | |
156 | /** | |
6a488035 TO |
157 | * modify metadata. Legacy support for contribution_id |
158 | */ | |
159 | function _civicrm_api3_contribution_delete_spec(&$params) { | |
160 | $params['id']['api.aliases'] = array('contribution_id'); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Retrieve a set of contributions, given a set of input params | |
165 | * | |
69dbd1ba EM |
166 | * @param array $params (reference ) input parameters |
167 | * | |
168 | * @internal param array $returnProperties Which properties should be included in the | |
6a488035 TO |
169 | * returned Contribution object. If NULL, the default |
170 | * set of properties will be included. | |
171 | * | |
172 | * @return array (reference ) array of contributions, if error an array with an error id and error message | |
173 | * @static void | |
174 | * @access public | |
175 | * {@getfields Contribution_get} | |
176 | * @example ContributionGet.php | |
177 | */ | |
178 | function civicrm_api3_contribution_get($params) { | |
179 | ||
82f7d8b2 EM |
180 | $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
181 | $entity = 'contribution'; | |
182 | list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity); | |
6a488035 TO |
183 | |
184 | $contribution = array(); | |
185 | while ($dao->fetch()) { | |
186 | //CRM-8662 | |
76ca3345 KJ |
187 | $contribution_details = $query->store($dao); |
188 | $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dao->contribution_id , TRUE); | |
189 | $contribution[$dao->contribution_id] = array_merge($contribution_details, $softContribution); | |
a1c68fd2 | 190 | if(isset($contribution[$dao->contribution_id]['financial_type_id'])){ |
191 | $contribution[$dao->contribution_id]['financial_type_id'] = $contribution[$dao->contribution_id]['financial_type_id']; | |
192 | } | |
76ca3345 KJ |
193 | // format soft credit for backward compatibility |
194 | _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]); | |
6a488035 TO |
195 | } |
196 | return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao); | |
197 | } | |
11e09c59 | 198 | |
76ca3345 KJ |
199 | /** |
200 | * This function is used to format the soft credit for backward compatibility | |
201 | * as of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key | |
202 | * but we still return first soft credit as a part of contribution array | |
203 | */ | |
204 | function _civicrm_api3_format_soft_credit(&$contribution) { | |
205 | if (!empty($contribution['soft_credit'])) { | |
206 | $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id']; | |
207 | $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id']; | |
208 | } | |
209 | } | |
210 | ||
11e09c59 | 211 | /** |
6a488035 TO |
212 | * Adjust Metadata for Get action |
213 | * | |
214 | * The metadata is used for setting defaults, documentation & validation | |
215 | * @param array $params array or parameters determined by getfields | |
216 | */ | |
217 | function _civicrm_api3_contribution_get_spec(&$params) { | |
218 | $params['contribution_test']['api.default'] = 0; | |
219 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); | |
220 | $params['contact_id'] = $params['contribution_contact_id']; | |
221 | $params['contact_id']['api.aliases'] = array('contribution_contact_id'); | |
222 | unset($params['contribution_contact_id']); | |
223 | } | |
224 | ||
225 | /** | |
226 | * take the input parameter list as specified in the data model and | |
227 | * convert it into the same format that we use in QF and BAO object | |
228 | * | |
69dbd1ba | 229 | * @param array $params Associative array of property name/value |
6a488035 | 230 | * pairs to insert in new contact. |
69dbd1ba | 231 | * @param array $values The reformatted properties that we can use internally |
6a488035 TO |
232 | * ' |
233 | * | |
69dbd1ba EM |
234 | * @param bool $create |
235 | * | |
6a488035 TO |
236 | * @return array|CRM_Error |
237 | * @access public | |
238 | */ | |
239 | function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) { | |
240 | //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer | |
6a488035 | 241 | _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values); |
6a488035 TO |
242 | return array(); |
243 | } | |
244 | ||
16c0ec8d CW |
245 | /** |
246 | * Adjust Metadata for Transact action | |
247 | * | |
248 | * The metadata is used for setting defaults, documentation & validation | |
249 | * @param array $params array or parameters determined by getfields | |
250 | */ | |
251 | function _civicrm_api3_contribution_transact_spec(&$params) { | |
252 | // This function calls create, so should inherit create spec | |
253 | _civicrm_api3_contribution_create_spec($params); | |
254 | $params['receive_date']['api.default'] = 'now'; | |
255 | } | |
256 | ||
6a488035 TO |
257 | /** |
258 | * Process a transaction and record it against the contact. | |
259 | * | |
260 | * @param array $params (reference ) input parameters | |
261 | * | |
262 | * @return array (reference ) contribution of created or updated record (or a civicrm error) | |
263 | * @static void | |
264 | * @access public | |
265 | * | |
266 | */ | |
267 | function civicrm_api3_contribution_transact($params) { | |
16c0ec8d CW |
268 | // Set some params specific to payment processing |
269 | $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; | |
270 | $params['amount'] = $params['total_amount']; | |
6a488035 TO |
271 | if (!isset($params['net_amount'])) { |
272 | $params['net_amount'] = $params['amount']; | |
273 | } | |
6a488035 TO |
274 | if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { |
275 | $params['invoiceID'] = $params['invoice_id']; | |
276 | } | |
277 | ||
16c0ec8d | 278 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); |
6a488035 TO |
279 | if (civicrm_error($paymentProcessor)) { |
280 | return $paymentProcessor; | |
281 | } | |
282 | ||
16c0ec8d | 283 | $payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); |
6a488035 TO |
284 | if (civicrm_error($payment)) { |
285 | return $payment; | |
286 | } | |
287 | ||
288 | $transaction = $payment->doDirectPayment($params); | |
289 | if (civicrm_error($transaction)) { | |
290 | return $transaction; | |
291 | } | |
292 | ||
293 | // but actually, $payment->doDirectPayment() doesn't return a | |
294 | // CRM_Core_Error by itself | |
16c0ec8d | 295 | if (is_object($transaction) && get_class($transaction) == 'CRM_Core_Error') { |
6a488035 TO |
296 | $errs = $transaction->getErrors(); |
297 | if (!empty($errs)) { | |
298 | $last_error = array_shift($errs); | |
299 | return CRM_Core_Error::createApiError($last_error['message']); | |
300 | } | |
301 | } | |
302 | ||
16c0ec8d | 303 | return civicrm_api('contribution', 'create', $params); |
6a488035 | 304 | } |
69dbd1ba | 305 | |
6a488035 TO |
306 | /** |
307 | * Send a contribution confirmation (receipt or invoice) | |
308 | * The appropriate online template will be used (the existence of related objects | |
309 | * (e.g. memberships ) will affect this selection | |
69dbd1ba | 310 | * |
6a488035 TO |
311 | * @param array $params input parameters |
312 | * {@getfields Contribution_sendconfirmation} | |
69dbd1ba EM |
313 | * |
314 | * @throws Exception | |
6a488035 TO |
315 | * @return array Api result array |
316 | * @static void | |
317 | * @access public | |
6a488035 TO |
318 | */ |
319 | function civicrm_api3_contribution_sendconfirmation($params) { | |
320 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
321 | $contribution->id = $params['id']; | |
76ca3345 | 322 | if (! $contribution->find(TRUE)) { |
6a488035 TO |
323 | throw new Exception('Contribution does not exist'); |
324 | } | |
325 | $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']); | |
76ca3345 KJ |
326 | $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE); |
327 | $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE); | |
6a488035 TO |
328 | } |
329 | ||
11e09c59 | 330 | /** |
6a0cf2c6 | 331 | * Adjust Metadata for sendconfirmation action |
6a488035 TO |
332 | * |
333 | * The metadata is used for setting defaults, documentation & validation | |
334 | * @param array $params array or parameters determined by getfields | |
335 | */ | |
336 | function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { | |
337 | $params['id'] = array( | |
338 | 'api.required' => 1, | |
339 | 'title' => 'Contribution ID' | |
340 | ); | |
341 | $params['receipt_from_email'] = array( | |
342 | 'api.required' =>1, | |
6a0cf2c6 CW |
343 | 'title' => 'From Email address (string) required until someone provides a patch :-)', |
344 | ); | |
345 | $params['receipt_from_name'] = array( | |
346 | 'title' => 'From Name (string)', | |
347 | ); | |
348 | $params['cc_receipt'] = array( | |
349 | 'title' => 'CC Email address (string)', | |
350 | ); | |
351 | $params['bcc_receipt'] = array( | |
352 | 'title' => 'BCC Email address (string)', | |
353 | ); | |
354 | $params['receipt_text'] = array( | |
355 | 'title' => 'Message (string)', | |
6a488035 TO |
356 | ); |
357 | } | |
0efa8efe | 358 | |
359 | /** | |
360 | * Complete an existing (pending) transaction, updating related entities (participant, membership, pledge etc) | |
361 | * and taking any complete actions from the contribution page (e.g. send receipt) | |
362 | * | |
363 | * @todo - most of this should live in the BAO layer but as we want it to be an addition | |
364 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later | |
365 | * | |
366 | * @param array $params input parameters | |
367 | * {@getfields Contribution_completetransaction} | |
69dbd1ba EM |
368 | * |
369 | * @throws API_Exception | |
0efa8efe | 370 | * @return array Api result array |
371 | * @static void | |
372 | * @access public | |
0efa8efe | 373 | */ |
374 | function civicrm_api3_contribution_completetransaction(&$params) { | |
375 | ||
376 | $input = $ids = array(); | |
377 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
378 | $contribution->id = $params['id']; | |
379 | $contribution->find(TRUE); | |
380 | if(!$contribution->id == $params['id']){ | |
381 | throw new API_Exception('A valid contribution ID is required', 'invalid_data'); | |
382 | } | |
383 | try { | |
384 | if(!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)){ | |
385 | throw new API_Exception('failed to load related objects'); | |
386 | } | |
387 | $objects = $contribution->_relatedObjects; | |
388 | $objects['contribution'] = &$contribution; | |
389 | $input['component'] = $contribution->_component; | |
390 | $input['is_test'] = $contribution->is_test; | |
eac25df2 | 391 | $input['trxn_id']= !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id; |
0efa8efe | 392 | $input['amount'] = $contribution->total_amount; |
393 | if(isset($params['is_email_receipt'])){ | |
394 | $input['is_email_receipt'] = $params['is_email_receipt']; | |
395 | } | |
396 | // @todo required for base ipn but problematic as api layer handles this | |
397 | $transaction = new CRM_Core_Transaction(); | |
398 | $ipn = new CRM_Core_Payment_BaseIPN(); | |
399 | $ipn->completeTransaction($input, $ids, $objects, $transaction); | |
400 | } | |
0e37ad72 | 401 | catch(Exception $e) { |
0efa8efe | 402 | throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString()); |
403 | } | |
404 | } | |
405 | ||
aa1b1481 EM |
406 | /** |
407 | * @param $params | |
408 | */ | |
0efa8efe | 409 | function _civicrm_api3_contribution_completetransaction(&$params) { |
eac25df2 EM |
410 | $params['id'] = array( |
411 | 'title' => 'Contribution ID', | |
412 | 'type' => CRM_Utils_Type::T_INT, | |
413 | 'api.required' => TRUE, | |
414 | ); | |
415 | $params['trxn_id'] = array( | |
416 | 'title' => 'Transaction ID', | |
417 | 'type' => CRM_Utils_Type::T_STRING, | |
418 | ); | |
419 | $params['is_email_receipt'] = array( | |
420 | 'title' => 'Send email Receipt?', | |
421 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
422 | ); | |
0efa8efe | 423 | } |