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