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