Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Core / Payment / BaseIPN.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_Payment_BaseIPN {
36
37 static $_now = NULL;
8196c759 38
c8aa607b 39 /**
40 * Input parameters from payment processor. Store these so that
41 * the code does not need to keep retrieving from the http request
42 * @var array
43 */
44 protected $_inputParameters = array();
45
937cf542
EM
46 protected $_isRecurring = FALSE;
47
48 protected $_isFirstOrLastRecurringPayment = FALSE;
8196c759 49 /**
50 * Constructor
51 */
6a488035
TO
52 function __construct() {
53 self::$_now = date('YmdHis');
54 }
55
c8aa607b 56 /**
57 * Store input array on the class
77b97be7 58 *
c8aa607b 59 * @param array $parameters
77b97be7
EM
60 *
61 * @throws CRM_Core_Exception
c8aa607b 62 */
63 function setInputParameters($parameters) {
64 if(!is_array($parameters)) {
cc0c30cc 65 throw new CRM_Core_Exception('Invalid input parameters');
c8aa607b 66 }
67 $this->_inputParameters = $parameters;
68 }
8196c759 69 /**
70 * Validate incoming data. This function is intended to ensure that incoming data matches
71 * It provides a form of pseudo-authentication - by checking the calling fn already knows
72 * the correct contact id & contribution id (this can be problematic when that has changed in
73 * the meantime for transactions that are delayed & contacts are merged in-between. e.g
74 * Paypal allows you to resend Instant Payment Notifications if you, for example, moved site
75 * and didn't update your IPN URL.
76 *
77 * @param array $input interpreted values from the values returned through the IPN
78 * @param array $ids more interpreted values (ids) from the values returned through the IPN
79 * @param array $objects an empty array that will be populated with loaded object
80 * @param boolean $required boolean Return FALSE if the relevant objects don't exist
81 * @param integer $paymentProcessorID Id of the payment processor ID in use
82 * @return boolean
83 */
6a488035
TO
84 function validateData(&$input, &$ids, &$objects, $required = TRUE, $paymentProcessorID = NULL) {
85
86 // make sure contact exists and is valid
5a9c68ac 87 $contact = new CRM_Contact_BAO_Contact();
6a488035
TO
88 $contact->id = $ids['contact'];
89 if (!$contact->find(TRUE)) {
90 CRM_Core_Error::debug_log_message("Could not find contact record: {$ids['contact']} in IPN request: ".print_r($input, TRUE));
91 echo "Failure: Could not find contact record: {$ids['contact']}<p>";
92 return FALSE;
93 }
94
95 // make sure contribution exists and is valid
5a9c68ac 96 $contribution = new CRM_Contribute_BAO_Contribution();
6a488035
TO
97 $contribution->id = $ids['contribution'];
98 if (!$contribution->find(TRUE)) {
99 CRM_Core_Error::debug_log_message("Could not find contribution record: {$contribution->id} in IPN request: ".print_r($input, TRUE));
100 echo "Failure: Could not find contribution record for {$contribution->id}<p>";
101 return FALSE;
102 }
103 $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date);
104
105 $objects['contact'] = &$contact;
106 $objects['contribution'] = &$contribution;
107 if (!$this->loadObjects($input, $ids, $objects, $required, $paymentProcessorID)) {
108 return FALSE;
109 }
a284891b
EM
110 //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
111 //current contribution. Here we ensure that the original contribution is available to the complete transaction function
112 //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
113 if (isset($objects['contributionRecur'])) {
114 $objects['first_contribution'] = $objects['contribution'];
115 }
6a488035
TO
116 return TRUE;
117 }
118
8196c759 119 /**
6a488035
TO
120 * Load objects related to contribution
121 *
122 * @input array information from Payment processor
dd244018
EM
123 *
124 * @param $input
8196c759 125 * @param array $ids
126 * @param array $objects
127 * @param boolean $required
128 * @param integer $paymentProcessorID
129 * @param array $error_handling
dd244018 130 *
8196c759 131 * @return multitype:number NULL |boolean
6a488035
TO
132 */
133 function loadObjects(&$input, &$ids, &$objects, $required, $paymentProcessorID, $error_handling = NULL) {
134 if (empty($error_handling)) {
135 // default options are that we log an error & echo it out
136 // note that we should refactor this error handling into error code @ some point
137 // but for now setting up enough separation so we can do unit tests
138 $error_handling = array(
139 'log_error' => 1,
140 'echo_error' => 1,
141 );
142 }
143 $ids['paymentProcessor'] = $paymentProcessorID;
144 if (is_a($objects['contribution'], 'CRM_Contribute_BAO_Contribution')) {
145 $contribution = &$objects['contribution'];
146 }
147 else {
148 //legacy support - functions are 'used' to be able to pass in a DAO
149 $contribution = new CRM_Contribute_BAO_Contribution();
150 $contribution->id = CRM_Utils_Array::value('contribution', $ids);
151 $contribution->find(TRUE);
152 $objects['contribution'] = &$contribution;
153 }
154 try {
155 $success = $contribution->loadRelatedObjects($input, $ids, $required);
156 }
c8aa607b 157 catch(Exception $e) {
cc0c30cc 158 $success = FALSE;
a7488080 159 if (!empty($error_handling['log_error'])) {
6a488035
TO
160 CRM_Core_Error::debug_log_message($e->getMessage());
161 }
a7488080 162 if (!empty($error_handling['echo_error'])) {
6a488035
TO
163 echo ($e->getMessage());
164 }
a7488080 165 if (!empty($error_handling['return_error'])) {
6a488035
TO
166 return array(
167 'is_error' => 1,
168 'error_message' => ($e->getMessage()),
169 );
170 }
171 }
172 $objects = array_merge($objects, $contribution->_relatedObjects);
173 return $success;
174 }
175
8196c759 176 /**
177 * Set contribution to failed
178 * @param array $objects
179 * @param object $transaction
180 * @param array $input
181 * @return boolean
182 */
6a488035
TO
183 function failed(&$objects, &$transaction, $input = array()) {
184 $contribution = &$objects['contribution'];
185 $memberships = array();
a7488080 186 if (!empty($objects['membership'])) {
6a488035
TO
187 $memberships = &$objects['membership'];
188 if (is_numeric($memberships)) {
189 $memberships = array($objects['membership']);
190 }
191 }
192
193 $addLineItems = FALSE;
194 if (empty($contribution->id)) {
195 $addLineItems = TRUE;
196 }
197 $participant = &$objects['participant'];
198
199 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
5a9c68ac
PJ
200 $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date);
201 $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date);
202 $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date);
6a488035
TO
203 $contribution->contribution_status_id = array_search('Failed', $contributionStatus);
204 $contribution->save();
205
206 //add lineitems for recurring payments
a7488080 207 if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id && $addLineItems) {
34a100a7 208 $this->addRecurLineItems($objects['contributionRecur']->id, $contribution);
6a488035
TO
209 }
210
8381af80 211 //add new soft credit against current contribution id and
6357981e 212 //copy initial contribution custom fields for recurring contributions
a7488080 213 if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) {
8381af80 214 $this->addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id);
6357981e
PJ
215 $this->copyCustomValues($objects['contributionRecur']->id, $contribution->id);
216 }
217
a7488080 218 if (empty($input['skipComponentSync'])) {
6a488035 219 if (!empty($memberships)) {
5968aa47
C
220 // if transaction is failed then set "Cancelled" as membership status
221 $cancelStatusId = array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus());
6a488035
TO
222 foreach ($memberships as $membership) {
223 if ($membership) {
5968aa47 224 $membership->status_id = $cancelStatusId;
6a488035 225 $membership->save();
d63f4fc3 226
6a488035 227 //update related Memberships.
5968aa47 228 $params = array('status_id' => $cancelStatusId);
6a488035
TO
229 CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params);
230 }
231 }
232 }
d63f4fc3 233
6a488035
TO
234 if ($participant) {
235 $participant->status_id = 4;
236 $participant->save();
237 }
238 }
239
240 $transaction->commit();
241 CRM_Core_Error::debug_log_message("Setting contribution status to failed");
242 //echo "Success: Setting contribution status to failed<p>";
243 return TRUE;
244 }
245
8196c759 246 /**
247 * Handled pending contribution status
248 * @param array $objects
249 * @param object $transaction
250 * @return boolean
251 */
6a488035
TO
252 function pending(&$objects, &$transaction) {
253 $transaction->commit();
254 CRM_Core_Error::debug_log_message("returning since contribution status is pending");
255 echo "Success: Returning since contribution status is pending<p>";
256 return TRUE;
257 }
258
6c786a9b
EM
259 /**
260 * @param $objects
261 * @param $transaction
262 * @param array $input
263 *
264 * @return bool
265 */
6a488035
TO
266 function cancelled(&$objects, &$transaction, $input = array()) {
267 $contribution = &$objects['contribution'];
268 $memberships = &$objects['membership'];
269 if (is_numeric($memberships)) {
270 $memberships = array($objects['membership']);
271 }
272
273 $participant = &$objects['participant'];
274 $addLineItems = FALSE;
275 if (empty($contribution->id)) {
276 $addLineItems = TRUE;
277 }
278 $contribution->contribution_status_id = 3;
279 $contribution->cancel_date = self::$_now;
280 $contribution->cancel_reason = CRM_Utils_Array::value('reasonCode', $input);
281 $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date);
282 $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date);
283 $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date);
284 $contribution->save();
285
286 //add lineitems for recurring payments
a7488080 287 if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id && $addLineItems) {
34a100a7 288 $this->addRecurLineItems($objects['contributionRecur']->id, $contribution);
6a488035
TO
289 }
290
8381af80 291 //add new soft credit against current $contribution and
6357981e 292 //copy initial contribution custom fields for recurring contributions
a7488080 293 if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) {
8381af80 294 $this->addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id);
6357981e
PJ
295 $this->copyCustomValues($objects['contributionRecur']->id, $contribution->id);
296 }
297
a7488080 298 if (empty($input['skipComponentSync'])) {
6a488035
TO
299 if (!empty($memberships)) {
300 foreach ($memberships as $membership) {
301 if ($membership) {
302 $membership->status_id = 6;
303 $membership->save();
d63f4fc3 304
6a488035
TO
305 //update related Memberships.
306 $params = array('status_id' => 6);
307 CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params);
308 }
309 }
310 }
d63f4fc3 311
6a488035
TO
312 if ($participant) {
313 $participant->status_id = 4;
314 $participant->save();
315 }
316 }
317 $transaction->commit();
318 CRM_Core_Error::debug_log_message("Setting contribution status to cancelled");
319 //echo "Success: Setting contribution status to cancelled<p>";
320 return TRUE;
321 }
322
6c786a9b
EM
323 /**
324 * @param $objects
325 * @param $transaction
326 *
327 * @return bool
328 */
6a488035
TO
329 function unhandled(&$objects, &$transaction) {
330 $transaction->rollback();
2d8851f6
EM
331 CRM_Core_Error::debug_log_message("returning since contribution status: is not handled");
332 echo "Failure: contribution status is not handled<p>";
6a488035
TO
333 return FALSE;
334 }
335
6c786a9b
EM
336 /**
337 * @param $input
338 * @param $ids
339 * @param $objects
340 * @param $transaction
341 * @param bool $recur
342 */
6a488035
TO
343 function completeTransaction(&$input, &$ids, &$objects, &$transaction, $recur = FALSE) {
344 $contribution = &$objects['contribution'];
a284891b
EM
345
346 $primaryContributionID = isset($contribution->id) ? $contribution->id : $objects['first_contribution']->id;
347
6a488035
TO
348 $memberships = &$objects['membership'];
349 if (is_numeric($memberships)) {
350 $memberships = array($objects['membership']);
351 }
352 $participant = &$objects['participant'];
353 $event = &$objects['event'];
354 $changeToday = CRM_Utils_Array::value('trxn_date', $input, self::$_now);
355 $recurContrib = &$objects['contributionRecur'];
356
357 $values = array();
358 $source = NULL;
359 if ($input['component'] == 'contribute') {
360 if ($contribution->contribution_page_id) {
361 CRM_Contribute_BAO_ContributionPage::setValues($contribution->contribution_page_id, $values);
362 $source = ts('Online Contribution') . ': ' . $values['title'];
363 }
364 elseif ($recurContrib && $recurContrib->id) {
365 $contribution->contribution_page_id = NULL;
366 $values['amount'] = $recurContrib->amount;
367 $values['financial_type_id'] = $objects['contributionType']->id;
368 $values['title'] = $source = ts('Offline Recurring Contribution');
6a488035
TO
369 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
370 $values['receipt_from_name'] = $domainValues[0];
371 $values['receipt_from_email'] = $domainValues[1];
372 }
ef3a8cf0 373 if($recurContrib && $recurContrib->id){
2b5b0279 374 //CRM-13273 - is_email_receipt setting on recurring contribution should take precedence over contribution page setting
375 $values['is_email_receipt'] = $recurContrib->is_email_receipt;
376 }
6a488035
TO
377
378 $contribution->source = $source;
a7488080 379 if (!empty($values['is_email_receipt'])) {
6a488035
TO
380 $contribution->receipt_date = self::$_now;
381 }
382
383 if (!empty($memberships)) {
384 $membershipsUpdate = array( );
385 foreach ($memberships as $membershipTypeIdKey => $membership) {
386 if ($membership) {
387 $format = '%Y%m%d';
388
389 $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membership->contact_id,
390 $membership->membership_type_id,
391 $membership->is_test, $membership->id
392 );
393
394 // CRM-8141 update the membership type with the value recorded in log when membership created/renewed
395 // this picks up membership type changes during renewals
396 $sql = "
397SELECT membership_type_id
398FROM civicrm_membership_log
399WHERE membership_id=$membership->id
400ORDER BY id DESC
401LIMIT 1;";
402 $dao = new CRM_Core_DAO;
403 $dao->query($sql);
404 if ($dao->fetch()) {
405 if (!empty($dao->membership_type_id)) {
406 $membership->membership_type_id = $dao->membership_type_id;
407 $membership->save();
408 }
409 // else fall back to using current membership type
410 }
411 // else fall back to using current membership type
412 $dao->free();
413
a284891b 414 $num_terms = $contribution->getNumTermsByContributionAndMembershipType($membership->membership_type_id, $primaryContributionID);
6a488035
TO
415 if ($currentMembership) {
416 /*
417 * Fixed FOR CRM-4433
418 * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status
419 * when Contribution mode is notify and membership is for renewal )
420 */
421 CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
422
2243fe93
EM
423 // @todo - we should pass membership_type_id instead of null here but not
424 // adding as not sure of testing
6a488035 425 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id,
2243fe93 426 $changeToday, NULL, $num_terms
6a488035 427 );
2243fe93 428
6a488035
TO
429 $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
430 }
431 else {
29347f3d 432 $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->membership_type_id, NULL, NULL, NULL, $num_terms);
6a488035
TO
433 }
434
435 //get the status for membership.
436 $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'],
437 $dates['end_date'],
438 $dates['join_date'],
439 'today',
5f11bbcc
EM
440 TRUE,
441 $membership->membership_type_id,
442 (array) $membership
6a488035
TO
443 );
444
445 $formatedParams = array('status_id' => CRM_Utils_Array::value('id', $calcStatus, 2),
446 'join_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $dates), $format),
447 'start_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $dates), $format),
448 'end_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $dates), $format),
449 );
450 //we might be renewing membership,
451 //so make status override false.
452 $formatedParams['is_override'] = FALSE;
453 $membership->copyValues($formatedParams);
454 $membership->save();
455
456 //updating the membership log
457 $membershipLog = array();
458 $membershipLog = $formatedParams;
459
460 $logStartDate = $formatedParams['start_date'];
a7488080 461 if (!empty($dates['log_start_date'])) {
6a488035
TO
462 $logStartDate = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
463 $logStartDate = CRM_Utils_Date::isoToMysql($logStartDate);
464 }
465
466 $membershipLog['start_date'] = $logStartDate;
467 $membershipLog['membership_id'] = $membership->id;
468 $membershipLog['modified_id'] = $membership->contact_id;
469 $membershipLog['modified_date'] = date('Ymd');
470 $membershipLog['membership_type_id'] = $membership->membership_type_id;
471
472 CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray);
473
474 //update related Memberships.
475 CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formatedParams);
d63f4fc3 476
6a488035
TO
477 //update the membership type key of membership relatedObjects array
478 //if it has changed after membership update
479 if ($membershipTypeIdKey != $membership->membership_type_id) {
480 $membershipsUpdate[$membership->membership_type_id] = $membership;
481 $contribution->_relatedObjects['membership'][$membership->membership_type_id] = $membership;
482 unset($contribution->_relatedObjects['membership'][$membershipTypeIdKey]);
483 unset($memberships[$membershipTypeIdKey]);
484 }
485 }
486 }
487 //update the memberships object with updated membershipTypeId data
488 //if membershipTypeId has changed after membership update
489 if (!empty($membershipsUpdate)) {
490 $memberships = $memberships + $membershipsUpdate;
491 }
492 }
493 }
494 else {
495 // event
496 $eventParams = array('id' => $objects['event']->id);
497 $values['event'] = array();
498
499 CRM_Event_BAO_Event::retrieve($eventParams, $values['event']);
500
501 //get location details
502 $locationParams = array('entity_id' => $objects['event']->id, 'entity_table' => 'civicrm_event');
503 $values['location'] = CRM_Core_BAO_Location::getValues($locationParams);
504
505 $ufJoinParams = array(
506 'entity_table' => 'civicrm_event',
507 'entity_id' => $ids['event'],
508 'module' => 'CiviEvent',
509 );
d63f4fc3 510
6a488035
TO
511 list($custom_pre_id,
512 $custom_post_ids
513 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
d63f4fc3 514
6a488035
TO
515 $values['custom_pre_id'] = $custom_pre_id;
516 $values['custom_post_id'] = $custom_post_ids;
e1eb31b5
RN
517 //for tasks 'Change Participant Status' and 'Batch Update Participants Via Profile' case
518 //and cases involving status updation through ipn
519 $values['totalAmount'] = $input['amount'];
6a488035
TO
520
521 $contribution->source = ts('Online Event Registration') . ': ' . $values['event']['title'];
522
523 if ($values['event']['is_email_confirm']) {
524 $contribution->receipt_date = self::$_now;
525 $values['is_email_receipt'] = 1;
526 }
a7488080 527 if (empty($input['skipComponentSync'])) {
6a488035
TO
528 $participant->status_id = 1;
529 }
530 $participant->save();
531 }
532
533 if (CRM_Utils_Array::value('net_amount', $input, 0) == 0 &&
534 CRM_Utils_Array::value('fee_amount', $input, 0) != 0
535 ) {
536 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
537 }
538 $addLineItems = FALSE;
539 if (empty($contribution->id)) {
540 $addLineItems = TRUE;
541 }
d63f4fc3 542
6a488035
TO
543 $contribution->contribution_status_id = 1;
544 $contribution->is_test = $input['is_test'];
545 $contribution->fee_amount = CRM_Utils_Array::value('fee_amount', $input, 0);
546 $contribution->net_amount = CRM_Utils_Array::value('net_amount', $input, 0);
547 $contribution->trxn_id = $input['trxn_id'];
548 $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date);
549 $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date);
46fa5206 550 $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date);
6a488035
TO
551 $contribution->cancel_date = 'null';
552
a7488080 553 if (!empty($input['check_number'])) {
6a488035
TO
554 $contribution->check_number = $input['check_number'];
555 }
556
a7488080 557 if (!empty($input['payment_instrument_id'])) {
6a488035
TO
558 $contribution->payment_instrument_id = $input['payment_instrument_id'];
559 }
d63f4fc3 560
6a488035
TO
561 if ($contribution->id) {
562 $contributionId['id'] = $contribution->id;
563 $input['prevContribution'] = CRM_Contribute_BAO_Contribution::getValues($contributionId, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray);
564 }
565 $contribution->save();
566
8381af80 567 //add new soft credit against current $contribution and
568 if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id) {
569 $this->addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id);
570 }
571
6a488035 572 //add lineitems for recurring payments
34a100a7
EM
573 if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) {
574 if ($addLineItems) {
575 $input ['line_item'] = $this->addRecurLineItems($objects['contributionRecur']->id, $contribution);
576 }
577 else {
578 // this is just to prevent e-notices when we call recordFinancialAccounts - per comments on that line - intention is somewhat unclear
579 $input['line_item'] = array();
580 }
6a488035
TO
581 }
582
6357981e
PJ
583 //copy initial contribution custom fields for recurring contributions
584 if ($recurContrib && $recurContrib->id) {
585 $this->copyCustomValues($recurContrib->id, $contribution->id);
586 }
587
6a488035
TO
588 // next create the transaction record
589 $paymentProcessor = $paymentProcessorId = '';
590 if (isset($objects['paymentProcessor'])) {
591 if (is_array($objects['paymentProcessor'])) {
592 $paymentProcessor = $objects['paymentProcessor']['payment_processor_type'];
593 $paymentProcessorId = $objects['paymentProcessor']['id'];
594 }
595 else {
596 $paymentProcessor = $objects['paymentProcessor']->payment_processor_type;
597 $paymentProcessorId = $objects['paymentProcessor']->id;
598 }
599 }
4d34aefa 600 //it's hard to see how it could reach this point without a contributon id as it is saved in line 511 above
601 // which raised the question as to whether this check preceded line 511 & if so whether something could be broken
602 // From a lot of code reading /debugging I'm still not sure the intent WRT first & subsequent payments in this code
603 // it would be good if someone added some comments or refactored this
6a488035
TO
604 if ($contribution->id) {
605 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
cc757ab9 606 if ((empty($input['prevContribution']) && $paymentProcessorId) || (!$input['prevContribution']->is_pay_later &&
607- $input['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatuses))) {
608 $input['payment_processor'] = $paymentProcessorId;
6a488035 609 }
78b79549 610 $input['contribution_status_id'] = array_search('Completed', $contributionStatuses);
6a488035
TO
611 $input['total_amount'] = $input['amount'];
612 $input['contribution'] = $contribution;
a1f5ffcc 613 $input['financial_type_id'] = $contribution->financial_type_id;
614
a7488080 615 if (!empty($contribution->_relatedObjects['participant'])) {
6a488035
TO
616 $input['contribution_mode'] = 'participant';
617 $input['participant_id'] = $contribution->_relatedObjects['participant']->id;
0ba0addf 618 $input['skipLineItem'] = 1;
f92fd420 619 }
d2035566
PN
620 elseif (!empty($contribution->_relatedObjects['membership'])) {
621 $input['skipLineItem'] = TRUE;
622 $input['contribution_mode'] = 'membership';
623 }
4d34aefa 624 //@todo writing a unit test I was unable to create a scenario where this line did not fatal on second
34a100a7 625 // and subsequent payments. In this case the line items are created at $this->addRecurLineItems
4d34aefa 626 // and since the contribution is saved prior to this line there is always a contribution-id,
627 // however there is never a prevContribution (which appears to mean original contribution not previous
628 // contribution - or preUpdateContributionObject most accurately)
629 // so, this is always called & only appears to succeed when prevContribution exists - which appears
630 // to mean "are we updating an exisitng pending contribution"
631 //I was able to make the unit test complete as fataling here doesn't prevent
632 // the contribution being created - but activities would not be created or emails sent
34a100a7 633
6a488035
TO
634 CRM_Contribute_BAO_Contribution::recordFinancialAccounts($input, NULL);
635 }
636
637 self::updateRecurLinkedPledge($contribution);
638
639 // create an activity record
640 if ($input['component'] == 'contribute') {
641 //CRM-4027
642 $targetContactID = NULL;
a7488080 643 if (!empty($ids['related_contact'])) {
6a488035
TO
644 $targetContactID = $contribution->contact_id;
645 $contribution->contact_id = $ids['related_contact'];
646 }
647 CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID);
648 // event
649 }
650 else {
651 CRM_Activity_BAO_Activity::addActivity($participant);
652 }
653
654 CRM_Core_Error::debug_log_message("Contribution record updated successfully");
655 $transaction->commit();
656
657 // CRM-9132 legacy behaviour was that receipts were sent out in all instances. Still sending
658 // when array_key 'is_email_receipt doesn't exist in case some instances where is needs setting haven't been set
659 if (!array_key_exists('is_email_receipt', $values) ||
660 $values['is_email_receipt'] == 1
661 ) {
662 self::sendMail($input, $ids, $objects, $values, $recur, FALSE);
bf014492 663 CRM_Core_Error::debug_log_message("Receipt sent");
6a488035
TO
664 }
665
bf014492 666 CRM_Core_Error::debug_log_message("Success: Database updated");
937cf542
EM
667 if ($this->_isRecurring) {
668 $this->sendRecurringStartOrEndNotification($ids, $recur);
669 }
6a488035
TO
670 }
671
6c786a9b
EM
672 /**
673 * @param $ids
674 *
675 * @return bool
676 */
6a488035
TO
677 function getBillingID(&$ids) {
678 // get the billing location type
180409a4 679 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
680 // CRM-8108 remove the ts around the Billing locationtype
681 //$ids['billing'] = array_search( ts('Billing'), $locationTypes );
682 $ids['billing'] = array_search('Billing', $locationTypes);
683 if (!$ids['billing']) {
684 CRM_Core_Error::debug_log_message(ts('Please set a location type of %1', array(1 => 'Billing')));
685 echo "Failure: Could not find billing location type<p>";
686 return FALSE;
687 }
688 return TRUE;
689 }
690
c490a46a 691 /**
6a488035
TO
692 * Send receipt from contribution. Note that the compose message part has been moved to contribution
693 * In general LoadObjects is called first to get the objects but the composeMessageArray function now calls it
694 *
c490a46a
CW
695 * @param array $input Incoming data from Payment processor
696 * @param array $ids Related object IDs
6c786a9b 697 * @param $objects
c490a46a
CW
698 * @param array $values values related to objects that have already been loaded
699 * @param bool $recur is it part of a recurring contribution
700 * @param bool $returnMessageText Should text be returned instead of sent. This
701 * is because the function is also used to generate pdfs
6c786a9b 702 *
c490a46a 703 * @return array
6c786a9b 704 */
6a488035
TO
705 function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) {
706 $contribution = &$objects['contribution'];
707 $input['is_recur'] = $recur;
708 // set receipt from e-mail and name in value
709 if (!$returnMessageText) {
710 $session = CRM_Core_Session::singleton();
711 $userID = $session->get('userID');
712 if (!empty($userID)) {
713 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
714 $values['receipt_from_email'] = $userEmail;
715 $values['receipt_from_name'] = $userName;
716 }
717 }
718 return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText);
719 }
720
937cf542
EM
721 /**
722 * Send start or end notification for recurring payments
723 * @param $ids
724 * @param $recur
725 */
726 function sendRecurringStartOrEndNotification($ids, $recur) {
727 if ($this->_isFirstOrLastRecurringPayment) {
728 $autoRenewMembership = FALSE;
729 if ($recur->id &&
730 isset($ids['membership']) && $ids['membership']
731 ) {
732 $autoRenewMembership = TRUE;
733 }
734
735 //send recurring Notification email for user
736 CRM_Contribute_BAO_ContributionPage::recurringNotify($this->_isFirstOrLastRecurringPayment,
737 $ids['contact'],
738 $ids['contributionPage'],
739 $recur,
740 $autoRenewMembership
741 );
742 }
743 }
744
8196c759 745 /**
746 * Update contribution status - this is only called from one place in the code &
747 * it is unclear whether it is a function on the way in or on the way out
748 *
749 * @param unknown_type $params
750 * @return void|Ambigous <value, unknown, array>
751 */
6a488035
TO
752 function updateContributionStatus(&$params) {
753 // get minimum required values.
754 $statusId = CRM_Utils_Array::value('contribution_status_id', $params);
755 $componentId = CRM_Utils_Array::value('component_id', $params);
756 $componentName = CRM_Utils_Array::value('componentName', $params);
757 $contributionId = CRM_Utils_Array::value('contribution_id', $params);
758
759 if (!$contributionId || !$componentId || !$componentName || !$statusId) {
760 return;
761 }
762
763 $input = $ids = $objects = array();
764
765 //get the required ids.
766 $ids['contribution'] = $contributionId;
767
768 if (!$ids['contact'] = CRM_Utils_Array::value('contact_id', $params)) {
769 $ids['contact'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
770 $contributionId,
771 'contact_id'
772 );
773 }
774
775 if ($componentName == 'Event') {
776 $name = 'event';
777 $ids['participant'] = $componentId;
778
779 if (!$ids['event'] = CRM_Utils_Array::value('event_id', $params)) {
780 $ids['event'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
781 $componentId,
782 'event_id'
783 );
784 }
785 }
786
787 if ($componentName == 'Membership') {
788 $name = 'contribute';
789 $ids['membership'] = $componentId;
790 }
791 $ids['contributionPage'] = NULL;
792 $ids['contributionRecur'] = NULL;
793 $input['component'] = $name;
794
795 $baseIPN = new CRM_Core_Payment_BaseIPN();
796 $transaction = new CRM_Core_Transaction();
797
798 // reset template values.
799 $template = CRM_Core_Smarty::singleton();
800 $template->clearTemplateVars();
801
802 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
803 CRM_Core_Error::fatal();
804 }
805
806 $contribution = &$objects['contribution'];
807
808 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
809 $input['skipComponentSync'] = CRM_Utils_Array::value('skipComponentSync', $params);
810 if ($statusId == array_search('Cancelled', $contributionStatuses)) {
811 $baseIPN->cancelled($objects, $transaction, $input);
812 $transaction->commit();
813 return $statusId;
814 }
815 elseif ($statusId == array_search('Failed', $contributionStatuses)) {
816 $baseIPN->failed($objects, $transaction, $input);
817 $transaction->commit();
818 return $statusId;
819 }
820
821 // status is not pending
822 if ($contribution->contribution_status_id != array_search('Pending', $contributionStatuses)) {
823 $transaction->commit();
824 return;
825 }
826
827 //set values for ipn code.
828 foreach (array(
829 'fee_amount', 'check_number', 'payment_instrument_id') as $field) {
830 if (!$input[$field] = CRM_Utils_Array::value($field, $params)) {
831 $input[$field] = $contribution->$field;
832 }
833 }
834 if (!$input['trxn_id'] = CRM_Utils_Array::value('trxn_id', $params)) {
835 $input['trxn_id'] = $contribution->invoice_id;
836 }
837 if (!$input['amount'] = CRM_Utils_Array::value('total_amount', $params)) {
838 $input['amount'] = $contribution->total_amount;
839 }
840 $input['is_test'] = $contribution->is_test;
841 $input['net_amount'] = $contribution->net_amount;
8cc574cf 842 if (!empty($input['fee_amount']) && !empty($input['amount'])) {
6a488035
TO
843 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
844 }
845
846 //complete the contribution.
847 $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE);
848
849 // reset template values before processing next transactions
850 $template->clearTemplateVars();
851
852 return $statusId;
853 }
854
855 /*
856 * Update pledge associated with a recurring contribution
857 *
858 * If the contribution has a pledge_payment record pledge, then update the pledge_payment record & pledge based on that linkage.
859 *
860 * If a previous contribution in the recurring contribution sequence is linked with a pledge then we assume this contribution
861 * should be linked with the same pledge also. Currently only back-office users can apply a recurring payment to a pledge &
862 * it should be assumed they
863 * do so with the intention that all payments will be linked
864 *
865 * The pledge payment record should already exist & will need to be updated with the new contribution ID.
866 * If not the contribution will also need to be linked to the pledge
867 */
6c786a9b
EM
868 /**
869 * @param $contribution
870 */
6a488035
TO
871 function updateRecurLinkedPledge(&$contribution) {
872 $returnProperties = array('id', 'pledge_id');
873 $paymentDetails = $paymentIDs = array();
874
875 if (CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $contribution->id,
876 $paymentDetails, $returnProperties
877 )) {
878 foreach ($paymentDetails as $key => $value) {
879 $paymentIDs[] = $value['id'];
880 $pledgeId = $value['pledge_id'];
881 }
882 }
883 else {
884 //payment is not already linked - if it is linked with a pledge we need to create a link.
885 // return if it is not recurring contribution
886 if (!$contribution->contribution_recur_id) {
887 return;
888 }
889
890 $relatedContributions = new CRM_Contribute_DAO_Contribution();
891 $relatedContributions->contribution_recur_id = $contribution->contribution_recur_id;
892 $relatedContributions->find();
893
894 while ($relatedContributions->fetch()) {
895 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $relatedContributions->id,
896 $paymentDetails, $returnProperties
897 );
898 }
899
900 if (empty($paymentDetails)) {
901 // payment is not linked with a pledge and neither are any other contributions on this
902 return;
903 }
904
905 foreach ($paymentDetails as $key => $value) {
906 $pledgeId = $value['pledge_id'];
907 }
908
909 // we have a pledge now we need to get the oldest unpaid payment
910 $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
e3f3156b 911 if(empty($paymentDetails['id'])){
912 // we can assume this pledge is now completed
913 // return now so we don't create a core error & roll back
914 return;
915 }
6a488035
TO
916 $paymentDetails['contribution_id'] = $contribution->id;
917 $paymentDetails['status_id'] = $contribution->contribution_status_id;
918 $paymentDetails['actual_amount'] = $contribution->total_amount;
919
920 // put contribution against it
921 $payment = CRM_Pledge_BAO_PledgePayment::add($paymentDetails);
922 $paymentIDs[] = $payment->id;
923 }
924
925 // update pledge and corresponding payment statuses
926 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIDs, $contribution->contribution_status_id,
927 NULL, $contribution->total_amount
928 );
929 }
930
6c786a9b 931 /**
c490a46a 932 * @param int $recurId
34a100a7
EM
933 * @param $contribution
934 *
34a100a7 935 * @return array
6c786a9b 936 */
34a100a7
EM
937 function addRecurLineItems($recurId, $contribution) {
938 $lineSets = array();
939
940 $originalContributionID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id');
941 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($originalContributionID);
942 if (!empty($lineItems)) {
943 foreach ($lineItems as $key => $value) {
944 $priceField = new CRM_Price_DAO_PriceField();
945 $priceField->id = $value['price_field_id'];
946 $priceField->find(TRUE);
947 $lineSets[$priceField->price_set_id][] = $value;
948 if ($value['entity_table'] == 'civicrm_membership') {
949 try {
950 civicrm_api3('membership_payment', 'create', array('membership_id' => $value['entity_id'], 'contribution_id' => $contribution->id));
951 }
952 catch (CiviCRM_API3_Exception $e) {
953 // we are catching & ignoring errors as an extra precaution since lost IPNs may be more serious that lost membership_payment data
954 // this fn is unit-tested so risk of changes elsewhere breaking it are otherwise mitigated
955 }
6a488035
TO
956 }
957 }
6a488035 958 }
34a100a7
EM
959 else {
960 CRM_Price_BAO_LineItem::processPriceSet($contribution->id, $lineSets, $contribution);
961 }
962 return $lineSets;
6a488035 963 }
6a488035 964
6357981e
PJ
965 // function to copy custom data of the
966 // initial contribution into its recurring contributions
6c786a9b
EM
967 /**
968 * @param $recurId
969 * @param $targetContributionId
970 */
6357981e
PJ
971 function copyCustomValues($recurId, $targetContributionId) {
972 if ($recurId && $targetContributionId) {
973 // get the initial contribution id of recur id
974 $sourceContributionId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id');
975
976 // if the same contribution is being proccessed then return
977 if ($sourceContributionId == $targetContributionId) {
978 return;
979 }
980 // check if proper recurring contribution record is being processed
981 $targetConRecurId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $targetContributionId, 'contribution_recur_id');
982 if ($targetConRecurId != $recurId) {
983 return;
984 }
985
986 // copy custom data
987 $extends = array('Contribution');
988 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
989 if ($groupTree) {
990 foreach ($groupTree as $groupID => $group) {
991 $table[$groupTree[$groupID]['table_name']] = array('entity_id');
992 foreach ($group['fields'] as $fieldID => $field) {
993 $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name'];
994 }
995 }
996
997 foreach ($table as $tableName => $tableColumns) {
998 $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') ';
999 $tableColumns[0] = $targetContributionId;
1000 $select = 'SELECT ' . implode(', ', $tableColumns);
1001 $from = ' FROM ' . $tableName;
1002 $where = " WHERE {$tableName}.entity_id = {$sourceContributionId}";
1003 $query = $insert . $select . $from . $where;
1004 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
1005 }
1006 }
1007 }
1008 }
8381af80 1009
1010 // function to copy soft credit record of first recurring contribution
1011 // and add new soft credit against $targetContributionId
6c786a9b
EM
1012 /**
1013 * @param $recurId
1014 * @param $targetContributionId
1015 */
8381af80 1016 function addrecurSoftCredit($recurId, $targetContributionId) {
1017 $contriID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id');
1018
1019 $soft_contribution = new CRM_Contribute_DAO_ContributionSoft();
1020 $soft_contribution->contribution_id = $contriID;
1021
1022 //check if first recurring contribution has any associated soft credit
1023 if ($soft_contribution->find(TRUE)) {
1024 $soft_contribution->contribution_id = $targetContributionId;
1025 unset($soft_contribution->id);
1026 $soft_contribution->save();
1027 }
1028 }
b2b0530a 1029}