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