| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 11 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 12 | | under the terms of the GNU Affero General Public License | |
| 13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 14 | | | |
| 15 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 18 | | See the GNU Affero General Public License for more details. | |
| 19 | | | |
| 20 | | You should have received a copy of the GNU Affero General Public | |
| 21 | | License and the CiviCRM Licensing Exception along | |
| 22 | | with this program; if not, contact CiviCRM LLC | |
| 23 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 24 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 26 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * File for the CiviCRM APIv3 Contribution functions |
| 31 | * |
| 32 | * @package CiviCRM_APIv3 |
| 33 | * @subpackage API_Contribute |
| 34 | * |
| 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 36 | * @version $Id: Contribution.php 30486 2010-11-02 16:12:09Z shot $ |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Add or update a contribution |
| 42 | * |
| 43 | * @param array $params (reference ) input parameters |
| 44 | * |
| 45 | * @throws API_Exception |
| 46 | * @return array Api result array |
| 47 | * @static void |
| 48 | * @access public |
| 49 | * @example ContributionCreate.php |
| 50 | * {@getfields Contribution_create} |
| 51 | */ |
| 52 | function civicrm_api3_contribution_create(&$params) { |
| 53 | $values = array(); |
| 54 | _civicrm_api3_custom_format_params($params, $values, 'Contribution'); |
| 55 | $params = array_merge($params, $values); |
| 56 | |
| 57 | if (!empty($params['id']) && !empty($params['contribution_status_id'])) { |
| 58 | $error = array(); |
| 59 | //throw error for invalid status change such as setting completed back to pending |
| 60 | //@todo this sort of validation belongs in the BAO not the API - if it is not an OK |
| 61 | // action it needs to be blocked there. If it is Ok through a form it needs to be OK through the api |
| 62 | CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error); |
| 63 | if (array_key_exists('contribution_status_id', $error)) { |
| 64 | throw new API_Exception($error['contribution_status_id']); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | _civicrm_api3_contribution_create_legacy_support_45($params); |
| 69 | |
| 70 | // make sure tax calculation is handled via api |
| 71 | $params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params); |
| 72 | |
| 73 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution'); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Adjust Metadata for Create action |
| 78 | * |
| 79 | * The metadata is used for setting defaults, documentation & validation |
| 80 | * @param array $params array or parameters determined by getfields |
| 81 | */ |
| 82 | function _civicrm_api3_contribution_create_spec(&$params) { |
| 83 | $params['contact_id']['api.required'] = 1; |
| 84 | $params['total_amount']['api.required'] = 1; |
| 85 | $params['payment_instrument_id']['api.aliases'] = array('payment_instrument'); |
| 86 | $params['receive_date']['api.default'] = 'now'; |
| 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'), |
| 94 | ); |
| 95 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); |
| 96 | $params['financial_type_id']['api.required'] = 1; |
| 97 | $params['note'] = array( |
| 98 | 'name' => 'note', |
| 99 | 'uniqueName' => 'contribution_note', |
| 100 | 'title' => 'note', |
| 101 | 'type' => 2, |
| 102 | 'description' => 'Associated Note in the notes table', |
| 103 | ); |
| 104 | $params['soft_credit_to'] = array( |
| 105 | 'name' => 'soft_credit_to', |
| 106 | 'title' => 'Soft Credit contact ID', |
| 107 | 'type' => 1, |
| 108 | 'description' => 'ID of Contact to be Soft credited to', |
| 109 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
| 110 | ); |
| 111 | $params['honor_contact_id'] = array( |
| 112 | 'name' => 'honor_contact_id', |
| 113 | 'title' => 'Honoree contact ID', |
| 114 | 'type' => 1, |
| 115 | 'description' => 'ID of honoree contact', |
| 116 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
| 117 | ); |
| 118 | // note this is a recommended option but not adding as a default to avoid |
| 119 | // creating unnecessary changes for the dev |
| 120 | $params['skipRecentView'] = array( |
| 121 | 'name' => 'skipRecentView', |
| 122 | 'title' => 'Skip adding to recent view', |
| 123 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 124 | 'description' => 'Do not add to recent view (setting this improves performance)', |
| 125 | ); |
| 126 | $params['skipLineItem'] = array( |
| 127 | 'name' => 'skipLineItem', |
| 128 | 'title' => 'Skip adding line items', |
| 129 | 'type' => 1, |
| 130 | 'api.default' => 0, |
| 131 | 'description' => 'Do not add line items by default (if you wish to add your own)', |
| 132 | ); |
| 133 | $params['batch_id'] = array( |
| 134 | 'title' => 'Batch', |
| 135 | 'type' => 1, |
| 136 | 'description' => 'Batch which relevant transactions should be added to', |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Support for schema changes made in 4.5 |
| 142 | * The main purpose of the API is to provide integrators a level of stability not provided by |
| 143 | * the core code or schema - this means we have to provide support for api calls (where possible) |
| 144 | * across schema changes. |
| 145 | */ |
| 146 | function _civicrm_api3_contribution_create_legacy_support_45(&$params){ |
| 147 | //legacy soft credit handling - recommended approach is chaining |
| 148 | if(!empty($params['soft_credit_to'])){ |
| 149 | $params['soft_credit'] = array(array( |
| 150 | 'contact_id' => $params['soft_credit_to'], |
| 151 | 'amount' => $params['total_amount'], |
| 152 | 'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"))); |
| 153 | } |
| 154 | if(!empty($params['honor_contact_id'])){ |
| 155 | $params['soft_credit'] = array(array( |
| 156 | 'contact_id' => $params['honor_contact_id'], |
| 157 | 'amount' => $params['total_amount'], |
| 158 | 'soft_credit_type_id' => CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'))); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Delete a contribution |
| 164 | * |
| 165 | * @param array $params (reference ) input parameters |
| 166 | * |
| 167 | * @return boolean true if success, else false |
| 168 | * @static void |
| 169 | * @access public |
| 170 | * {@getfields Contribution_delete} |
| 171 | * @example ContributionDelete.php |
| 172 | */ |
| 173 | function civicrm_api3_contribution_delete($params) { |
| 174 | |
| 175 | $contributionID = !empty($params['contribution_id']) ? $params['contribution_id'] : $params['id']; |
| 176 | if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) { |
| 177 | return civicrm_api3_create_success(array($contributionID => 1)); |
| 178 | } |
| 179 | else { |
| 180 | return civicrm_api3_create_error('Could not delete contribution'); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * modify metadata. Legacy support for contribution_id |
| 186 | */ |
| 187 | function _civicrm_api3_contribution_delete_spec(&$params) { |
| 188 | $params['id']['api.aliases'] = array('contribution_id'); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Retrieve a set of contributions, given a set of input params |
| 193 | * |
| 194 | * @param array $params (reference ) input parameters |
| 195 | * |
| 196 | * @internal param array $returnProperties Which properties should be included in the |
| 197 | * returned Contribution object. If NULL, the default |
| 198 | * set of properties will be included. |
| 199 | * |
| 200 | * @return array (reference ) array of contributions, if error an array with an error id and error message |
| 201 | * @static void |
| 202 | * @access public |
| 203 | * {@getfields Contribution_get} |
| 204 | * @example ContributionGet.php |
| 205 | */ |
| 206 | function civicrm_api3_contribution_get($params) { |
| 207 | |
| 208 | $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
| 209 | $entity = 'contribution'; |
| 210 | list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity); |
| 211 | |
| 212 | $contribution = array(); |
| 213 | while ($dao->fetch()) { |
| 214 | //CRM-8662 |
| 215 | $contribution_details = $query->store($dao); |
| 216 | $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dao->contribution_id , TRUE); |
| 217 | $contribution[$dao->contribution_id] = array_merge($contribution_details, $softContribution); |
| 218 | if(isset($contribution[$dao->contribution_id]['financial_type_id'])){ |
| 219 | $contribution[$dao->contribution_id]['financial_type_id'] = $contribution[$dao->contribution_id]['financial_type_id']; |
| 220 | } |
| 221 | // format soft credit for backward compatibility |
| 222 | _civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]); |
| 223 | } |
| 224 | return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * This function is used to format the soft credit for backward compatibility |
| 229 | * as of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key |
| 230 | * but we still return first soft credit as a part of contribution array |
| 231 | */ |
| 232 | function _civicrm_api3_format_soft_credit(&$contribution) { |
| 233 | if (!empty($contribution['soft_credit'])) { |
| 234 | $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id']; |
| 235 | $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id']; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Adjust Metadata for Get action |
| 241 | * |
| 242 | * The metadata is used for setting defaults, documentation & validation |
| 243 | * @param array $params array or parameters determined by getfields |
| 244 | */ |
| 245 | function _civicrm_api3_contribution_get_spec(&$params) { |
| 246 | $params['contribution_test']['api.default'] = 0; |
| 247 | $params['contribution_test']['title'] = 'Get Test Contributions?'; |
| 248 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); |
| 249 | $params['contact_id'] = $params['contribution_contact_id']; |
| 250 | $params['contact_id']['api.aliases'] = array('contribution_contact_id'); |
| 251 | unset($params['contribution_contact_id']); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * take the input parameter list as specified in the data model and |
| 256 | * convert it into the same format that we use in QF and BAO object |
| 257 | * |
| 258 | * @param array $params Associative array of property name/value |
| 259 | * pairs to insert in new contact. |
| 260 | * @param array $values The reformatted properties that we can use internally |
| 261 | * ' |
| 262 | * |
| 263 | * @param bool $create |
| 264 | * |
| 265 | * @return array|CRM_Error |
| 266 | * @access public |
| 267 | */ |
| 268 | function _civicrm_api3_contribute_format_params($params, &$values, $create = FALSE) { |
| 269 | //legacy way of formatting from v2 api - v3 way is to define metadata & do it in the api layer |
| 270 | _civicrm_api3_filter_fields_for_bao('Contribution', $params, $values); |
| 271 | return array(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Adjust Metadata for Transact action |
| 276 | * |
| 277 | * The metadata is used for setting defaults, documentation & validation |
| 278 | * @param array $params array or parameters determined by getfields |
| 279 | */ |
| 280 | function _civicrm_api3_contribution_transact_spec(&$params) { |
| 281 | $fields = civicrm_api3('contribution', 'getfields', array('action' => 'create')); |
| 282 | $params = array_merge($params, $fields['values']); |
| 283 | $params['receive_date']['api.default'] = 'now'; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Process a transaction and record it against the contact. |
| 288 | * |
| 289 | * @param array $params (reference ) input parameters |
| 290 | * |
| 291 | * @return array (reference ) contribution of created or updated record (or a civicrm error) |
| 292 | * @static void |
| 293 | * @access public |
| 294 | * |
| 295 | */ |
| 296 | function civicrm_api3_contribution_transact($params) { |
| 297 | // Set some params specific to payment processing |
| 298 | $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; |
| 299 | $params['amount'] = $params['total_amount']; |
| 300 | if (!isset($params['net_amount'])) { |
| 301 | $params['net_amount'] = $params['amount']; |
| 302 | } |
| 303 | if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { |
| 304 | $params['invoiceID'] = $params['invoice_id']; |
| 305 | } |
| 306 | |
| 307 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); |
| 308 | if (civicrm_error($paymentProcessor)) { |
| 309 | return $paymentProcessor; |
| 310 | } |
| 311 | |
| 312 | $payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); |
| 313 | if (civicrm_error($payment)) { |
| 314 | return $payment; |
| 315 | } |
| 316 | |
| 317 | $transaction = $payment->doDirectPayment($params); |
| 318 | if (civicrm_error($transaction)) { |
| 319 | return $transaction; |
| 320 | } |
| 321 | |
| 322 | // but actually, $payment->doDirectPayment() doesn't return a |
| 323 | // CRM_Core_Error by itself |
| 324 | if (is_object($transaction) && get_class($transaction) == 'CRM_Core_Error') { |
| 325 | $errs = $transaction->getErrors(); |
| 326 | if (!empty($errs)) { |
| 327 | $last_error = array_shift($errs); |
| 328 | return CRM_Core_Error::createApiError($last_error['message']); |
| 329 | } |
| 330 | } |
| 331 | $params['payment_instrument_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', $paymentProcessor['payment_processor_type_id'], 'payment_type') == 1 ? 'Credit Card' : 'Debit Card'; |
| 332 | return civicrm_api('contribution', 'create', $params); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Send a contribution confirmation (receipt or invoice) |
| 337 | * The appropriate online template will be used (the existence of related objects |
| 338 | * (e.g. memberships ) will affect this selection |
| 339 | * |
| 340 | * @param array $params input parameters |
| 341 | * {@getfields Contribution_sendconfirmation} |
| 342 | * |
| 343 | * @throws Exception |
| 344 | * @return array Api result array |
| 345 | * @static void |
| 346 | * @access public |
| 347 | */ |
| 348 | function civicrm_api3_contribution_sendconfirmation($params) { |
| 349 | $contribution = new CRM_Contribute_BAO_Contribution(); |
| 350 | $contribution->id = $params['id']; |
| 351 | if (! $contribution->find(TRUE)) { |
| 352 | throw new Exception('Contribution does not exist'); |
| 353 | } |
| 354 | $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']); |
| 355 | $contribution->loadRelatedObjects($input, $ids, FALSE, TRUE); |
| 356 | $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Adjust Metadata for sendconfirmation action |
| 361 | * |
| 362 | * The metadata is used for setting defaults, documentation & validation |
| 363 | * @param array $params array or parameters determined by getfields |
| 364 | */ |
| 365 | function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { |
| 366 | $params['id'] = array( |
| 367 | 'api.required' => 1, |
| 368 | 'title' => 'Contribution ID' |
| 369 | ); |
| 370 | $params['receipt_from_email'] = array( |
| 371 | 'api.required' =>1, |
| 372 | 'title' => 'From Email address (string) required until someone provides a patch :-)', |
| 373 | ); |
| 374 | $params['receipt_from_name'] = array( |
| 375 | 'title' => 'From Name (string)', |
| 376 | ); |
| 377 | $params['cc_receipt'] = array( |
| 378 | 'title' => 'CC Email address (string)', |
| 379 | ); |
| 380 | $params['bcc_receipt'] = array( |
| 381 | 'title' => 'BCC Email address (string)', |
| 382 | ); |
| 383 | $params['receipt_text'] = array( |
| 384 | 'title' => 'Message (string)', |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Complete an existing (pending) transaction, updating related entities (participant, membership, pledge etc) |
| 390 | * and taking any complete actions from the contribution page (e.g. send receipt) |
| 391 | * |
| 392 | * @todo - most of this should live in the BAO layer but as we want it to be an addition |
| 393 | * to 4.3 which is already stable we should add it to the api layer & re-factor into the BAO layer later |
| 394 | * |
| 395 | * @param array $params input parameters |
| 396 | * {@getfields Contribution_completetransaction} |
| 397 | * |
| 398 | * @throws API_Exception |
| 399 | * @return array Api result array |
| 400 | * @static void |
| 401 | * @access public |
| 402 | */ |
| 403 | function civicrm_api3_contribution_completetransaction(&$params) { |
| 404 | |
| 405 | $input = $ids = array(); |
| 406 | $contribution = new CRM_Contribute_BAO_Contribution(); |
| 407 | $contribution->id = $params['id']; |
| 408 | $contribution->find(TRUE); |
| 409 | if(!$contribution->id == $params['id']){ |
| 410 | throw new API_Exception('A valid contribution ID is required', 'invalid_data'); |
| 411 | } |
| 412 | try { |
| 413 | if(!$contribution->loadRelatedObjects($input, $ids, FALSE, TRUE)){ |
| 414 | throw new API_Exception('failed to load related objects'); |
| 415 | } |
| 416 | elseif ($contribution->contribution_status_id == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) { |
| 417 | throw new API_Exception(ts('Contribution already completed')); |
| 418 | } |
| 419 | $objects = $contribution->_relatedObjects; |
| 420 | $objects['contribution'] = &$contribution; |
| 421 | $input['component'] = $contribution->_component; |
| 422 | $input['is_test'] = $contribution->is_test; |
| 423 | $input['trxn_id']= !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id; |
| 424 | $input['amount'] = $contribution->total_amount; |
| 425 | if(isset($params['is_email_receipt'])){ |
| 426 | $input['is_email_receipt'] = $params['is_email_receipt']; |
| 427 | } |
| 428 | // @todo required for base ipn but problematic as api layer handles this |
| 429 | $transaction = new CRM_Core_Transaction(); |
| 430 | $ipn = new CRM_Core_Payment_BaseIPN(); |
| 431 | $ipn->completeTransaction($input, $ids, $objects, $transaction, !empty($contribution->contribution_recur_id)); |
| 432 | } |
| 433 | catch(Exception $e) { |
| 434 | throw new API_Exception('failed to load related objects' . $e->getMessage() . "\n" . $e->getTraceAsString()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * provide function metadata |
| 440 | * @param $params |
| 441 | */ |
| 442 | function _civicrm_api3_contribution_completetransaction_spec(&$params) { |
| 443 | $params['id'] = array( |
| 444 | 'title' => 'Contribution ID', |
| 445 | 'type' => CRM_Utils_Type::T_INT, |
| 446 | 'api.required' => TRUE, |
| 447 | ); |
| 448 | $params['trxn_id'] = array( |
| 449 | 'title' => 'Transaction ID', |
| 450 | 'type' => CRM_Utils_Type::T_STRING, |
| 451 | ); |
| 452 | $params['is_email_receipt'] = array( |
| 453 | 'title' => 'Send email Receipt?', |
| 454 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 455 | ); |
| 456 | } |