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