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