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