Merge pull request #20837 from colemanw/customACLs
[civicrm-core.git] / api / v3 / Contribution.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Contribution records.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 use Civi\Api4\Contribution;
19
20 /**
21 * Add or update a Contribution.
22 *
23 * @param array $params
24 * Input parameters.
25 *
26 * @throws API_Exception
27 * @return array
28 * Api result array
29 */
30 function civicrm_api3_contribution_create($params) {
31 $values = [];
32 _civicrm_api3_custom_format_params($params, $values, 'Contribution');
33 $params = array_merge($params, $values);
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.)
36 if (empty($params['skipCleanMoney'])) {
37 foreach (['total_amount', 'net_amount', 'fee_amount', 'non_deductible_amount'] as $field) {
38 if (isset($params[$field])) {
39 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
40 }
41 }
42 }
43 $params['skipCleanMoney'] = TRUE;
44
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']) {
53 throw new API_Exception('You do not have permission to create this contribution');
54 }
55 }
56 if (!empty($params['id']) && !empty($params['contribution_status_id'])) {
57 $error = [];
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 }
65 }
66 if (!empty($params['id']) && !empty($params['financial_type_id'])) {
67 $error = [];
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 }
73 _civicrm_api3_contribution_create_legacy_support_45($params);
74
75 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution');
76 }
77
78 /**
79 * Adjust Metadata for Create action.
80 *
81 * The metadata is used for setting defaults, documentation & validation.
82 *
83 * @param array $params
84 * Array of parameters determined by getfields.
85 */
86 function _civicrm_api3_contribution_create_spec(&$params) {
87 $params['contact_id']['api.required'] = 1;
88 $params['total_amount']['api.required'] = 1;
89 $params['payment_instrument_id']['api.aliases'] = ['payment_instrument'];
90 $params['receive_date']['api.default'] = 'now';
91 $params['receive_date']['api.required'] = TRUE;
92 $params['payment_processor'] = [
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
98 'api.aliases' => ['payment_processor_id'],
99 'type' => CRM_Utils_Type::T_INT,
100 ];
101 $params['financial_type_id']['api.aliases'] = ['contribution_type_id', 'contribution_type'];
102 $params['financial_type_id']['api.required'] = 1;
103 $params['note'] = [
104 'name' => 'note',
105 'uniqueName' => 'contribution_note',
106 'title' => 'note',
107 'type' => 2,
108 'description' => 'Associated Note in the notes table',
109 ];
110 $params['soft_credit_to'] = [
111 'name' => 'soft_credit_to',
112 'title' => 'Soft Credit contact ID (legacy)',
113 'type' => CRM_Utils_Type::T_INT,
114 'description' => 'ID of Contact to be Soft credited to (deprecated - use contribution_soft api)',
115 'FKClassName' => 'CRM_Contact_DAO_Contact',
116 ];
117 $params['honor_contact_id'] = [
118 'name' => 'honor_contact_id',
119 'title' => 'Honoree contact ID (legacy)',
120 'type' => CRM_Utils_Type::T_INT,
121 'description' => 'ID of honoree contact (deprecated - use contribution_soft api)',
122 'FKClassName' => 'CRM_Contact_DAO_Contact',
123 ];
124 $params['honor_type_id'] = [
125 'name' => 'honor_type_id',
126 'title' => 'Honoree Type (legacy)',
127 'type' => CRM_Utils_Type::T_INT,
128 'description' => 'Type of honoree contact (deprecated - use contribution_soft api)',
129 'pseudoconstant' => TRUE,
130 ];
131 // note this is a recommended option but not adding as a default to avoid
132 // creating unnecessary changes for the dev
133 $params['skipRecentView'] = [
134 'name' => 'skipRecentView',
135 'title' => 'Skip adding to recent view',
136 'type' => CRM_Utils_Type::T_BOOLEAN,
137 'description' => 'Do not add to recent view (setting this improves performance)',
138 ];
139 $params['skipLineItem'] = [
140 'name' => 'skipLineItem',
141 'title' => 'Skip adding line items',
142 'type' => CRM_Utils_Type::T_BOOLEAN,
143 'api.default' => 0,
144 'description' => 'Do not add line items by default (if you wish to add your own)',
145 ];
146 $params['batch_id'] = [
147 'title' => 'Batch',
148 'type' => CRM_Utils_Type::T_INT,
149 'description' => 'Batch which relevant transactions should be added to',
150 ];
151 $params['refund_trxn_id'] = [
152 'title' => 'Refund Transaction ID',
153 'type' => CRM_Utils_Type::T_STRING,
154 'description' => 'Transaction ID specific to the refund taking place',
155 ];
156 $params['card_type_id'] = [
157 'title' => 'Card Type ID',
158 'description' => 'Providing Credit Card Type ID',
159 'type' => CRM_Utils_Type::T_INT,
160 'pseudoconstant' => [
161 'optionGroupName' => 'accept_creditcard',
162 ],
163 ];
164 }
165
166 /**
167 * Support for schema changes made in 4.5.
168 *
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.
172 *
173 * @param array $params
174 */
175 function _civicrm_api3_contribution_create_legacy_support_45(&$params) {
176 //legacy soft credit handling - recommended approach is chaining
177 if (!empty($params['soft_credit_to'])) {
178 $params['soft_credit'][] = [
179 'contact_id' => $params['soft_credit_to'],
180 'amount' => $params['total_amount'],
181 'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
182 ];
183 }
184 if (!empty($params['honor_contact_id'])) {
185 $params['soft_credit'][] = [
186 'contact_id' => $params['honor_contact_id'],
187 'amount' => $params['total_amount'],
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')),
189 ];
190 }
191 }
192
193 /**
194 * Delete a Contribution.
195 *
196 * @param array $params
197 * Input parameters.
198 *
199 * @return array
200 * @throws \API_Exception
201 */
202 function civicrm_api3_contribution_delete($params) {
203
204 $contributionID = !empty($params['contribution_id']) ? $params['contribution_id'] : $params['id'];
205 if (!empty($params['check_permissions']) && !\Civi\Api4\Utils\CoreUtil::checkAccessDelegated('Contribution', 'delete', ['id' => $contributionID], CRM_Core_Session::getLoggedInContactID() ?: 0)) {
206 throw new API_Exception('You do not have permission to delete this contribution');
207 }
208 if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) {
209 return civicrm_api3_create_success([$contributionID => 1]);
210 }
211 throw new API_Exception('Could not delete contribution');
212 }
213
214 /**
215 * Modify metadata for delete action.
216 *
217 * Legacy support for contribution_id.
218 *
219 * @param array $params
220 */
221 function _civicrm_api3_contribution_delete_spec(&$params) {
222 $params['id']['api.aliases'] = ['contribution_id'];
223 }
224
225 /**
226 * Retrieve a set of contributions.
227 *
228 * @param array $params
229 * Input parameters.
230 *
231 * @return array
232 * Array of contributions, if error an array with an error id and error message
233 */
234 function civicrm_api3_contribution_get($params) {
235
236 $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
237 $additionalOptions = _civicrm_api3_contribution_get_support_nonunique_returns($params);
238 $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode);
239
240 // Get the contributions based on parameters passed in
241 $contributions = _civicrm_api3_get_using_query_object('Contribution', $params, $additionalOptions, NULL, $mode, $returnProperties);
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 }
250 }
251 return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get');
252 }
253
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) {
268 $additionalOptions = [];
269 $options = _civicrm_api3_get_options_from_params($params, TRUE);
270 foreach (['check_number', 'address_id', 'cancel_date'] as $changedVariable) {
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.
288 $outputAliases = [
289 'contribution_check_number' => 'check_number',
290 'contribution_address_id' => 'address_id',
291 'payment_instrument_id' => 'instrument_id',
292 'contribution_cancel_date' => 'cancel_date',
293 ];
294 foreach ($outputAliases as $returnName => $copyTo) {
295 if (array_key_exists($returnName, $contribution)) {
296 $contribution[$copyTo] = $contribution[$returnName];
297 }
298 }
299
300 }
301
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) {
310 $count = _civicrm_api3_get_using_query_object('Contribution', $params, [], TRUE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE);
311 return (int) $count;
312 }
313
314 /**
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
318 * but we still return first soft credit as a part of contribution array
319 *
320 * @param $contribution
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
329 /**
330 * Adjust Metadata for Get action.
331 *
332 * The metadata is used for setting defaults, documentation & validation.
333 *
334 * @param array $params
335 * Array of parameters determined by getfields.
336 */
337 function _civicrm_api3_contribution_get_spec(&$params) {
338 $params['contribution_test'] = [
339 'api.default' => 0,
340 'type' => CRM_Utils_Type::T_BOOLEAN,
341 'title' => 'Get Test Contributions?',
342 'api.aliases' => ['is_test'],
343 ];
344
345 $params['financial_type_id']['api.aliases'] = ['contribution_type_id'];
346 $params['payment_instrument_id']['api.aliases'] = ['contribution_payment_instrument', 'payment_instrument'];
347 $params['contact_id'] = $params['contribution_contact_id'] ?? NULL;
348 $params['contact_id']['api.aliases'] = ['contribution_contact_id'];
349 $params['is_template']['api.default'] = 0;
350 unset($params['contribution_contact_id']);
351 }
352
353 /**
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.
358 *
359 * @param array $params
360 * property name/value pairs to insert in new contact.
361 * @param array $values
362 * The reformatted properties that we can use internally.
363 *
364 * @return array
365 */
366 function _civicrm_api3_contribute_format_params($params, &$values) {
367 //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer
368 _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values);
369 return [];
370 }
371
372 /**
373 * Send a contribution confirmation (receipt or invoice).
374 *
375 * The appropriate online template will be used (the existence of related objects
376 * (e.g. memberships ) will affect this selection
377 *
378 * @param array $params
379 * Input parameters.
380 *
381 * @throws Exception
382 */
383 function civicrm_api3_contribution_sendconfirmation($params) {
384 $ids = [];
385 $allowedParams = [
386 'receipt_from_email',
387 'receipt_from_name',
388 'receipt_update',
389 'cc_receipt',
390 'bcc_receipt',
391 'receipt_text',
392 'pay_later_receipt',
393 'payment_processor_id',
394 ];
395 $input = array_intersect_key($params, array_flip($allowedParams));
396 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $params['id']);
397 return [];
398 }
399
400 /**
401 * Adjust Metadata for sendconfirmation action.
402 *
403 * The metadata is used for setting defaults, documentation & validation.
404 *
405 * @param array $params
406 * Array of parameters determined by getfields.
407 */
408 function _civicrm_api3_contribution_sendconfirmation_spec(&$params) {
409 $params['id'] = [
410 'api.required' => 1,
411 'title' => ts('Contribution ID'),
412 'type' => CRM_Utils_Type::T_INT,
413 ];
414 $params['receipt_from_email'] = [
415 'title' => ts('From Email address (string)'),
416 'type' => CRM_Utils_Type::T_STRING,
417 ];
418 $params['receipt_from_name'] = [
419 'title' => ts('From Name (string)'),
420 'type' => CRM_Utils_Type::T_STRING,
421 ];
422 $params['cc_receipt'] = [
423 'title' => ts('CC Email address (string)'),
424 'type' => CRM_Utils_Type::T_STRING,
425 ];
426 $params['bcc_receipt'] = [
427 'title' => ts('BCC Email address (string)'),
428 'type' => CRM_Utils_Type::T_STRING,
429 ];
430 $params['receipt_text'] = [
431 'title' => ts('Message (string)'),
432 'type' => CRM_Utils_Type::T_STRING,
433 ];
434 $params['pay_later_receipt'] = [
435 'title' => ts('Pay Later Message (string)'),
436 'type' => CRM_Utils_Type::T_STRING,
437 ];
438 $params['receipt_update'] = [
439 'title' => ts('Update the Receipt Date'),
440 'type' => CRM_Utils_Type::T_BOOLEAN,
441 'api.default' => TRUE,
442 ];
443 $params['payment_processor_id'] = [
444 'title' => ts('Payment processor Id (avoids mis-guesses)'),
445 'type' => CRM_Utils_Type::T_INT,
446 ];
447 }
448
449 /**
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).
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 *
458 * @param array $params
459 * Input parameters.
460 *
461 * @return array
462 * API result array
463 * @throws \API_Exception
464 * @throws \CRM_Core_Exception
465 * @throws \Exception
466 */
467 function civicrm_api3_contribution_completetransaction($params) {
468 $contribution = new CRM_Contribute_BAO_Contribution();
469 $contribution->id = $params['id'];
470 if (!$contribution->find(TRUE)) {
471 throw new API_Exception('A valid contribution ID is required', 'invalid_data');
472 }
473 if ($contribution->contribution_status_id == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) {
474 throw new API_Exception(ts('Contribution already completed'), 'contribution_completed');
475 }
476
477 $params['trxn_id'] = $params['trxn_id'] ?? $contribution->trxn_id;
478
479 $passThroughParams = [
480 'fee_amount',
481 'payment_processor_id',
482 'trxn_id',
483 ];
484 $input = array_intersect_key($params, array_fill_keys($passThroughParams, NULL));
485
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
491 return _ipn_process_transaction($params, $contribution, $input, $ids);
492 }
493
494 /**
495 * Provide function metadata.
496 *
497 * @param array $params
498 */
499 function _civicrm_api3_contribution_completetransaction_spec(&$params) {
500 $params['id'] = [
501 'title' => 'Contribution ID',
502 'type' => CRM_Utils_Type::T_INT,
503 'api.required' => TRUE,
504 ];
505 $params['trxn_id'] = [
506 'title' => 'Transaction ID',
507 'type' => CRM_Utils_Type::T_STRING,
508 ];
509 $params['is_email_receipt'] = [
510 'title' => 'Send email Receipt?',
511 'type' => CRM_Utils_Type::T_BOOLEAN,
512 ];
513 $params['receipt_from_email'] = [
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,
517 ];
518 $params['receipt_from_name'] = [
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,
522 ];
523 $params['payment_processor_id'] = [
524 'title' => 'Payment processor ID',
525 'description' => 'Providing this is strongly recommended, as not possible to calculate it accurately always',
526 'type' => CRM_Utils_Type::T_INT,
527 ];
528 $params['fee_amount'] = [
529 'title' => 'Fee charged on transaction',
530 'description' => 'If a fee has been charged then the amount',
531 'type' => CRM_Utils_Type::T_FLOAT,
532 ];
533 $params['trxn_date'] = [
534 'title' => 'Transaction Date',
535 'description' => 'Date this transaction occurred',
536 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
537 ];
538 $params['card_type_id'] = [
539 'title' => 'Card Type ID',
540 'description' => 'Providing Credit Card Type ID',
541 'type' => CRM_Utils_Type::T_INT,
542 'pseudoconstant' => [
543 'optionGroupName' => 'accept_creditcard',
544 ],
545 ];
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 ];
555 }
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 *
569 * @return array
570 * Api result array.
571 * @throws API_Exception
572 */
573 function civicrm_api3_contribution_repeattransaction($params) {
574 civicrm_api3_verify_one_mandatory($params, NULL, ['contribution_recur_id', 'original_contribution_id']);
575 if (empty($params['original_contribution_id'])) {
576 // CRM-19873 call with test mode.
577 $params['original_contribution_id'] = civicrm_api3('contribution', 'getvalue', [
578 'return' => 'id',
579 'contribution_status_id' => ['IN' => ['Completed']],
580 'contribution_recur_id' => $params['contribution_recur_id'],
581 'contribution_test' => CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $params['contribution_recur_id'], 'is_test'),
582 'options' => ['limit' => 1, 'sort' => 'id DESC'],
583 ]);
584 }
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 }
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
599 $params['payment_processor_id'] = civicrm_api3('contributionRecur', 'getvalue', [
600 'return' => 'payment_processor_id',
601 'id' => $contribution->contribution_recur_id,
602 ]);
603
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
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
623 return _ipn_process_transaction($params, $contribution, $input, $ids);
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
639 * @throws \CRM_Core_Exception
640 * @throws \CiviCRM_API3_Exception
641 */
642 function _ipn_process_transaction($params, $contribution, $input, $ids) {
643 $objects = $contribution->_relatedObjects;
644 $objects['contribution'] = &$contribution;
645 $input['component'] = $contribution->_component;
646 $input['is_test'] = $contribution->is_test;
647 $input['amount'] = empty($input['total_amount']) ? $contribution->total_amount : $input['total_amount'];
648
649 if (isset($params['is_email_receipt'])) {
650 $input['is_email_receipt'] = $params['is_email_receipt'];
651 }
652 if (!empty($params['trxn_date'])) {
653 $input['trxn_date'] = $params['trxn_date'];
654 }
655 if (!empty($params['receive_date'])) {
656 $input['receive_date'] = $params['receive_date'];
657 }
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']))) {
662 [$domainFromName, $domainFromEmail] = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
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 }
667 $input['card_type_id'] = $params['card_type_id'] ?? NULL;
668 $input['pan_truncation'] = $params['pan_truncation'] ?? NULL;
669 if (!empty($params['payment_instrument_id'])) {
670 $input['payment_instrument_id'] = $params['payment_instrument_id'];
671 }
672 return CRM_Contribute_BAO_Contribution::completeOrder($input,
673 !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
674 $objects['contribution']->id ?? NULL,
675 $params['is_post_payment_create'] ?? NULL);
676 }
677
678 /**
679 * Provide function metadata.
680 *
681 * @param array $params
682 */
683 function _civicrm_api3_contribution_repeattransaction_spec(&$params) {
684 $params['original_contribution_id'] = [
685 'title' => 'Original Contribution ID',
686 'description' => 'Contribution ID to copy (will be calculated from recurring contribution if not provided)',
687 'type' => CRM_Utils_Type::T_INT,
688 ];
689 $params['contribution_recur_id'] = [
690 'title' => 'Recurring contribution ID',
691 'type' => CRM_Utils_Type::T_INT,
692 ];
693 $params['trxn_id'] = [
694 'title' => 'Transaction ID',
695 'type' => CRM_Utils_Type::T_STRING,
696 ];
697 $params['is_email_receipt'] = [
698 'title' => 'Send email Receipt?',
699 'type' => CRM_Utils_Type::T_BOOLEAN,
700 ];
701 $params['contribution_status_id'] = [
702 'title' => 'Contribution Status ID',
703 'name' => 'contribution_status_id',
704 'type' => CRM_Utils_Type::T_INT,
705 'pseudoconstant' => [
706 'optionGroupName' => 'contribution_status',
707 ],
708 'api.required' => TRUE,
709 'api.default' => 'Pending',
710 ];
711 $params['receive_date'] = [
712 'title' => 'Contribution Receive Date',
713 'name' => 'receive_date',
714 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
715 'api.default' => 'now',
716 ];
717 $params['trxn_id'] = [
718 'title' => 'Transaction ID',
719 'name' => 'trxn_id',
720 'type' => CRM_Utils_Type::T_STRING,
721 ];
722 $params['campaign_id'] = [
723 'title' => 'Campaign ID',
724 'name' => 'campaign_id',
725 'type' => CRM_Utils_Type::T_INT,
726 'pseudoconstant' => [
727 'table' => 'civicrm_campaign',
728 'keyColumn' => 'id',
729 'labelColumn' => 'title',
730 ],
731 ];
732 $params['financial_type_id'] = [
733 'title' => 'Financial ID (ignored if more than one line item)',
734 'name' => 'financial_type_id',
735 'type' => CRM_Utils_Type::T_INT,
736 'pseudoconstant' => [
737 'table' => 'civicrm_financial_type',
738 'keyColumn' => 'id',
739 'labelColumn' => 'name',
740 ],
741 ];
742 $params['payment_processor_id'] = [
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,
747 ];
748 }
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 }