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