Commit | Line | Data |
---|---|---|
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 | */ |
44 | function 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 | */ |
94 | function _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 | 174 | function _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 | */ |
200 | function 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 | */ |
227 | function _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 | */ |
240 | function civicrm_api3_contribution_get($params) { | |
241 | ||
82f7d8b2 | 242 | $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
3123273f | 243 | $additionalOptions = _civicrm_api3_contribution_get_support_nonunique_returns($params); |
be81f101 | 244 | $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode); |
6a488035 | 245 | |
3123273f | 246 | $contributions = _civicrm_api3_get_using_query_object('Contribution', $params, $additionalOptions, NULL, $mode, $returnProperties); |
be81f101 | 247 | |
248 | foreach ($contributions as $id => $contribution) { | |
249 | $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id, TRUE); | |
250 | $contributions[$id] = array_merge($contribution, $softContribution); | |
76ca3345 | 251 | // format soft credit for backward compatibility |
be81f101 | 252 | _civicrm_api3_format_soft_credit($contributions[$id]); |
3123273f | 253 | _civicrm_api3_contribution_add_supported_fields($contributions[$id]); |
254 | ||
6a488035 | 255 | } |
be81f101 | 256 | return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get'); |
9ae25b56 | 257 | } |
258 | ||
3123273f | 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 | ||
9ae25b56 | 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; | |
6a488035 | 316 | } |
11e09c59 | 317 | |
76ca3345 | 318 | /** |
1747ab99 EM |
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 | |
76ca3345 | 322 | * but we still return first soft credit as a part of contribution array |
dc64d047 | 323 | * |
645ee340 | 324 | * @param $contribution |
76ca3345 KJ |
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 | ||
11e09c59 | 333 | /** |
1747ab99 | 334 | * Adjust Metadata for Get action. |
6a488035 | 335 | * |
0aa0303c EM |
336 | * The metadata is used for setting defaults, documentation & validation. |
337 | * | |
cf470720 | 338 | * @param array $params |
b081365f | 339 | * Array of parameters determined by getfields. |
6a488035 TO |
340 | */ |
341 | function _civicrm_api3_contribution_get_spec(&$params) { | |
d142432b EM |
342 | $params['contribution_test'] = array( |
343 | 'api.default' => 0, | |
344 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
345 | 'title' => 'Get Test Contributions?', | |
71d5a412 | 346 | 'api.aliases' => array('is_test'), |
d142432b | 347 | ); |
739a8336 | 348 | |
6a488035 | 349 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); |
e6b9e2ae | 350 | $params['payment_instrument_id']['api.aliases'] = array('contribution_payment_instrument', 'payment_instrument'); |
6a488035 TO |
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 | /** | |
1747ab99 EM |
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. | |
6a488035 | 361 | * |
cf470720 | 362 | * @param array $params |
c23f45d3 | 363 | * property name/value pairs to insert in new contact. |
cf470720 TO |
364 | * @param array $values |
365 | * The reformatted properties that we can use internally. | |
6a488035 | 366 | * |
c23f45d3 | 367 | * @return array |
6a488035 | 368 | */ |
c23f45d3 | 369 | function _civicrm_api3_contribute_format_params($params, &$values) { |
35671d00 | 370 | //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer |
6a488035 | 371 | _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values); |
6a488035 TO |
372 | return array(); |
373 | } | |
374 | ||
16c0ec8d | 375 | /** |
1747ab99 | 376 | * Adjust Metadata for Transact action. |
16c0ec8d | 377 | * |
0aa0303c EM |
378 | * The metadata is used for setting defaults, documentation & validation. |
379 | * | |
cf470720 | 380 | * @param array $params |
b081365f | 381 | * Array of parameters determined by getfields. |
16c0ec8d CW |
382 | */ |
383 | function _civicrm_api3_contribution_transact_spec(&$params) { | |
244bbdd8 | 384 | $fields = civicrm_api3('Contribution', 'getfields', array('action' => 'create')); |
4c41ecb2 | 385 | $params = array_merge($params, $fields['values']); |
16c0ec8d CW |
386 | $params['receive_date']['api.default'] = 'now'; |
387 | } | |
388 | ||
6a488035 TO |
389 | /** |
390 | * Process a transaction and record it against the contact. | |
391 | * | |
cf470720 | 392 | * @param array $params |
1747ab99 | 393 | * Input parameters. |
6a488035 | 394 | * |
a6c01b45 | 395 | * @return array |
c23f45d3 | 396 | * contribution of created or updated record (or a civicrm error) |
6a488035 TO |
397 | */ |
398 | function civicrm_api3_contribution_transact($params) { | |
16c0ec8d | 399 | // Set some params specific to payment processing |
8319cf11 EM |
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. | |
16c0ec8d CW |
404 | $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; |
405 | $params['amount'] = $params['total_amount']; | |
6a488035 TO |
406 | if (!isset($params['net_amount'])) { |
407 | $params['net_amount'] = $params['amount']; | |
408 | } | |
6a488035 TO |
409 | if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { |
410 | $params['invoiceID'] = $params['invoice_id']; | |
411 | } | |
412 | ||
be06e507 CW |
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 | ||
16c0ec8d | 416 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); |
9d8f43b1 | 417 | $paymentProcessor['object']->doPayment($params); |
6a488035 | 418 | |
26ad2b72 | 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'; |
244bbdd8 | 420 | return civicrm_api('Contribution', 'create', $params); |
6a488035 | 421 | } |
69dbd1ba | 422 | |
6a488035 | 423 | /** |
1747ab99 EM |
424 | * Send a contribution confirmation (receipt or invoice). |
425 | * | |
6a488035 TO |
426 | * The appropriate online template will be used (the existence of related objects |
427 | * (e.g. memberships ) will affect this selection | |
69dbd1ba | 428 | * |
cf470720 TO |
429 | * @param array $params |
430 | * Input parameters. | |
69dbd1ba EM |
431 | * |
432 | * @throws Exception | |
6a488035 TO |
433 | */ |
434 | function civicrm_api3_contribution_sendconfirmation($params) { | |
71687b3d CW |
435 | $ids = $values = array(); |
436 | $allowedParams = array( | |
ec7e3954 E |
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 | ); | |
71687b3d | 445 | $input = array_intersect_key($params, array_flip($allowedParams)); |
55df1211 | 446 | $input['is_email_receipt'] = TRUE; |
ec7e3954 | 447 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $params['id'], $values); |
6a488035 TO |
448 | } |
449 | ||
11e09c59 | 450 | /** |
1747ab99 | 451 | * Adjust Metadata for sendconfirmation action. |
6a488035 | 452 | * |
0aa0303c EM |
453 | * The metadata is used for setting defaults, documentation & validation. |
454 | * | |
cf470720 | 455 | * @param array $params |
b081365f | 456 | * Array of parameters determined by getfields. |
6a488035 TO |
457 | */ |
458 | function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { | |
459 | $params['id'] = array( | |
460 | 'api.required' => 1, | |
ec7e3954 | 461 | 'title' => ts('Contribution ID'), |
d142432b | 462 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
463 | ); |
464 | $params['receipt_from_email'] = array( | |
ec7e3954 | 465 | 'title' => ts('From Email address (string)'), |
d142432b | 466 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
467 | ); |
468 | $params['receipt_from_name'] = array( | |
ec7e3954 | 469 | 'title' => ts('From Name (string)'), |
d142432b | 470 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
471 | ); |
472 | $params['cc_receipt'] = array( | |
ec7e3954 | 473 | 'title' => ts('CC Email address (string)'), |
d142432b | 474 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
475 | ); |
476 | $params['bcc_receipt'] = array( | |
ec7e3954 | 477 | 'title' => ts('BCC Email address (string)'), |
d142432b | 478 | 'type' => CRM_Utils_Type::T_STRING, |
6a0cf2c6 CW |
479 | ); |
480 | $params['receipt_text'] = array( | |
ec7e3954 | 481 | 'title' => ts('Message (string)'), |
d142432b | 482 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 483 | ); |
ec7e3954 E |
484 | $params['receipt_update'] = array( |
485 | 'title' => ts('Update the Receipt Date'), | |
486 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
487 | 'api.default' => TRUE, | |
488 | ); | |
489 | $params['payment_processor_id'] = array( | |
490 | 'title' => ts('Payment processor Id (avoids mis-guesses)'), | |
491 | 'type' => CRM_Utils_Type::T_INT, | |
492 | ); | |
6a488035 | 493 | } |
0efa8efe | 494 | |
495 | /** | |
1747ab99 EM |
496 | * Complete an existing (pending) transaction. |
497 | * | |
498 | * This will update related entities (participant, membership, pledge etc) | |
499 | * and take any complete actions from the contribution page (e.g. send receipt). | |
0efa8efe | 500 | * |
501 | * @todo - most of this should live in the BAO layer but as we want it to be an addition | |
502 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later | |
503 | * | |
cf470720 TO |
504 | * @param array $params |
505 | * Input parameters. | |
69dbd1ba | 506 | * |
734d2daa | 507 | * @return array |
508 | * API result array | |
509 | * @throws \API_Exception | |
510 | * @throws \CRM_Core_Exception | |
511 | * @throws \Exception | |
0efa8efe | 512 | */ |
513 | function civicrm_api3_contribution_completetransaction(&$params) { | |
514 | ||
515 | $input = $ids = array(); | |
07b46478 | 516 | if (isset($params['payment_processor_id'])) { |
517 | $input['payment_processor_id'] = $params['payment_processor_id']; | |
518 | } | |
0efa8efe | 519 | $contribution = new CRM_Contribute_BAO_Contribution(); |
520 | $contribution->id = $params['id']; | |
521 | $contribution->find(TRUE); | |
9b873358 | 522 | if (!$contribution->id == $params['id']) { |
0efa8efe | 523 | throw new API_Exception('A valid contribution ID is required', 'invalid_data'); |
524 | } | |
5ddd6889 | 525 | |
276e3ec6 | 526 | if (!$contribution->loadRelatedObjects($input, $ids, TRUE)) { |
5ddd6889 | 527 | throw new API_Exception('failed to load related objects'); |
0efa8efe | 528 | } |
5ddd6889 EM |
529 | elseif ($contribution->contribution_status_id == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) { |
530 | throw new API_Exception(ts('Contribution already completed'), 'contribution_completed'); | |
0efa8efe | 531 | } |
5ddd6889 | 532 | $input['trxn_id'] = !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id; |
080a561b | 533 | if (!empty($params['fee_amount'])) { |
534 | $input['fee_amount'] = $params['fee_amount']; | |
535 | } | |
734d2daa | 536 | return _ipn_process_transaction($params, $contribution, $input, $ids); |
5ddd6889 | 537 | |
0efa8efe | 538 | } |
539 | ||
aa1b1481 | 540 | /** |
9d32e6f7 EM |
541 | * Provide function metadata. |
542 | * | |
c490a46a | 543 | * @param array $params |
aa1b1481 | 544 | */ |
3dfbdece | 545 | function _civicrm_api3_contribution_completetransaction_spec(&$params) { |
eac25df2 EM |
546 | $params['id'] = array( |
547 | 'title' => 'Contribution ID', | |
548 | 'type' => CRM_Utils_Type::T_INT, | |
549 | 'api.required' => TRUE, | |
550 | ); | |
551 | $params['trxn_id'] = array( | |
552 | 'title' => 'Transaction ID', | |
553 | 'type' => CRM_Utils_Type::T_STRING, | |
554 | ); | |
555 | $params['is_email_receipt'] = array( | |
556 | 'title' => 'Send email Receipt?', | |
557 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
558 | ); | |
0930231a EM |
559 | $params['receipt_from_email'] = array( |
560 | 'title' => 'Email to send receipt from.', | |
561 | 'description' => 'If not provided this will default to being based on domain mail or contribution page', | |
562 | 'type' => CRM_Utils_Type::T_EMAIL, | |
563 | ); | |
564 | $params['receipt_from_name'] = array( | |
565 | 'title' => 'Name to send receipt from', | |
566 | 'description' => '. If not provided this will default to domain mail or contribution page', | |
567 | 'type' => CRM_Utils_Type::T_STRING, | |
568 | ); | |
07b46478 | 569 | $params['payment_processor_id'] = array( |
570 | 'title' => 'Payment processor ID', | |
080a561b | 571 | 'description' => 'Providing this is strongly recommended, as not possible to calculate it accurately always', |
07b46478 | 572 | 'type' => CRM_Utils_Type::T_INT, |
573 | ); | |
080a561b | 574 | $params['fee_amount'] = array( |
575 | 'title' => 'Fee charged on transaction', | |
576 | 'description' => 'If a fee has been charged then the amount', | |
577 | 'type' => CRM_Utils_Type::T_FLOAT, | |
578 | ); | |
7104593e | 579 | $params['trxn_date'] = array( |
580 | 'title' => 'Transaction Date', | |
581 | 'description' => 'Date this transaction occurred', | |
970aa2fe | 582 | 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, |
7104593e | 583 | ); |
0efa8efe | 584 | } |
5fa7c328 EM |
585 | |
586 | /** | |
587 | * Complete an existing (pending) transaction. | |
588 | * | |
589 | * This will update related entities (participant, membership, pledge etc) | |
590 | * and take any complete actions from the contribution page (e.g. send receipt). | |
591 | * | |
592 | * @todo - most of this should live in the BAO layer but as we want it to be an addition | |
593 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later | |
594 | * | |
595 | * @param array $params | |
596 | * Input parameters. | |
597 | * | |
e4384ce8 | 598 | * @return array |
5fa7c328 | 599 | * Api result array. |
e4384ce8 | 600 | * @throws API_Exception |
5fa7c328 EM |
601 | */ |
602 | function civicrm_api3_contribution_repeattransaction(&$params) { | |
603 | $input = $ids = array(); | |
1eade77d | 604 | civicrm_api3_verify_one_mandatory($params, NULL, array('contribution_recur_id', 'original_contribution_id')); |
605 | if (empty($params['original_contribution_id'])) { | |
018203cd | 606 | // CRM-19873 call with test mode. |
1eade77d | 607 | $params['original_contribution_id'] = civicrm_api3('contribution', 'getvalue', array( |
608 | 'return' => 'id', | |
d2334242 | 609 | 'contribution_status_id' => array('IN' => array('Completed')), |
1eade77d | 610 | 'contribution_recur_id' => $params['contribution_recur_id'], |
018203cd | 611 | 'contribution_test' => CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $params['contribution_recur_id'], 'is_test'), |
1eade77d | 612 | 'options' => array('limit' => 1, 'sort' => 'id DESC'), |
613 | )); | |
614 | } | |
5fa7c328 EM |
615 | $contribution = new CRM_Contribute_BAO_Contribution(); |
616 | $contribution->id = $params['original_contribution_id']; | |
617 | if (!$contribution->find(TRUE)) { | |
618 | throw new API_Exception( | |
619 | 'A valid original contribution ID is required', 'invalid_data'); | |
620 | } | |
d97c96dc | 621 | $original_contribution = clone $contribution; |
69a2b0bc AD |
622 | $input['payment_processor_id'] = civicrm_api3('contributionRecur', 'getvalue', array( |
623 | 'return' => 'payment_processor_id', | |
624 | 'id' => $contribution->contribution_recur_id, | |
625 | )); | |
5fa7c328 | 626 | try { |
276e3ec6 | 627 | if (!$contribution->loadRelatedObjects($input, $ids, TRUE)) { |
5fa7c328 EM |
628 | throw new API_Exception('failed to load related objects'); |
629 | } | |
d97c96dc EM |
630 | |
631 | unset($contribution->id, $contribution->receive_date, $contribution->invoice_id); | |
5fa7c328 | 632 | $contribution->receive_date = $params['receive_date']; |
96f0fe0d | 633 | |
d5580ed4 | 634 | $passThroughParams = array( |
635 | 'trxn_id', | |
636 | 'total_amount', | |
637 | 'campaign_id', | |
638 | 'fee_amount', | |
639 | 'financial_type_id', | |
640 | 'contribution_status_id', | |
641 | ); | |
96f0fe0d | 642 | $input = array_intersect_key($params, array_fill_keys($passThroughParams, NULL)); |
d97c96dc | 643 | |
734d2daa | 644 | return _ipn_process_transaction($params, $contribution, $input, $ids, $original_contribution); |
5fa7c328 EM |
645 | } |
646 | catch(Exception $e) { | |
647 | throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString()); | |
648 | } | |
649 | } | |
650 | ||
651 | /** | |
652 | * Calls IPN complete transaction for completing or repeating a transaction. | |
653 | * | |
654 | * The IPN function is overloaded with two purposes - this is simply a wrapper for that | |
655 | * when separating them in the api layer. | |
656 | * | |
657 | * @param array $params | |
658 | * @param CRM_Contribute_BAO_Contribution $contribution | |
659 | * @param array $input | |
660 | * | |
661 | * @param array $ids | |
662 | * | |
d97c96dc EM |
663 | * @param CRM_Contribute_BAO_Contribution $firstContribution |
664 | * | |
5fa7c328 EM |
665 | * @return mixed |
666 | */ | |
d97c96dc | 667 | function _ipn_process_transaction(&$params, $contribution, $input, $ids, $firstContribution = NULL) { |
5fa7c328 EM |
668 | $objects = $contribution->_relatedObjects; |
669 | $objects['contribution'] = &$contribution; | |
d97c96dc EM |
670 | |
671 | if ($firstContribution) { | |
672 | $objects['first_contribution'] = $firstContribution; | |
673 | } | |
5fa7c328 EM |
674 | $input['component'] = $contribution->_component; |
675 | $input['is_test'] = $contribution->is_test; | |
96f0fe0d EM |
676 | $input['amount'] = empty($input['total_amount']) ? $contribution->total_amount : $input['total_amount']; |
677 | ||
5fa7c328 EM |
678 | if (isset($params['is_email_receipt'])) { |
679 | $input['is_email_receipt'] = $params['is_email_receipt']; | |
680 | } | |
7104593e | 681 | if (!empty($params['trxn_date'])) { |
682 | $input['trxn_date'] = $params['trxn_date']; | |
683 | } | |
dccd9f4f ERL |
684 | if (!empty($params['receive_date'])) { |
685 | $input['receive_date'] = $params['receive_date']; | |
686 | } | |
0930231a EM |
687 | if (empty($contribution->contribution_page_id)) { |
688 | static $domainFromName; | |
689 | static $domainFromEmail; | |
690 | if (empty($domainFromEmail) && (empty($params['receipt_from_name']) || empty($params['receipt_from_email']))) { | |
37b8953e | 691 | list($domainFromName, $domainFromEmail) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE); |
0930231a EM |
692 | } |
693 | $input['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', $params, $domainFromName); | |
694 | $input['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', $params, $domainFromEmail); | |
695 | } | |
4b6c8c1e | 696 | $transaction = new CRM_Core_Transaction(); |
734d2daa | 697 | return CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $transaction, !empty |
698 | ($contribution->contribution_recur_id), $contribution, | |
59cdadfc | 699 | FALSE, FALSE); |
5fa7c328 EM |
700 | } |
701 | ||
702 | /** | |
703 | * Provide function metadata. | |
704 | * | |
705 | * @param array $params | |
706 | */ | |
707 | function _civicrm_api3_contribution_repeattransaction_spec(&$params) { | |
708 | $params['original_contribution_id'] = array( | |
709 | 'title' => 'Original Contribution ID', | |
1eade77d | 710 | 'description' => 'Contribution ID to copy (will be calculated from recurring contribution if not provided)', |
711 | 'type' => CRM_Utils_Type::T_INT, | |
712 | ); | |
713 | $params['contribution_recur_id'] = array( | |
714 | 'title' => 'Recurring contribution ID', | |
5fa7c328 | 715 | 'type' => CRM_Utils_Type::T_INT, |
5fa7c328 EM |
716 | ); |
717 | $params['trxn_id'] = array( | |
718 | 'title' => 'Transaction ID', | |
719 | 'type' => CRM_Utils_Type::T_STRING, | |
720 | ); | |
721 | $params['is_email_receipt'] = array( | |
722 | 'title' => 'Send email Receipt?', | |
723 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
724 | ); | |
725 | $params['contribution_status_id'] = array( | |
726 | 'title' => 'Contribution Status ID', | |
d97c96dc | 727 | 'name' => 'contribution_status_id', |
5fa7c328 EM |
728 | 'type' => CRM_Utils_Type::T_INT, |
729 | 'pseudoconstant' => array( | |
730 | 'optionGroupName' => 'contribution_status', | |
731 | ), | |
732 | 'api.required' => TRUE, | |
733 | ); | |
734 | $params['receive_date'] = array( | |
735 | 'title' => 'Contribution Receive Date', | |
d97c96dc | 736 | 'name' => 'receive_date', |
970aa2fe | 737 | 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, |
5fa7c328 EM |
738 | 'api.default' => 'now', |
739 | ); | |
d97c96dc EM |
740 | $params['trxn_id'] = array( |
741 | 'title' => 'Transaction ID', | |
742 | 'name' => 'trxn_id', | |
743 | 'type' => CRM_Utils_Type::T_STRING, | |
744 | ); | |
c02c17df | 745 | $params['campaign_id'] = array( |
746 | 'title' => 'Campaign ID', | |
747 | 'name' => 'campaign_id', | |
748 | 'type' => CRM_Utils_Type::T_INT, | |
749 | 'pseudoconstant' => array( | |
750 | 'table' => 'civicrm_campaign', | |
751 | 'keyColumn' => 'id', | |
752 | 'labelColumn' => 'title', | |
753 | ), | |
754 | ); | |
3c49d90c | 755 | $params['financial_type_id'] = array( |
756 | 'title' => 'Financial ID (ignored if more than one line item)', | |
757 | 'name' => 'financial_type_id', | |
758 | 'type' => CRM_Utils_Type::T_INT, | |
759 | 'pseudoconstant' => array( | |
760 | 'table' => 'civicrm_financial_type', | |
761 | 'keyColumn' => 'id', | |
762 | 'labelColumn' => 'name', | |
763 | ), | |
764 | ); | |
d97c96dc EM |
765 | $params['payment_processor_id'] = array( |
766 | 'description' => ts('Payment processor ID, will be loaded from contribution_recur if not provided'), | |
767 | 'title' => 'Payment processor ID', | |
768 | 'name' => 'payment_processor_id', | |
769 | 'type' => CRM_Utils_Type::T_INT, | |
770 | ); | |
5fa7c328 | 771 | } |