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