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