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