Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | * | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
36 | * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $ | |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Add or update a contribution | |
42 | * | |
43 | * @param array $params (reference ) input parameters | |
44 | * | |
45 | * @return array Api result array | |
46 | * @static void | |
47 | * @access public | |
48 | * @example ContributionCreate.php | |
49 | * {@getfields Contribution_create} | |
50 | */ | |
51 | function civicrm_api3_contribution_create(&$params) { | |
52 | $values = array(); | |
53 | ||
54 | _civicrm_api3_contribute_format_params($params, $values); | |
55 | ||
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); | |
502b3d42 | 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'])); | |
64 | } | |
6a488035 TO |
65 | $ids = array(); |
66 | if (CRM_Utils_Array::value('id', $params)) { | |
67 | $ids['contribution'] = $params['id']; | |
c71ae314 PN |
68 | // CRM-12498 |
69 | if (CRM_Utils_Array::value('contribution_status_id', $params)) { | |
8ef12e64 | 70 | $error = array(); |
c71ae314 PN |
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']); | |
75 | } | |
76 | } | |
6a488035 | 77 | } |
6a488035 TO |
78 | $contribution = CRM_Contribute_BAO_Contribution::create($values, $ids); |
79 | ||
80 | if (is_a($contribution, 'CRM_Core_Error')) { | |
81 | return civicrm_api3_create_error($contribution->_errors[0]['message']); | |
82 | } | |
83 | _civicrm_api3_object_to_array($contribution, $contributeArray[$contribution->id]); | |
84 | ||
85 | return civicrm_api3_create_success($contributeArray, $params, 'contribution', 'create', $contribution); | |
86 | } | |
11e09c59 TO |
87 | |
88 | /** | |
6a488035 TO |
89 | * Adjust Metadata for Create action |
90 | * | |
91 | * The metadata is used for setting defaults, documentation & validation | |
92 | * @param array $params array or parameters determined by getfields | |
93 | */ | |
94 | function _civicrm_api3_contribution_create_spec(&$params) { | |
95 | $params['contact_id']['api.required'] = 1; | |
96 | $params['total_amount']['api.required'] = 1; | |
53191813 | 97 | $params['payment_instrument_id']['api.aliases'] = array('payment_instrument'); |
6a488035 TO |
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'), | |
105 | ); | |
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( | |
109 | 'name' => 'note', | |
110 | 'uniqueName' => 'contribution_note', | |
111 | 'title' => 'note', | |
112 | 'type' => 2, | |
113 | 'description' => 'Associated Note in the notes table', | |
114 | ); | |
115 | $params['soft_credit_to'] = array( | |
116 | 'name' => 'soft_credit_to', | |
117 | 'title' => 'Soft Credit contact ID', | |
118 | 'type' => 1, | |
119 | 'description' => 'ID of Contact to be Soft credited to', | |
120 | 'FKClassName' => 'CRM_Contact_DAO_Contact', | |
121 | ); | |
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', | |
0900b21e | 127 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
6a488035 TO |
128 | 'description' => 'Do not add to recent view (setting this improves performance)', |
129 | ); | |
130 | $params['skipLineItem'] = array( | |
131 | 'name' => 'skipLineItem', | |
132 | 'title' => 'Skip adding line items', | |
133 | 'type' => 1, | |
134 | 'api.default' => 0, | |
135 | 'description' => 'Do not add line items by default (if you wish to add your own)', | |
136 | ); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Delete a contribution | |
141 | * | |
142 | * @param array $params (reference ) input parameters | |
143 | * | |
144 | * @return boolean true if success, else false | |
145 | * @static void | |
146 | * @access public | |
147 | * {@getfields Contribution_delete} | |
148 | * @example ContributionDelete.php | |
149 | */ | |
150 | function civicrm_api3_contribution_delete($params) { | |
151 | ||
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)); | |
155 | } | |
156 | else { | |
157 | return civicrm_api3_create_error('Could not delete contribution'); | |
158 | } | |
159 | } | |
11e09c59 TO |
160 | |
161 | /** | |
6a488035 TO |
162 | * modify metadata. Legacy support for contribution_id |
163 | */ | |
164 | function _civicrm_api3_contribution_delete_spec(&$params) { | |
165 | $params['id']['api.aliases'] = array('contribution_id'); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Retrieve a set of contributions, given a set of input params | |
170 | * | |
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. | |
175 | * | |
176 | * @return array (reference ) array of contributions, if error an array with an error id and error message | |
177 | * @static void | |
178 | * @access public | |
179 | * {@getfields Contribution_get} | |
180 | * @example ContributionGet.php | |
181 | */ | |
182 | function civicrm_api3_contribution_get($params) { | |
183 | ||
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); | |
6a488035 TO |
191 | if (empty($returnProperties)) { |
192 | $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE); | |
193 | } | |
194 | ||
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 | |
198 | ); | |
199 | list($select, $from, $where, $having) = $query->query(); | |
200 | ||
201 | $sql = "$select $from $where $having"; | |
202 | ||
203 | if (!empty($sort)) { | |
204 | $sql .= " ORDER BY $sort "; | |
205 | } | |
206 | $sql .= " LIMIT $offset, $rowCount "; | |
207 | $dao = CRM_Core_DAO::executeQuery($sql); | |
208 | ||
209 | $contribution = array(); | |
210 | while ($dao->fetch()) { | |
211 | //CRM-8662 | |
76ca3345 KJ |
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); | |
a1c68fd2 | 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']; | |
217 | } | |
76ca3345 KJ |
218 | // format soft credit for backward compatibility |
219 | _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]); | |
6a488035 TO |
220 | } |
221 | return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao); | |
222 | } | |
11e09c59 | 223 | |
76ca3345 KJ |
224 | /** |
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 | |
228 | */ | |
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']; | |
233 | } | |
234 | } | |
235 | ||
11e09c59 | 236 | /** |
6a488035 TO |
237 | * Adjust Metadata for Get action |
238 | * | |
239 | * The metadata is used for setting defaults, documentation & validation | |
240 | * @param array $params array or parameters determined by getfields | |
241 | */ | |
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']); | |
248 | } | |
249 | ||
250 | /** | |
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 | |
253 | * | |
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 | |
257 | * ' | |
258 | * | |
259 | * @return array|CRM_Error | |
260 | * @access public | |
261 | */ | |
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 | |
6a488035 | 264 | _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values); |
6a488035 TO |
265 | return array(); |
266 | } | |
267 | ||
268 | /** | |
269 | * Process a transaction and record it against the contact. | |
270 | * | |
271 | * @param array $params (reference ) input parameters | |
272 | * | |
273 | * @return array (reference ) contribution of created or updated record (or a civicrm error) | |
274 | * @static void | |
275 | * @access public | |
276 | * | |
277 | */ | |
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'."); | |
283 | } | |
284 | } | |
285 | ||
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 */ | |
288 | $defaults = array( | |
289 | 'payment_processor_mode' => 'live', | |
290 | ); | |
291 | $params = array_merge($defaults, $params); | |
292 | ||
293 | // clean up / adjust some values which | |
294 | if (!isset($params['total_amount'])) { | |
295 | $params['total_amount'] = $params['amount']; | |
296 | } | |
297 | if (!isset($params['net_amount'])) { | |
298 | $params['net_amount'] = $params['amount']; | |
299 | } | |
300 | if (!isset($params['receive_date'])) { | |
301 | $params['receive_date'] = date('Y-m-d'); | |
302 | } | |
303 | if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { | |
304 | $params['invoiceID'] = $params['invoice_id']; | |
305 | } | |
306 | ||
6a488035 TO |
307 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor_id'], $params['payment_processor_mode']); |
308 | if (civicrm_error($paymentProcessor)) { | |
309 | return $paymentProcessor; | |
310 | } | |
311 | ||
6a488035 TO |
312 | $payment = &CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); |
313 | if (civicrm_error($payment)) { | |
314 | return $payment; | |
315 | } | |
316 | ||
317 | $transaction = $payment->doDirectPayment($params); | |
318 | if (civicrm_error($transaction)) { | |
319 | return $transaction; | |
320 | } | |
321 | ||
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(); | |
326 | if (!empty($errs)) { | |
327 | $last_error = array_shift($errs); | |
328 | return CRM_Core_Error::createApiError($last_error['message']); | |
329 | } | |
330 | } | |
331 | ||
332 | $contribution = civicrm_api('contribution', 'create', $params); | |
333 | return $contribution['values']; | |
334 | } | |
335 | /** | |
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 | |
342 | * @static void | |
343 | * @access public | |
344 | * | |
345 | */ | |
346 | function civicrm_api3_contribution_sendconfirmation($params) { | |
347 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
348 | $contribution->id = $params['id']; | |
76ca3345 | 349 | if (! $contribution->find(TRUE)) { |
6a488035 TO |
350 | throw new Exception('Contribution does not exist'); |
351 | } | |
352 | $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']); | |
76ca3345 KJ |
353 | $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE); |
354 | $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE); | |
6a488035 TO |
355 | } |
356 | ||
11e09c59 | 357 | /** |
6a488035 TO |
358 | * Adjust Metadata for Create action |
359 | * | |
360 | * The metadata is used for setting defaults, documentation & validation | |
361 | * @param array $params array or parameters determined by getfields | |
362 | */ | |
363 | function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { | |
364 | $params['id'] = array( | |
365 | 'api.required' => 1, | |
366 | 'title' => 'Contribution ID' | |
367 | ); | |
368 | $params['receipt_from_email'] = array( | |
369 | 'api.required' =>1, | |
370 | 'title' => 'From Email (required until someone provides a patch :-)', | |
371 | ||
372 | ); | |
373 | } | |
0efa8efe | 374 | |
375 | /** | |
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) | |
378 | * | |
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 | |
381 | * | |
382 | * @param array $params input parameters | |
383 | * {@getfields Contribution_completetransaction} | |
384 | * @return array Api result array | |
385 | * @static void | |
386 | * @access public | |
387 | * | |
388 | */ | |
389 | function civicrm_api3_contribution_completetransaction(&$params) { | |
390 | ||
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'); | |
397 | } | |
398 | try { | |
399 | if(!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)){ | |
400 | throw new API_Exception('failed to load related objects'); | |
401 | } | |
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']; | |
410 | } | |
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); | |
415 | } | |
416 | catch(Exception$e) { | |
417 | throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString()); | |
418 | } | |
419 | } | |
420 | ||
421 | function _civicrm_api3_contribution_completetransaction(&$params) { | |
422 | ||
423 | } |