Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | /** | |
244bbdd8 | 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 | */ |
44 | function 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); |
83644f47 | 48 | // The BAO should not clean money - it should be done in the form layer & api wrapper |
49 | // (although arguably the api should expect pre-cleaned it seems to do some cleaning.) | |
4750ab01 | 50 | if (empty($params['skipCleanMoney'])) { |
51 | foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) { | |
52 | if (isset($params[$field])) { | |
53 | $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); | |
54 | } | |
55 | } | |
56 | } | |
83644f47 | 57 | $params['skipCleanMoney'] = TRUE; |
504a78f6 | 58 | |
ae2ade98 | 59 | if (!empty($params['check_permissions']) && CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { |
3300bf67 | 60 | if (empty($params['id'])) { |
8715ff99 | 61 | $op = CRM_Core_Action::ADD; |
3300bf67 PN |
62 | } |
63 | else { | |
64 | if (empty($params['financial_type_id'])) { | |
f4a331f4 | 65 | $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['id'], 'financial_type_id'); |
3300bf67 | 66 | } |
8715ff99 | 67 | $op = CRM_Core_Action::UPDATE; |
3300bf67 PN |
68 | } |
69 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types, $op); | |
70 | if (!in_array($params['financial_type_id'], array_keys($types))) { | |
ae2ade98 | 71 | throw new API_Exception('You do not have permission to create this contribution'); |
3300bf67 | 72 | } |
816cac04 | 73 | } |
8cc574cf | 74 | if (!empty($params['id']) && !empty($params['contribution_status_id'])) { |
504a78f6 | 75 | $error = array(); |
76 | //throw error for invalid status change such as setting completed back to pending | |
77 | //@todo this sort of validation belongs in the BAO not the API - if it is not an OK | |
78 | // action it needs to be blocked there. If it is Ok through a form it needs to be OK through the api | |
79 | CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error); | |
80 | if (array_key_exists('contribution_status_id', $error)) { | |
81 | throw new API_Exception($error['contribution_status_id']); | |
82 | } | |
6a488035 | 83 | } |
4d47ad17 PN |
84 | if (!empty($params['id']) && !empty($params['financial_type_id'])) { |
85 | $error = array(); | |
86 | CRM_Contribute_BAO_Contribution::checkFinancialTypeChange($params['financial_type_id'], $params['id'], $error); | |
87 | if (array_key_exists('financial_type_id', $error)) { | |
88 | throw new API_Exception($error['financial_type_id']); | |
89 | } | |
90 | } | |
6a516bd6 DG |
91 | _civicrm_api3_contribution_create_legacy_support_45($params); |
92 | ||
504a78f6 | 93 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution'); |
6a488035 | 94 | } |
11e09c59 TO |
95 | |
96 | /** | |
0aa0303c EM |
97 | * Adjust Metadata for Create action. |
98 | * | |
99 | * The metadata is used for setting defaults, documentation & validation. | |
6a488035 | 100 | * |
cf470720 | 101 | * @param array $params |
b081365f | 102 | * Array of parameters determined by getfields. |
6a488035 TO |
103 | */ |
104 | function _civicrm_api3_contribution_create_spec(&$params) { | |
105 | $params['contact_id']['api.required'] = 1; | |
106 | $params['total_amount']['api.required'] = 1; | |
53191813 | 107 | $params['payment_instrument_id']['api.aliases'] = array('payment_instrument'); |
8757a378 | 108 | $params['receive_date']['api.default'] = 'now'; |
2b0fe7ac | 109 | $params['receive_date']['api.required'] = TRUE; |
6a488035 TO |
110 | $params['payment_processor'] = array( |
111 | 'name' => 'payment_processor', | |
112 | 'title' => 'Payment Processor ID', | |
113 | 'description' => 'ID of payment processor used for this contribution', | |
114 | // field is called payment processor - not payment processor id but can only be one id so | |
115 | // it seems likely someone will fix it up one day to be more consistent - lets alias it from the start | |
116 | 'api.aliases' => array('payment_processor_id'), | |
d142432b | 117 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
118 | ); |
119 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); | |
120 | $params['financial_type_id']['api.required'] = 1; | |
121 | $params['note'] = array( | |
122 | 'name' => 'note', | |
123 | 'uniqueName' => 'contribution_note', | |
124 | 'title' => 'note', | |
125 | 'type' => 2, | |
126 | 'description' => 'Associated Note in the notes table', | |
127 | ); | |
128 | $params['soft_credit_to'] = array( | |
129 | 'name' => 'soft_credit_to', | |
33a429d4 | 130 | 'title' => 'Soft Credit contact ID (legacy)', |
797d4c52 | 131 | 'type' => CRM_Utils_Type::T_INT, |
33a429d4 | 132 | 'description' => 'ID of Contact to be Soft credited to (deprecated - use contribution_soft api)', |
6a488035 TO |
133 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
134 | ); | |
6a516bd6 DG |
135 | $params['honor_contact_id'] = array( |
136 | 'name' => 'honor_contact_id', | |
33a429d4 | 137 | 'title' => 'Honoree contact ID (legacy)', |
797d4c52 | 138 | 'type' => CRM_Utils_Type::T_INT, |
33a429d4 | 139 | 'description' => 'ID of honoree contact (deprecated - use contribution_soft api)', |
6a516bd6 DG |
140 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
141 | ); | |
33a429d4 CW |
142 | $params['honor_type_id'] = array( |
143 | 'name' => 'honor_type_id', | |
144 | 'title' => 'Honoree Type (legacy)', | |
797d4c52 | 145 | 'type' => CRM_Utils_Type::T_INT, |
33a429d4 CW |
146 | 'description' => 'Type of honoree contact (deprecated - use contribution_soft api)', |
147 | 'pseudoconstant' => TRUE, | |
148 | ); | |
6a488035 | 149 | // note this is a recommended option but not adding as a default to avoid |
c206647d | 150 | // creating unnecessary changes for the dev |
6a488035 TO |
151 | $params['skipRecentView'] = array( |
152 | 'name' => 'skipRecentView', | |
153 | 'title' => 'Skip adding to recent view', | |
0900b21e | 154 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
6a488035 TO |
155 | 'description' => 'Do not add to recent view (setting this improves performance)', |
156 | ); | |
157 | $params['skipLineItem'] = array( | |
158 | 'name' => 'skipLineItem', | |
159 | 'title' => 'Skip adding line items', | |
b3b7f4c5 | 160 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
6a488035 TO |
161 | 'api.default' => 0, |
162 | 'description' => 'Do not add line items by default (if you wish to add your own)', | |
163 | ); | |
599c61ac E |
164 | $params['batch_id'] = array( |
165 | 'title' => 'Batch', | |
797d4c52 | 166 | 'type' => CRM_Utils_Type::T_INT, |
599c61ac E |
167 | 'description' => 'Batch which relevant transactions should be added to', |
168 | ); | |
797d4c52 | 169 | $params['refund_trxn_id'] = array( |
170 | 'title' => 'Refund Transaction ID', | |
171 | 'type' => CRM_Utils_Type::T_STRING, | |
172 | 'description' => 'Transaction ID specific to the refund taking place', | |
173 | ); | |
a55e39e9 | 174 | $params['card_type_id'] = array( |
175 | 'title' => 'Card Type ID', | |
176 | 'description' => 'Providing Credit Card Type ID', | |
177 | 'type' => CRM_Utils_Type::T_INT, | |
178 | 'pseudoconstant' => array( | |
179 | 'optionGroupName' => 'accept_creditcard', | |
180 | ), | |
181 | ); | |
6a488035 TO |
182 | } |
183 | ||
6a516bd6 | 184 | /** |
9d32e6f7 EM |
185 | * Support for schema changes made in 4.5. |
186 | * | |
35671d00 TO |
187 | * The main purpose of the API is to provide integrators a level of stability not provided by |
188 | * the core code or schema - this means we have to provide support for api calls (where possible) | |
189 | * across schema changes. | |
dc64d047 | 190 | * |
d0997921 | 191 | * @param array $params |
35671d00 | 192 | */ |
9b873358 | 193 | function _civicrm_api3_contribution_create_legacy_support_45(&$params) { |
6a516bd6 | 194 | //legacy soft credit handling - recommended approach is chaining |
9b873358 | 195 | if (!empty($params['soft_credit_to'])) { |
33a429d4 | 196 | $params['soft_credit'][] = array( |
6a516bd6 DG |
197 | 'contact_id' => $params['soft_credit_to'], |
198 | 'amount' => $params['total_amount'], | |
21dfd5f5 | 199 | 'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), |
33a429d4 | 200 | ); |
6a516bd6 | 201 | } |
9b873358 | 202 | if (!empty($params['honor_contact_id'])) { |
33a429d4 | 203 | $params['soft_credit'][] = array( |
6a516bd6 DG |
204 | 'contact_id' => $params['honor_contact_id'], |
205 | 'amount' => $params['total_amount'], | |
a28ce73f | 206 | 'soft_credit_type_id' => CRM_Utils_Array::value('honor_type_id', $params, CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'in_honor_of')), |
33a429d4 | 207 | ); |
6a516bd6 DG |
208 | } |
209 | } | |
210 | ||
6a488035 | 211 | /** |
244bbdd8 | 212 | * Delete a Contribution. |
6a488035 | 213 | * |
cf470720 | 214 | * @param array $params |
9d32e6f7 | 215 | * Input parameters. |
6a488035 | 216 | * |
9d32e6f7 | 217 | * @return array |
ae2ade98 | 218 | * @throws \API_Exception |
6a488035 TO |
219 | */ |
220 | function civicrm_api3_contribution_delete($params) { | |
221 | ||
0d8afee2 | 222 | $contributionID = !empty($params['contribution_id']) ? $params['contribution_id'] : $params['id']; |
c93fecd1 E |
223 | // First check contribution financial type |
224 | $financialType = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionID, 'financial_type_id'); | |
c93fecd1 | 225 | // Now check permissioned lineitems & permissioned contribution |
ae2ade98 | 226 | if (!empty($params['check_permissions']) && CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && |
227 | ( | |
228 | !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($financialType)) | |
229 | || !CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributionID, 'delete', FALSE) | |
230 | ) | |
66af7c48 | 231 | ) { |
ae2ade98 | 232 | throw new API_Exception('You do not have permission to delete this contribution'); |
c93fecd1 | 233 | } |
6a488035 TO |
234 | if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) { |
235 | return civicrm_api3_create_success(array($contributionID => 1)); | |
236 | } | |
237 | else { | |
8744e9ce | 238 | throw new API_Exception('Could not delete contribution'); |
6a488035 TO |
239 | } |
240 | } | |
11e09c59 TO |
241 | |
242 | /** | |
9d32e6f7 EM |
243 | * Modify metadata for delete action. |
244 | * | |
245 | * Legacy support for contribution_id. | |
246 | * | |
d0997921 | 247 | * @param array $params |
6a488035 TO |
248 | */ |
249 | function _civicrm_api3_contribution_delete_spec(&$params) { | |
250 | $params['id']['api.aliases'] = array('contribution_id'); | |
251 | } | |
252 | ||
253 | /** | |
35823763 | 254 | * Retrieve a set of contributions. |
6a488035 | 255 | * |
cf470720 | 256 | * @param array $params |
35823763 | 257 | * Input parameters. |
69dbd1ba | 258 | * |
a6c01b45 | 259 | * @return array |
16b10e64 | 260 | * Array of contributions, if error an array with an error id and error message |
6a488035 TO |
261 | */ |
262 | function civicrm_api3_contribution_get($params) { | |
263 | ||
82f7d8b2 | 264 | $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
3123273f | 265 | $additionalOptions = _civicrm_api3_contribution_get_support_nonunique_returns($params); |
be81f101 | 266 | $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode); |
6a488035 | 267 | |
3123273f | 268 | $contributions = _civicrm_api3_get_using_query_object('Contribution', $params, $additionalOptions, NULL, $mode, $returnProperties); |
be81f101 | 269 | |
270 | foreach ($contributions as $id => $contribution) { | |
271 | $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id, TRUE); | |
272 | $contributions[$id] = array_merge($contribution, $softContribution); | |
76ca3345 | 273 | // format soft credit for backward compatibility |
be81f101 | 274 | _civicrm_api3_format_soft_credit($contributions[$id]); |
3123273f | 275 | _civicrm_api3_contribution_add_supported_fields($contributions[$id]); |
276 | ||
6a488035 | 277 | } |
be81f101 | 278 | return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get'); |
9ae25b56 | 279 | } |
280 | ||
3123273f | 281 | /** |
282 | * Fix the return values to reflect cases where the schema has been changed. | |
283 | * | |
284 | * At the query object level using uniquenames dismbiguates between tables. | |
285 | * | |
286 | * However, adding uniquename can change inputs accepted by the api, so we need | |
287 | * to ensure we are asking for the unique name return fields. | |
288 | * | |
289 | * @param array $params | |
290 | * | |
291 | * @return array | |
292 | * @throws \API_Exception | |
293 | */ | |
294 | function _civicrm_api3_contribution_get_support_nonunique_returns($params) { | |
295 | $additionalOptions = array(); | |
296 | $options = _civicrm_api3_get_options_from_params($params, TRUE); | |
297 | foreach (array('check_number', 'address_id') as $changedVariable) { | |
298 | if (isset($options['return']) && !empty($options['return'][$changedVariable])) { | |
299 | $additionalOptions['return']['contribution_' . $changedVariable] = 1; | |
300 | } | |
301 | } | |
302 | return $additionalOptions; | |
303 | } | |
304 | ||
305 | /** | |
306 | * Support for supported output variables. | |
307 | * | |
308 | * @param $contribution | |
309 | */ | |
310 | function _civicrm_api3_contribution_add_supported_fields(&$contribution) { | |
311 | // These are output fields that are supported in our test contract. | |
312 | // Arguably we should also do the same with 'campaign_id' & | |
313 | // 'source' - which are also fields being rendered with unique names. | |
314 | // That seems more consistent with other api where we output the actual field names. | |
315 | $outputAliases = array( | |
316 | 'contribution_check_number' => 'check_number', | |
317 | 'contribution_address_id' => 'address_id', | |
318 | 'payment_instrument_id' => 'instrument_id', | |
319 | ); | |
320 | foreach ($outputAliases as $returnName => $copyTo) { | |
321 | if (array_key_exists($returnName, $contribution)) { | |
322 | $contribution[$copyTo] = $contribution[$returnName]; | |
323 | } | |
324 | } | |
325 | ||
326 | } | |
327 | ||
9ae25b56 | 328 | /** |
329 | * Get number of contacts matching the supplied criteria. | |
330 | * | |
331 | * @param array $params | |
332 | * | |
333 | * @return int | |
334 | */ | |
335 | function civicrm_api3_contribution_getcount($params) { | |
336 | $count = _civicrm_api3_get_using_query_object('Contribution', $params, array(), TRUE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE); | |
337 | return (int) $count; | |
6a488035 | 338 | } |
11e09c59 | 339 | |
76ca3345 | 340 | /** |
1747ab99 EM |
341 | * This function is used to format the soft credit for backward compatibility. |
342 | * | |
343 | * As of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key | |
76ca3345 | 344 | * but we still return first soft credit as a part of contribution array |
dc64d047 | 345 | * |
645ee340 | 346 | * @param $contribution |
76ca3345 KJ |
347 | */ |
348 | function _civicrm_api3_format_soft_credit(&$contribution) { | |
349 | if (!empty($contribution['soft_credit'])) { | |
350 | $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id']; | |
351 | $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id']; | |
352 | } | |
353 | } | |
354 | ||
11e09c59 | 355 | /** |
1747ab99 | 356 | * Adjust Metadata for Get 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 | */ |
363 | function _civicrm_api3_contribution_get_spec(&$params) { | |
d142432b EM |
364 | $params['contribution_test'] = array( |
365 | 'api.default' => 0, | |
366 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
367 | 'title' => 'Get Test Contributions?', | |
71d5a412 | 368 | 'api.aliases' => array('is_test'), |
d142432b | 369 | ); |
739a8336 | 370 | |
6a488035 | 371 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); |
e6b9e2ae | 372 | $params['payment_instrument_id']['api.aliases'] = array('contribution_payment_instrument', 'payment_instrument'); |
03df80cf | 373 | $params['contact_id'] = CRM_Utils_Array::value('contribution_contact_id', $params); |
6a488035 TO |
374 | $params['contact_id']['api.aliases'] = array('contribution_contact_id'); |
375 | unset($params['contribution_contact_id']); | |
376 | } | |
377 | ||
378 | /** | |
1747ab99 EM |
379 | * Legacy handling for contribution parameters. |
380 | * | |
381 | * Take the input parameter list as specified in the data model and | |
382 | * convert it into the same format that we use in QF and BAO object. | |
6a488035 | 383 | * |
cf470720 | 384 | * @param array $params |
c23f45d3 | 385 | * property name/value pairs to insert in new contact. |
cf470720 TO |
386 | * @param array $values |
387 | * The reformatted properties that we can use internally. | |
6a488035 | 388 | * |
c23f45d3 | 389 | * @return array |
6a488035 | 390 | */ |
c23f45d3 | 391 | function _civicrm_api3_contribute_format_params($params, &$values) { |
35671d00 | 392 | //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer |
6a488035 | 393 | _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values); |
6a488035 TO |
394 | return array(); |
395 | } | |
396 | ||
16c0ec8d | 397 | /** |
1747ab99 | 398 | * Adjust Metadata for Transact action. |
16c0ec8d | 399 | * |
0aa0303c EM |
400 | * The metadata is used for setting defaults, documentation & validation. |
401 | * | |
cf470720 | 402 | * @param array $params |
b081365f | 403 | * Array of parameters determined by getfields. |
16c0ec8d CW |
404 | */ |
405 | function _civicrm_api3_contribution_transact_spec(&$params) { | |
244bbdd8 | 406 | $fields = civicrm_api3('Contribution', 'getfields', array('action' => 'create')); |
4c41ecb2 | 407 | $params = array_merge($params, $fields['values']); |
16c0ec8d CW |
408 | $params['receive_date']['api.default'] = 'now'; |
409 | } | |
410 | ||
6a488035 TO |
411 | /** |
412 | * Process a transaction and record it against the contact. | |
413 | * | |
cf470720 | 414 | * @param array $params |
1747ab99 | 415 | * Input parameters. |
6a488035 | 416 | * |
a6c01b45 | 417 | * @return array |
c23f45d3 | 418 | * contribution of created or updated record (or a civicrm error) |
6a488035 TO |
419 | */ |
420 | function civicrm_api3_contribution_transact($params) { | |
16c0ec8d | 421 | // Set some params specific to payment processing |
8319cf11 EM |
422 | // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with |
423 | // 'is_error' set | |
424 | // also trxn_id is not saved. | |
425 | // but since there is no test it's not desirable to jump in & make the obvious changes. | |
16c0ec8d CW |
426 | $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; |
427 | $params['amount'] = $params['total_amount']; | |
6a488035 TO |
428 | if (!isset($params['net_amount'])) { |
429 | $params['net_amount'] = $params['amount']; | |
430 | } | |
6a488035 TO |
431 | if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { |
432 | $params['invoiceID'] = $params['invoice_id']; | |
433 | } | |
434 | ||
be06e507 CW |
435 | // Some payment processors expect a unique invoice_id - generate one if not supplied |
436 | $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE))); | |
437 | ||
16c0ec8d | 438 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); |
9d8f43b1 | 439 | $paymentProcessor['object']->doPayment($params); |
6a488035 | 440 | |
5a9f9519 O |
441 | $params['payment_instrument_id'] = $paymentProcessor['object']->getPaymentInstrumentID(); |
442 | ||
244bbdd8 | 443 | return civicrm_api('Contribution', 'create', $params); |
6a488035 | 444 | } |
69dbd1ba | 445 | |
6a488035 | 446 | /** |
1747ab99 EM |
447 | * Send a contribution confirmation (receipt or invoice). |
448 | * | |
6a488035 TO |
449 | * The appropriate online template will be used (the existence of related objects |
450 | * (e.g. memberships ) will affect this selection | |
69dbd1ba | 451 | * |
cf470720 TO |
452 | * @param array $params |
453 | * Input parameters. | |
69dbd1ba EM |
454 | * |
455 | * @throws Exception | |
6a488035 TO |
456 | */ |
457 | function civicrm_api3_contribution_sendconfirmation($params) { | |
71687b3d CW |
458 | $ids = $values = array(); |
459 | $allowedParams = array( | |
ec7e3954 E |
460 | 'receipt_from_email', |
461 | 'receipt_from_name', | |
462 | 'receipt_update', | |
463 | 'cc_receipt', | |
464 | 'bcc_receipt', | |
465 | 'receipt_text', | |
466 | 'payment_processor_id', | |
467 | ); | |
71687b3d | 468 | $input = array_intersect_key($params, array_flip($allowedParams)); |
55df1211 | 469 | $input['is_email_receipt'] = TRUE; |
ec7e3954 | 470 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $params['id'], $values); |
6a488035 TO |
471 | } |
472 | ||
11e09c59 | 473 | /** |
1747ab99 | 474 | * Adjust Metadata for sendconfirmation action. |
6a488035 | 475 | * |
0aa0303c EM |
476 | * The metadata is used for setting defaults, documentation & validation. |
477 | * | |
cf470720 | 478 | * @param array $params |
b081365f | 479 | * Array of parameters determined by getfields. |
6a488035 TO |
480 | */ |
481 | function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { | |
482 | $params['id'] = array( | |
483 | 'api.required' => 1, | |
ec7e3954 | 484 | 'title' => ts('Contribution ID'), |
d142432b | 485 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
486 | ); |
487 | $params['receipt_from_email'] = array( | |
ec7e3954 | 488 | 'title' => ts('From Email address (string)'), |
d142432b | 489 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
490 | ); |
491 | $params['receipt_from_name'] = array( | |
ec7e3954 | 492 | 'title' => ts('From Name (string)'), |
d142432b | 493 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
494 | ); |
495 | $params['cc_receipt'] = array( | |
ec7e3954 | 496 | 'title' => ts('CC Email address (string)'), |
d142432b | 497 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
498 | ); |
499 | $params['bcc_receipt'] = array( | |
ec7e3954 | 500 | 'title' => ts('BCC Email address (string)'), |
d142432b | 501 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
502 | ); |
503 | $params['receipt_text'] = array( | |
ec7e3954 | 504 | 'title' => ts('Message (string)'), |
d142432b | 505 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 506 | ); |
ec7e3954 E |
507 | $params['receipt_update'] = array( |
508 | 'title' => ts('Update the Receipt Date'), | |
509 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
510 | 'api.default' => TRUE, | |
511 | ); | |
512 | $params['payment_processor_id'] = array( | |
513 | 'title' => ts('Payment processor Id (avoids mis-guesses)'), | |
514 | 'type' => CRM_Utils_Type::T_INT, | |
515 | ); | |
6a488035 | 516 | } |
0efa8efe | 517 | |
518 | /** | |
1747ab99 EM |
519 | * Complete an existing (pending) transaction. |
520 | * | |
521 | * This will update related entities (participant, membership, pledge etc) | |
522 | * and take any complete actions from the contribution page (e.g. send receipt). | |
0efa8efe | 523 | * |
524 | * @todo - most of this should live in the BAO layer but as we want it to be an addition | |
525 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later | |
526 | * | |
cf470720 TO |
527 | * @param array $params |
528 | * Input parameters. | |
69dbd1ba | 529 | * |
734d2daa | 530 | * @return array |
531 | * API result array | |
532 | * @throws \API_Exception | |
533 | * @throws \CRM_Core_Exception | |
534 | * @throws \Exception | |
0efa8efe | 535 | */ |
536 | function civicrm_api3_contribution_completetransaction(&$params) { | |
0efa8efe | 537 | $input = $ids = array(); |
07b46478 | 538 | if (isset($params['payment_processor_id'])) { |
539 | $input['payment_processor_id'] = $params['payment_processor_id']; | |
540 | } | |
0efa8efe | 541 | $contribution = new CRM_Contribute_BAO_Contribution(); |
542 | $contribution->id = $params['id']; | |
4e379895 | 543 | if (!$contribution->find(TRUE)) { |
0efa8efe | 544 | throw new API_Exception('A valid contribution ID is required', 'invalid_data'); |
545 | } | |
5ddd6889 | 546 | |
276e3ec6 | 547 | if (!$contribution->loadRelatedObjects($input, $ids, TRUE)) { |
5ddd6889 | 548 | throw new API_Exception('failed to load related objects'); |
0efa8efe | 549 | } |
f527e012 | 550 | elseif ($contribution->contribution_status_id == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) { |
5ddd6889 | 551 | throw new API_Exception(ts('Contribution already completed'), 'contribution_completed'); |
0efa8efe | 552 | } |
5ddd6889 | 553 | $input['trxn_id'] = !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id; |
080a561b | 554 | if (!empty($params['fee_amount'])) { |
555 | $input['fee_amount'] = $params['fee_amount']; | |
556 | } | |
734d2daa | 557 | return _ipn_process_transaction($params, $contribution, $input, $ids); |
5ddd6889 | 558 | |
0efa8efe | 559 | } |
560 | ||
aa1b1481 | 561 | /** |
9d32e6f7 EM |
562 | * Provide function metadata. |
563 | * | |
c490a46a | 564 | * @param array $params |
aa1b1481 | 565 | */ |
3dfbdece | 566 | function _civicrm_api3_contribution_completetransaction_spec(&$params) { |
eac25df2 EM |
567 | $params['id'] = array( |
568 | 'title' => 'Contribution ID', | |
569 | 'type' => CRM_Utils_Type::T_INT, | |
570 | 'api.required' => TRUE, | |
571 | ); | |
572 | $params['trxn_id'] = array( | |
573 | 'title' => 'Transaction ID', | |
574 | 'type' => CRM_Utils_Type::T_STRING, | |
575 | ); | |
576 | $params['is_email_receipt'] = array( | |
577 | 'title' => 'Send email Receipt?', | |
578 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
579 | ); | |
0930231a EM |
580 | $params['receipt_from_email'] = array( |
581 | 'title' => 'Email to send receipt from.', | |
582 | 'description' => 'If not provided this will default to being based on domain mail or contribution page', | |
583 | 'type' => CRM_Utils_Type::T_EMAIL, | |
584 | ); | |
585 | $params['receipt_from_name'] = array( | |
586 | 'title' => 'Name to send receipt from', | |
587 | 'description' => '. If not provided this will default to domain mail or contribution page', | |
588 | 'type' => CRM_Utils_Type::T_STRING, | |
589 | ); | |
07b46478 | 590 | $params['payment_processor_id'] = array( |
591 | 'title' => 'Payment processor ID', | |
080a561b | 592 | 'description' => 'Providing this is strongly recommended, as not possible to calculate it accurately always', |
07b46478 | 593 | 'type' => CRM_Utils_Type::T_INT, |
594 | ); | |
080a561b | 595 | $params['fee_amount'] = array( |
596 | 'title' => 'Fee charged on transaction', | |
597 | 'description' => 'If a fee has been charged then the amount', | |
598 | 'type' => CRM_Utils_Type::T_FLOAT, | |
599 | ); | |
7104593e | 600 | $params['trxn_date'] = array( |
601 | 'title' => 'Transaction Date', | |
602 | 'description' => 'Date this transaction occurred', | |
970aa2fe | 603 | 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, |
7104593e | 604 | ); |
a55e39e9 | 605 | $params['card_type_id'] = array( |
606 | 'title' => 'Card Type ID', | |
607 | 'description' => 'Providing Credit Card Type ID', | |
608 | 'type' => CRM_Utils_Type::T_INT, | |
609 | 'pseudoconstant' => array( | |
610 | 'optionGroupName' => 'accept_creditcard', | |
611 | ), | |
612 | ); | |
0efa8efe | 613 | } |
5fa7c328 EM |
614 | |
615 | /** | |
616 | * Complete an existing (pending) transaction. | |
617 | * | |
618 | * This will update related entities (participant, membership, pledge etc) | |
619 | * and take any complete actions from the contribution page (e.g. send receipt). | |
620 | * | |
621 | * @todo - most of this should live in the BAO layer but as we want it to be an addition | |
622 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later | |
623 | * | |
624 | * @param array $params | |
625 | * Input parameters. | |
626 | * | |
e4384ce8 | 627 | * @return array |
5fa7c328 | 628 | * Api result array. |
e4384ce8 | 629 | * @throws API_Exception |
5fa7c328 EM |
630 | */ |
631 | function civicrm_api3_contribution_repeattransaction(&$params) { | |
632 | $input = $ids = array(); | |
1eade77d | 633 | civicrm_api3_verify_one_mandatory($params, NULL, array('contribution_recur_id', 'original_contribution_id')); |
634 | if (empty($params['original_contribution_id'])) { | |
018203cd | 635 | // CRM-19873 call with test mode. |
1eade77d | 636 | $params['original_contribution_id'] = civicrm_api3('contribution', 'getvalue', array( |
637 | 'return' => 'id', | |
d2334242 | 638 | 'contribution_status_id' => array('IN' => array('Completed')), |
1eade77d | 639 | 'contribution_recur_id' => $params['contribution_recur_id'], |
018203cd | 640 | 'contribution_test' => CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $params['contribution_recur_id'], 'is_test'), |
1eade77d | 641 | 'options' => array('limit' => 1, 'sort' => 'id DESC'), |
642 | )); | |
643 | } | |
5fa7c328 EM |
644 | $contribution = new CRM_Contribute_BAO_Contribution(); |
645 | $contribution->id = $params['original_contribution_id']; | |
646 | if (!$contribution->find(TRUE)) { | |
647 | throw new API_Exception( | |
648 | 'A valid original contribution ID is required', 'invalid_data'); | |
649 | } | |
d97c96dc | 650 | $original_contribution = clone $contribution; |
69a2b0bc AD |
651 | $input['payment_processor_id'] = civicrm_api3('contributionRecur', 'getvalue', array( |
652 | 'return' => 'payment_processor_id', | |
653 | 'id' => $contribution->contribution_recur_id, | |
654 | )); | |
5fa7c328 | 655 | try { |
276e3ec6 | 656 | if (!$contribution->loadRelatedObjects($input, $ids, TRUE)) { |
5fa7c328 EM |
657 | throw new API_Exception('failed to load related objects'); |
658 | } | |
d97c96dc EM |
659 | |
660 | unset($contribution->id, $contribution->receive_date, $contribution->invoice_id); | |
5fa7c328 | 661 | $contribution->receive_date = $params['receive_date']; |
96f0fe0d | 662 | |
d5580ed4 | 663 | $passThroughParams = array( |
664 | 'trxn_id', | |
665 | 'total_amount', | |
666 | 'campaign_id', | |
667 | 'fee_amount', | |
668 | 'financial_type_id', | |
669 | 'contribution_status_id', | |
670 | ); | |
96f0fe0d | 671 | $input = array_intersect_key($params, array_fill_keys($passThroughParams, NULL)); |
d97c96dc | 672 | |
734d2daa | 673 | return _ipn_process_transaction($params, $contribution, $input, $ids, $original_contribution); |
5fa7c328 EM |
674 | } |
675 | catch(Exception $e) { | |
676 | throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString()); | |
677 | } | |
678 | } | |
679 | ||
680 | /** | |
681 | * Calls IPN complete transaction for completing or repeating a transaction. | |
682 | * | |
683 | * The IPN function is overloaded with two purposes - this is simply a wrapper for that | |
684 | * when separating them in the api layer. | |
685 | * | |
686 | * @param array $params | |
687 | * @param CRM_Contribute_BAO_Contribution $contribution | |
688 | * @param array $input | |
689 | * | |
690 | * @param array $ids | |
691 | * | |
d97c96dc EM |
692 | * @param CRM_Contribute_BAO_Contribution $firstContribution |
693 | * | |
5fa7c328 EM |
694 | * @return mixed |
695 | */ | |
d97c96dc | 696 | function _ipn_process_transaction(&$params, $contribution, $input, $ids, $firstContribution = NULL) { |
5fa7c328 EM |
697 | $objects = $contribution->_relatedObjects; |
698 | $objects['contribution'] = &$contribution; | |
d97c96dc EM |
699 | |
700 | if ($firstContribution) { | |
701 | $objects['first_contribution'] = $firstContribution; | |
702 | } | |
5fa7c328 EM |
703 | $input['component'] = $contribution->_component; |
704 | $input['is_test'] = $contribution->is_test; | |
96f0fe0d EM |
705 | $input['amount'] = empty($input['total_amount']) ? $contribution->total_amount : $input['total_amount']; |
706 | ||
5fa7c328 EM |
707 | if (isset($params['is_email_receipt'])) { |
708 | $input['is_email_receipt'] = $params['is_email_receipt']; | |
709 | } | |
7104593e | 710 | if (!empty($params['trxn_date'])) { |
711 | $input['trxn_date'] = $params['trxn_date']; | |
712 | } | |
dccd9f4f ERL |
713 | if (!empty($params['receive_date'])) { |
714 | $input['receive_date'] = $params['receive_date']; | |
715 | } | |
0930231a EM |
716 | if (empty($contribution->contribution_page_id)) { |
717 | static $domainFromName; | |
718 | static $domainFromEmail; | |
719 | if (empty($domainFromEmail) && (empty($params['receipt_from_name']) || empty($params['receipt_from_email']))) { | |
37b8953e | 720 | list($domainFromName, $domainFromEmail) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE); |
0930231a EM |
721 | } |
722 | $input['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', $params, $domainFromName); | |
723 | $input['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', $params, $domainFromEmail); | |
724 | } | |
a55e39e9 | 725 | $input['card_type_id'] = CRM_Utils_Array::value('card_type_id', $params); |
726 | $input['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $params); | |
4b6c8c1e | 727 | $transaction = new CRM_Core_Transaction(); |
734d2daa | 728 | return CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $transaction, !empty |
4086637a | 729 | ($contribution->contribution_recur_id), $contribution); |
5fa7c328 EM |
730 | } |
731 | ||
732 | /** | |
733 | * Provide function metadata. | |
734 | * | |
735 | * @param array $params | |
736 | */ | |
737 | function _civicrm_api3_contribution_repeattransaction_spec(&$params) { | |
738 | $params['original_contribution_id'] = array( | |
739 | 'title' => 'Original Contribution ID', | |
1eade77d | 740 | 'description' => 'Contribution ID to copy (will be calculated from recurring contribution if not provided)', |
741 | 'type' => CRM_Utils_Type::T_INT, | |
742 | ); | |
743 | $params['contribution_recur_id'] = array( | |
744 | 'title' => 'Recurring contribution ID', | |
5fa7c328 | 745 | 'type' => CRM_Utils_Type::T_INT, |
5fa7c328 EM |
746 | ); |
747 | $params['trxn_id'] = array( | |
748 | 'title' => 'Transaction ID', | |
749 | 'type' => CRM_Utils_Type::T_STRING, | |
750 | ); | |
751 | $params['is_email_receipt'] = array( | |
752 | 'title' => 'Send email Receipt?', | |
753 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
754 | ); | |
755 | $params['contribution_status_id'] = array( | |
756 | 'title' => 'Contribution Status ID', | |
d97c96dc | 757 | 'name' => 'contribution_status_id', |
5fa7c328 EM |
758 | 'type' => CRM_Utils_Type::T_INT, |
759 | 'pseudoconstant' => array( | |
760 | 'optionGroupName' => 'contribution_status', | |
761 | ), | |
762 | 'api.required' => TRUE, | |
763 | ); | |
764 | $params['receive_date'] = array( | |
765 | 'title' => 'Contribution Receive Date', | |
d97c96dc | 766 | 'name' => 'receive_date', |
970aa2fe | 767 | 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, |
5fa7c328 EM |
768 | 'api.default' => 'now', |
769 | ); | |
d97c96dc EM |
770 | $params['trxn_id'] = array( |
771 | 'title' => 'Transaction ID', | |
772 | 'name' => 'trxn_id', | |
773 | 'type' => CRM_Utils_Type::T_STRING, | |
774 | ); | |
c02c17df | 775 | $params['campaign_id'] = array( |
776 | 'title' => 'Campaign ID', | |
777 | 'name' => 'campaign_id', | |
778 | 'type' => CRM_Utils_Type::T_INT, | |
779 | 'pseudoconstant' => array( | |
780 | 'table' => 'civicrm_campaign', | |
781 | 'keyColumn' => 'id', | |
782 | 'labelColumn' => 'title', | |
783 | ), | |
784 | ); | |
3c49d90c | 785 | $params['financial_type_id'] = array( |
786 | 'title' => 'Financial ID (ignored if more than one line item)', | |
787 | 'name' => 'financial_type_id', | |
788 | 'type' => CRM_Utils_Type::T_INT, | |
789 | 'pseudoconstant' => array( | |
790 | 'table' => 'civicrm_financial_type', | |
791 | 'keyColumn' => 'id', | |
792 | 'labelColumn' => 'name', | |
793 | ), | |
794 | ); | |
d97c96dc EM |
795 | $params['payment_processor_id'] = array( |
796 | 'description' => ts('Payment processor ID, will be loaded from contribution_recur if not provided'), | |
797 | 'title' => 'Payment processor ID', | |
798 | 'name' => 'payment_processor_id', | |
799 | 'type' => CRM_Utils_Type::T_INT, | |
800 | ); | |
5fa7c328 | 801 | } |