| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * Class CRM_Core_Payment_BaseIPN. |
| 30 | */ |
| 31 | class CRM_Core_Payment_BaseIPN { |
| 32 | |
| 33 | static $_now = NULL; |
| 34 | |
| 35 | /** |
| 36 | * Input parameters from payment processor. Store these so that |
| 37 | * the code does not need to keep retrieving from the http request |
| 38 | * @var array |
| 39 | */ |
| 40 | protected $_inputParameters = array(); |
| 41 | |
| 42 | /** |
| 43 | * Only used by AuthorizeNetIPN. |
| 44 | * |
| 45 | * @deprecated |
| 46 | * |
| 47 | * @var bool |
| 48 | */ |
| 49 | protected $_isRecurring = FALSE; |
| 50 | |
| 51 | /** |
| 52 | * Only used by AuthorizeNetIPN. |
| 53 | * |
| 54 | * @deprecated |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
| 58 | protected $_isFirstOrLastRecurringPayment = FALSE; |
| 59 | |
| 60 | /** |
| 61 | * Constructor. |
| 62 | */ |
| 63 | public function __construct() { |
| 64 | self::$_now = date('YmdHis'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Store input array on the class. |
| 69 | * |
| 70 | * @param array $parameters |
| 71 | * |
| 72 | * @throws CRM_Core_Exception |
| 73 | */ |
| 74 | public function setInputParameters($parameters) { |
| 75 | if (!is_array($parameters)) { |
| 76 | throw new CRM_Core_Exception('Invalid input parameters'); |
| 77 | } |
| 78 | $this->_inputParameters = $parameters; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Validate incoming data. |
| 83 | * |
| 84 | * This function is intended to ensure that incoming data matches |
| 85 | * It provides a form of pseudo-authentication - by checking the calling fn already knows |
| 86 | * the correct contact id & contribution id (this can be problematic when that has changed in |
| 87 | * the meantime for transactions that are delayed & contacts are merged in-between. e.g |
| 88 | * Paypal allows you to resend Instant Payment Notifications if you, for example, moved site |
| 89 | * and didn't update your IPN URL. |
| 90 | * |
| 91 | * @param array $input |
| 92 | * Interpreted values from the values returned through the IPN. |
| 93 | * @param array $ids |
| 94 | * More interpreted values (ids) from the values returned through the IPN. |
| 95 | * @param array $objects |
| 96 | * An empty array that will be populated with loaded object. |
| 97 | * @param bool $required |
| 98 | * Boolean Return FALSE if the relevant objects don't exist. |
| 99 | * @param int $paymentProcessorID |
| 100 | * Id of the payment processor ID in use. |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function validateData(&$input, &$ids, &$objects, $required = TRUE, $paymentProcessorID = NULL) { |
| 105 | |
| 106 | // make sure contact exists and is valid |
| 107 | $contact = new CRM_Contact_BAO_Contact(); |
| 108 | $contact->id = $ids['contact']; |
| 109 | if (!$contact->find(TRUE)) { |
| 110 | CRM_Core_Error::debug_log_message("Could not find contact record: {$ids['contact']} in IPN request: " . print_r($input, TRUE)); |
| 111 | echo "Failure: Could not find contact record: {$ids['contact']}<p>"; |
| 112 | return FALSE; |
| 113 | } |
| 114 | |
| 115 | // make sure contribution exists and is valid |
| 116 | $contribution = new CRM_Contribute_BAO_Contribution(); |
| 117 | $contribution->id = $ids['contribution']; |
| 118 | if (!$contribution->find(TRUE)) { |
| 119 | CRM_Core_Error::debug_log_message("Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE)); |
| 120 | echo "Failure: Could not find contribution record for {$contribution->id}<p>"; |
| 121 | return FALSE; |
| 122 | } |
| 123 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); |
| 124 | $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date); |
| 125 | |
| 126 | $objects['contact'] = &$contact; |
| 127 | $objects['contribution'] = &$contribution; |
| 128 | |
| 129 | // CRM-19478: handle oddity when p=null is set in place of contribution page ID, |
| 130 | if (!empty($ids['contributionPage']) && !is_numeric($ids['contributionPage'])) { |
| 131 | // We don't need to worry if about removing contribution page id as it will be set later in |
| 132 | // CRM_Contribute_BAO_Contribution::loadRelatedObjects(..) using $objects['contribution']->contribution_page_id |
| 133 | unset($ids['contributionPage']); |
| 134 | } |
| 135 | |
| 136 | if (!$this->loadObjects($input, $ids, $objects, $required, $paymentProcessorID)) { |
| 137 | return FALSE; |
| 138 | } |
| 139 | //the process is that the loadObjects is kind of hacked by loading the objects for the original contribution and then somewhat inconsistently using them for the |
| 140 | //current contribution. Here we ensure that the original contribution is available to the complete transaction function |
| 141 | //we don't want to fix this in the payment processor classes because we would have to fix all of them - so better to fix somewhere central |
| 142 | if (isset($objects['contributionRecur'])) { |
| 143 | $objects['first_contribution'] = $objects['contribution']; |
| 144 | } |
| 145 | return TRUE; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Load objects related to contribution. |
| 150 | * |
| 151 | * @input array information from Payment processor |
| 152 | * |
| 153 | * @param array $input |
| 154 | * @param array $ids |
| 155 | * @param array $objects |
| 156 | * @param bool $required |
| 157 | * @param int $paymentProcessorID |
| 158 | * @param array $error_handling |
| 159 | * |
| 160 | * @return bool|array |
| 161 | */ |
| 162 | public function loadObjects(&$input, &$ids, &$objects, $required, $paymentProcessorID, $error_handling = NULL) { |
| 163 | if (empty($error_handling)) { |
| 164 | // default options are that we log an error & echo it out |
| 165 | // note that we should refactor this error handling into error code @ some point |
| 166 | // but for now setting up enough separation so we can do unit tests |
| 167 | $error_handling = array( |
| 168 | 'log_error' => 1, |
| 169 | 'echo_error' => 1, |
| 170 | ); |
| 171 | } |
| 172 | $ids['paymentProcessor'] = $paymentProcessorID; |
| 173 | if (is_a($objects['contribution'], 'CRM_Contribute_BAO_Contribution')) { |
| 174 | $contribution = &$objects['contribution']; |
| 175 | } |
| 176 | else { |
| 177 | //legacy support - functions are 'used' to be able to pass in a DAO |
| 178 | $contribution = new CRM_Contribute_BAO_Contribution(); |
| 179 | $contribution->id = CRM_Utils_Array::value('contribution', $ids); |
| 180 | $contribution->find(TRUE); |
| 181 | $objects['contribution'] = &$contribution; |
| 182 | } |
| 183 | try { |
| 184 | $success = $contribution->loadRelatedObjects($input, $ids); |
| 185 | if ($required && empty($contribution->_relatedObjects['paymentProcessor'])) { |
| 186 | throw new CRM_Core_Exception("Could not find payment processor for contribution record: " . $contribution->id); |
| 187 | } |
| 188 | } |
| 189 | catch (Exception $e) { |
| 190 | $success = FALSE; |
| 191 | if (!empty($error_handling['log_error'])) { |
| 192 | CRM_Core_Error::debug_log_message($e->getMessage()); |
| 193 | } |
| 194 | if (!empty($error_handling['echo_error'])) { |
| 195 | echo $e->getMessage(); |
| 196 | } |
| 197 | if (!empty($error_handling['return_error'])) { |
| 198 | return array( |
| 199 | 'is_error' => 1, |
| 200 | 'error_message' => ($e->getMessage()), |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 | $objects = array_merge($objects, $contribution->_relatedObjects); |
| 205 | return $success; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Set contribution to failed. |
| 210 | * |
| 211 | * @param array $objects |
| 212 | * @param object $transaction |
| 213 | * @param array $input |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | public function failed(&$objects, &$transaction, $input = array()) { |
| 218 | $contribution = &$objects['contribution']; |
| 219 | $memberships = array(); |
| 220 | if (!empty($objects['membership'])) { |
| 221 | $memberships = &$objects['membership']; |
| 222 | if (is_numeric($memberships)) { |
| 223 | $memberships = array($objects['membership']); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | $addLineItems = FALSE; |
| 228 | if (empty($contribution->id)) { |
| 229 | $addLineItems = TRUE; |
| 230 | } |
| 231 | $participant = &$objects['participant']; |
| 232 | |
| 233 | // CRM-15546 |
| 234 | $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array( |
| 235 | 'labelColumn' => 'name', |
| 236 | 'flip' => 1, |
| 237 | )); |
| 238 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); |
| 239 | $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date); |
| 240 | $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date); |
| 241 | $contribution->contribution_status_id = $contributionStatuses['Failed']; |
| 242 | $contribution->save(); |
| 243 | |
| 244 | // Add line items for recurring payments. |
| 245 | if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id && $addLineItems) { |
| 246 | CRM_Contribute_BAO_ContributionRecur::addRecurLineItems($objects['contributionRecur']->id, $contribution); |
| 247 | } |
| 248 | |
| 249 | //add new soft credit against current contribution id and |
| 250 | //copy initial contribution custom fields for recurring contributions |
| 251 | if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) { |
| 252 | CRM_Contribute_BAO_ContributionRecur::addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id); |
| 253 | CRM_Contribute_BAO_ContributionRecur::copyCustomValues($objects['contributionRecur']->id, $contribution->id); |
| 254 | } |
| 255 | |
| 256 | if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) { |
| 257 | if (!empty($memberships)) { |
| 258 | // if transaction is failed then set "Cancelled" as membership status |
| 259 | $membershipStatuses = CRM_Core_PseudoConstant::get('CRM_Member_DAO_Membership', 'status_id', array( |
| 260 | 'labelColumn' => 'name', |
| 261 | 'flip' => 1, |
| 262 | )); |
| 263 | foreach ($memberships as $membership) { |
| 264 | if ($membership) { |
| 265 | $membership->status_id = $membershipStatuses['Cancelled']; |
| 266 | $membership->save(); |
| 267 | |
| 268 | //update related Memberships. |
| 269 | $params = array('status_id' => $membershipStatuses['Cancelled']); |
| 270 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if ($participant) { |
| 276 | $participantStatuses = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'status_id', array( |
| 277 | 'labelColumn' => 'name', |
| 278 | 'flip' => 1, |
| 279 | )); |
| 280 | $participant->status_id = $participantStatuses['Cancelled']; |
| 281 | $participant->save(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | $transaction->commit(); |
| 286 | CRM_Core_Error::debug_log_message("Setting contribution status to failed"); |
| 287 | //echo "Success: Setting contribution status to failed<p>"; |
| 288 | return TRUE; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Handled pending contribution status. |
| 293 | * |
| 294 | * @param array $objects |
| 295 | * @param object $transaction |
| 296 | * |
| 297 | * @return bool |
| 298 | */ |
| 299 | public function pending(&$objects, &$transaction) { |
| 300 | $transaction->commit(); |
| 301 | CRM_Core_Error::debug_log_message("returning since contribution status is pending"); |
| 302 | echo "Success: Returning since contribution status is pending<p>"; |
| 303 | return TRUE; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Process cancelled payment outcome. |
| 308 | * |
| 309 | * @param array $objects |
| 310 | * @param CRM_Core_Transaction $transaction |
| 311 | * @param array $input |
| 312 | * |
| 313 | * @return bool |
| 314 | */ |
| 315 | public function cancelled(&$objects, &$transaction, $input = array()) { |
| 316 | $contribution = &$objects['contribution']; |
| 317 | $memberships = &$objects['membership']; |
| 318 | if (is_numeric($memberships)) { |
| 319 | $memberships = array($objects['membership']); |
| 320 | } |
| 321 | |
| 322 | $participant = &$objects['participant']; |
| 323 | $addLineItems = FALSE; |
| 324 | if (empty($contribution->id)) { |
| 325 | $addLineItems = TRUE; |
| 326 | } |
| 327 | $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array( |
| 328 | 'labelColumn' => 'name', |
| 329 | 'flip' => 1, |
| 330 | )); |
| 331 | $contribution->contribution_status_id = $contributionStatuses['Cancelled']; |
| 332 | $contribution->cancel_date = self::$_now; |
| 333 | $contribution->cancel_reason = CRM_Utils_Array::value('reasonCode', $input); |
| 334 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); |
| 335 | $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date); |
| 336 | $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date); |
| 337 | $contribution->save(); |
| 338 | |
| 339 | //add lineitems for recurring payments |
| 340 | if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id && $addLineItems) { |
| 341 | CRM_Contribute_BAO_ContributionRecur::addRecurLineItems($objects['contributionRecur']->id, $contribution); |
| 342 | } |
| 343 | |
| 344 | //add new soft credit against current $contribution and |
| 345 | //copy initial contribution custom fields for recurring contributions |
| 346 | if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) { |
| 347 | CRM_Contribute_BAO_ContributionRecur::addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id); |
| 348 | CRM_Contribute_BAO_ContributionRecur::copyCustomValues($objects['contributionRecur']->id, $contribution->id); |
| 349 | } |
| 350 | |
| 351 | if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) { |
| 352 | if (!empty($memberships)) { |
| 353 | $membershipStatuses = CRM_Core_PseudoConstant::get('CRM_Member_DAO_Membership', 'status_id', array( |
| 354 | 'labelColumn' => 'name', |
| 355 | 'flip' => 1, |
| 356 | )); |
| 357 | // Cancel only Pending memberships |
| 358 | // CRM-18688 |
| 359 | $pendingStatusId = $membershipStatuses['Pending']; |
| 360 | foreach ($memberships as $membership) { |
| 361 | if ($membership && ($membership->status_id == $pendingStatusId)) { |
| 362 | $membership->status_id = $membershipStatuses['Cancelled']; |
| 363 | $membership->save(); |
| 364 | |
| 365 | //update related Memberships. |
| 366 | $params = array('status_id' => $membershipStatuses['Cancelled']); |
| 367 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if ($participant) { |
| 373 | $participantStatuses = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'status_id', array( |
| 374 | 'labelColumn' => 'name', |
| 375 | 'flip' => 1, |
| 376 | )); |
| 377 | $participant->status_id = $participantStatuses['Cancelled']; |
| 378 | $participant->save(); |
| 379 | } |
| 380 | } |
| 381 | $transaction->commit(); |
| 382 | CRM_Core_Error::debug_log_message("Setting contribution status to cancelled"); |
| 383 | //echo "Success: Setting contribution status to cancelled<p>"; |
| 384 | return TRUE; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Rollback unhandled outcomes. |
| 389 | * |
| 390 | * @param array $objects |
| 391 | * @param CRM_Core_Transaction $transaction |
| 392 | * |
| 393 | * @return bool |
| 394 | */ |
| 395 | public function unhandled(&$objects, &$transaction) { |
| 396 | $transaction->rollback(); |
| 397 | CRM_Core_Error::debug_log_message("returning since contribution status: is not handled"); |
| 398 | echo "Failure: contribution status is not handled<p>"; |
| 399 | return FALSE; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @deprecated |
| 404 | * |
| 405 | * Jumbled up function. |
| 406 | * |
| 407 | * The purpose of this function is to transition a pending transaction to Completed including updating any |
| 408 | * related entities. |
| 409 | * |
| 410 | * It has been overloaded to also add recurring transactions to the database, cloning the original transaction and |
| 411 | * updating related entities. |
| 412 | * |
| 413 | * It is recommended to avoid calling this function directly and call the api functions: |
| 414 | * - contribution.completetransaction |
| 415 | * - contribution.repeattransaction |
| 416 | * |
| 417 | * These functions are the focus of testing efforts and more accurately reflect the division of roles |
| 418 | * (the job of the IPN class is to determine the outcome, transaction id, invoice id & to validate the source |
| 419 | * and from there it should be possible to pass off transaction management.) |
| 420 | * |
| 421 | * This function has been problematic for some time but there are now several tests via the api_v3_Contribution test |
| 422 | * and the Paypal & Authorize.net IPN tests so any refactoring should be done in conjunction with those. |
| 423 | * |
| 424 | * This function needs to have the 'body' moved to the CRM_Contribute_BAO_Contribute class and to undergo |
| 425 | * refactoring to separate the complete transaction and repeat transaction functionality into separate functions with |
| 426 | * a shared function that updates related components. |
| 427 | * |
| 428 | * Note that it is not necessary payment processor extension to implement an IPN class now. In general the code on the |
| 429 | * IPN class is better accessed through the api which de-jumbles it a bit. |
| 430 | * |
| 431 | * e.g the payment class can have a function like (based on Omnipay extension): |
| 432 | * |
| 433 | * public function handlePaymentNotification() { |
| 434 | * $response = $this->getValidatedOutcome(); |
| 435 | * if ($response->isSuccessful()) { |
| 436 | * try { |
| 437 | * // @todo check if it is a repeat transaction & call repeattransaction instead. |
| 438 | * civicrm_api3('contribution', 'completetransaction', array('id' => $this->transaction_id)); |
| 439 | * } |
| 440 | * catch (CiviCRM_API3_Exception $e) { |
| 441 | * if (!stristr($e->getMessage(), 'Contribution already completed')) { |
| 442 | * $this->handleError('error', $this->transaction_id . $e->getMessage(), 'ipn_completion', 9000, 'An error may |
| 443 | * have occurred. Please check your receipt is correct'); |
| 444 | * $this->redirectOrExit('success'); |
| 445 | * } |
| 446 | * elseif ($this->transaction_id) { |
| 447 | * civicrm_api3('contribution', 'create', array('id' => $this->transaction_id, 'contribution_status_id' => |
| 448 | * 'Failed')); |
| 449 | * } |
| 450 | * |
| 451 | * @param array $input |
| 452 | * @param array $ids |
| 453 | * @param array $objects |
| 454 | * @param CRM_Core_Transaction $transaction |
| 455 | * @param bool $recur |
| 456 | */ |
| 457 | public function completeTransaction(&$input, &$ids, &$objects, &$transaction, $recur = FALSE) { |
| 458 | $contribution = &$objects['contribution']; |
| 459 | |
| 460 | CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $transaction, $recur, $contribution); |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Get site billing ID. |
| 465 | * |
| 466 | * @param array $ids |
| 467 | * |
| 468 | * @return bool |
| 469 | */ |
| 470 | public function getBillingID(&$ids) { |
| 471 | $ids['billing'] = CRM_Core_BAO_LocationType::getBilling(); |
| 472 | if (!$ids['billing']) { |
| 473 | CRM_Core_Error::debug_log_message(ts('Please set a location type of %1', array(1 => 'Billing'))); |
| 474 | echo "Failure: Could not find billing location type<p>"; |
| 475 | return FALSE; |
| 476 | } |
| 477 | return TRUE; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @deprecated |
| 482 | * |
| 483 | * @todo confirm this function is not being used by any payment processor outside core & remove. |
| 484 | * |
| 485 | * Note that the compose message part has been moved to contribution |
| 486 | * In general LoadObjects is called first to get the objects but the composeMessageArray function now calls it |
| 487 | * |
| 488 | * @param array $input |
| 489 | * Incoming data from Payment processor. |
| 490 | * @param array $ids |
| 491 | * Related object IDs. |
| 492 | * @param array $objects |
| 493 | * @param array $values |
| 494 | * Values related to objects that have already been loaded. |
| 495 | * @param bool $recur |
| 496 | * Is it part of a recurring contribution. |
| 497 | * @param bool $returnMessageText |
| 498 | * Should text be returned instead of sent. This. |
| 499 | * is because the function is also used to generate pdfs |
| 500 | * |
| 501 | * @return array |
| 502 | */ |
| 503 | public function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) { |
| 504 | return CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, |
| 505 | $returnMessageText); |
| 506 | } |
| 507 | |
| 508 | } |