Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_BaseIPN { | |
36 | ||
37 | static $_now = NULL; | |
38 | function __construct() { | |
39 | self::$_now = date('YmdHis'); | |
40 | } | |
41 | ||
42 | function validateData(&$input, &$ids, &$objects, $required = TRUE, $paymentProcessorID = NULL) { | |
43 | ||
44 | // make sure contact exists and is valid | |
45 | $contact = new CRM_Contact_DAO_Contact(); | |
46 | $contact->id = $ids['contact']; | |
47 | if (!$contact->find(TRUE)) { | |
48 | CRM_Core_Error::debug_log_message("Could not find contact record: {$ids['contact']} in IPN request: ".print_r($input, TRUE)); | |
49 | echo "Failure: Could not find contact record: {$ids['contact']}<p>"; | |
50 | return FALSE; | |
51 | } | |
52 | ||
53 | // make sure contribution exists and is valid | |
54 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
55 | $contribution->id = $ids['contribution']; | |
56 | if (!$contribution->find(TRUE)) { | |
57 | CRM_Core_Error::debug_log_message("Could not find contribution record: {$contribution->id} in IPN request: ".print_r($input, TRUE)); | |
58 | echo "Failure: Could not find contribution record for {$contribution->id}<p>"; | |
59 | return FALSE; | |
60 | } | |
61 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); | |
62 | ||
63 | $objects['contact'] = &$contact; | |
64 | $objects['contribution'] = &$contribution; | |
65 | if (!$this->loadObjects($input, $ids, $objects, $required, $paymentProcessorID)) { | |
66 | return FALSE; | |
67 | } | |
68 | ||
69 | return TRUE; | |
70 | } | |
71 | ||
72 | function createContact(&$input, &$ids, &$objects) { | |
73 | $params = array(); | |
74 | $billingID = $ids['billing']; | |
75 | $lookup = array( | |
76 | 'first_name', | |
77 | 'last_name', | |
78 | "street_address-{$billingID}", | |
79 | "city-{$billingID}", | |
80 | "state-{$billingID}", | |
81 | "postal_code-{$billingID}", | |
82 | "country-{$billingID}", | |
83 | ); | |
84 | foreach ($lookup as $name) { | |
85 | $params[$name] = $input[$name]; | |
86 | } | |
87 | if (!empty($params)) { | |
88 | // update contact record | |
89 | $contact = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray, $ids['contact']); | |
90 | } | |
91 | ||
92 | return TRUE; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Load objects related to contribution | |
97 | * | |
98 | * @input array information from Payment processor | |
99 | */ | |
100 | function loadObjects(&$input, &$ids, &$objects, $required, $paymentProcessorID, $error_handling = NULL) { | |
101 | if (empty($error_handling)) { | |
102 | // default options are that we log an error & echo it out | |
103 | // note that we should refactor this error handling into error code @ some point | |
104 | // but for now setting up enough separation so we can do unit tests | |
105 | $error_handling = array( | |
106 | 'log_error' => 1, | |
107 | 'echo_error' => 1, | |
108 | ); | |
109 | } | |
110 | $ids['paymentProcessor'] = $paymentProcessorID; | |
111 | if (is_a($objects['contribution'], 'CRM_Contribute_BAO_Contribution')) { | |
112 | $contribution = &$objects['contribution']; | |
113 | } | |
114 | else { | |
115 | //legacy support - functions are 'used' to be able to pass in a DAO | |
116 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
117 | $contribution->id = CRM_Utils_Array::value('contribution', $ids); | |
118 | $contribution->find(TRUE); | |
119 | $objects['contribution'] = &$contribution; | |
120 | } | |
121 | try { | |
122 | $success = $contribution->loadRelatedObjects($input, $ids, $required); | |
123 | } | |
124 | catch(Exception$e) { | |
125 | if (CRM_Utils_Array::value('log_error', $error_handling)) { | |
126 | CRM_Core_Error::debug_log_message($e->getMessage()); | |
127 | } | |
128 | if (CRM_Utils_Array::value('echo_error', $error_handling)) { | |
129 | echo ($e->getMessage()); | |
130 | } | |
131 | if (CRM_Utils_Array::value('return_error', $error_handling)) { | |
132 | return array( | |
133 | 'is_error' => 1, | |
134 | 'error_message' => ($e->getMessage()), | |
135 | ); | |
136 | } | |
137 | } | |
138 | $objects = array_merge($objects, $contribution->_relatedObjects); | |
139 | return $success; | |
140 | } | |
141 | ||
142 | function failed(&$objects, &$transaction, $input = array()) { | |
143 | $contribution = &$objects['contribution']; | |
144 | $memberships = array(); | |
145 | if (CRM_Utils_Array::value('membership', $objects)) { | |
146 | $memberships = &$objects['membership']; | |
147 | if (is_numeric($memberships)) { | |
148 | $memberships = array($objects['membership']); | |
149 | } | |
150 | } | |
151 | ||
152 | $addLineItems = FALSE; | |
153 | if (empty($contribution->id)) { | |
154 | $addLineItems = TRUE; | |
155 | } | |
156 | $participant = &$objects['participant']; | |
157 | ||
158 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
159 | $contribution->contribution_status_id = array_search('Failed', $contributionStatus); | |
160 | $contribution->save(); | |
161 | ||
162 | //add lineitems for recurring payments | |
163 | if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id && $addLineItems) { | |
164 | $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); | |
165 | } | |
166 | ||
167 | if (!CRM_Utils_Array::value('skipComponentSync', $input)) { | |
168 | if (!empty($memberships)) { | |
169 | foreach ($memberships as $membership) { | |
170 | if ($membership) { | |
171 | $membership->status_id = 4; | |
172 | $membership->save(); | |
d63f4fc3 | 173 | |
6a488035 TO |
174 | //update related Memberships. |
175 | $params = array('status_id' => 4); | |
176 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params); | |
177 | } | |
178 | } | |
179 | } | |
d63f4fc3 | 180 | |
6a488035 TO |
181 | if ($participant) { |
182 | $participant->status_id = 4; | |
183 | $participant->save(); | |
184 | } | |
185 | } | |
186 | ||
187 | $transaction->commit(); | |
188 | CRM_Core_Error::debug_log_message("Setting contribution status to failed"); | |
189 | //echo "Success: Setting contribution status to failed<p>"; | |
190 | return TRUE; | |
191 | } | |
192 | ||
193 | function pending(&$objects, &$transaction) { | |
194 | $transaction->commit(); | |
195 | CRM_Core_Error::debug_log_message("returning since contribution status is pending"); | |
196 | echo "Success: Returning since contribution status is pending<p>"; | |
197 | return TRUE; | |
198 | } | |
199 | ||
200 | function cancelled(&$objects, &$transaction, $input = array()) { | |
201 | $contribution = &$objects['contribution']; | |
202 | $memberships = &$objects['membership']; | |
203 | if (is_numeric($memberships)) { | |
204 | $memberships = array($objects['membership']); | |
205 | } | |
206 | ||
207 | $participant = &$objects['participant']; | |
208 | $addLineItems = FALSE; | |
209 | if (empty($contribution->id)) { | |
210 | $addLineItems = TRUE; | |
211 | } | |
212 | $contribution->contribution_status_id = 3; | |
213 | $contribution->cancel_date = self::$_now; | |
214 | $contribution->cancel_reason = CRM_Utils_Array::value('reasonCode', $input); | |
215 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); | |
216 | $contribution->receipt_date = CRM_Utils_Date::isoToMysql($contribution->receipt_date); | |
217 | $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date); | |
218 | $contribution->save(); | |
219 | ||
220 | //add lineitems for recurring payments | |
221 | if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id && $addLineItems) { | |
222 | $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); | |
223 | } | |
224 | ||
225 | if (!CRM_Utils_Array::value('skipComponentSync', $input)) { | |
226 | if (!empty($memberships)) { | |
227 | foreach ($memberships as $membership) { | |
228 | if ($membership) { | |
229 | $membership->status_id = 6; | |
230 | $membership->save(); | |
d63f4fc3 | 231 | |
6a488035 TO |
232 | //update related Memberships. |
233 | $params = array('status_id' => 6); | |
234 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params); | |
235 | } | |
236 | } | |
237 | } | |
d63f4fc3 | 238 | |
6a488035 TO |
239 | if ($participant) { |
240 | $participant->status_id = 4; | |
241 | $participant->save(); | |
242 | } | |
243 | } | |
244 | $transaction->commit(); | |
245 | CRM_Core_Error::debug_log_message("Setting contribution status to cancelled"); | |
246 | //echo "Success: Setting contribution status to cancelled<p>"; | |
247 | return TRUE; | |
248 | } | |
249 | ||
250 | function unhandled(&$objects, &$transaction) { | |
251 | $transaction->rollback(); | |
252 | // we dont handle this as yet | |
253 | CRM_Core_Error::debug_log_message("returning since contribution status: $status is not handled"); | |
254 | echo "Failure: contribution status $status is not handled<p>"; | |
255 | return FALSE; | |
256 | } | |
257 | ||
258 | function completeTransaction(&$input, &$ids, &$objects, &$transaction, $recur = FALSE) { | |
259 | $contribution = &$objects['contribution']; | |
260 | $memberships = &$objects['membership']; | |
261 | if (is_numeric($memberships)) { | |
262 | $memberships = array($objects['membership']); | |
263 | } | |
264 | $participant = &$objects['participant']; | |
265 | $event = &$objects['event']; | |
266 | $changeToday = CRM_Utils_Array::value('trxn_date', $input, self::$_now); | |
267 | $recurContrib = &$objects['contributionRecur']; | |
268 | ||
269 | $values = array(); | |
270 | $source = NULL; | |
271 | if ($input['component'] == 'contribute') { | |
272 | if ($contribution->contribution_page_id) { | |
273 | CRM_Contribute_BAO_ContributionPage::setValues($contribution->contribution_page_id, $values); | |
274 | $source = ts('Online Contribution') . ': ' . $values['title']; | |
275 | } | |
276 | elseif ($recurContrib && $recurContrib->id) { | |
277 | $contribution->contribution_page_id = NULL; | |
278 | $values['amount'] = $recurContrib->amount; | |
279 | $values['financial_type_id'] = $objects['contributionType']->id; | |
280 | $values['title'] = $source = ts('Offline Recurring Contribution'); | |
281 | $values['is_email_receipt'] = $recurContrib->is_email_receipt; | |
282 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); | |
283 | $values['receipt_from_name'] = $domainValues[0]; | |
284 | $values['receipt_from_email'] = $domainValues[1]; | |
285 | } | |
286 | ||
287 | $contribution->source = $source; | |
288 | if (CRM_Utils_Array::value('is_email_receipt', $values)) { | |
289 | $contribution->receipt_date = self::$_now; | |
290 | } | |
291 | ||
292 | if (!empty($memberships)) { | |
293 | $membershipsUpdate = array( ); | |
294 | foreach ($memberships as $membershipTypeIdKey => $membership) { | |
295 | if ($membership) { | |
296 | $format = '%Y%m%d'; | |
297 | ||
298 | $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membership->contact_id, | |
299 | $membership->membership_type_id, | |
300 | $membership->is_test, $membership->id | |
301 | ); | |
302 | ||
303 | // CRM-8141 update the membership type with the value recorded in log when membership created/renewed | |
304 | // this picks up membership type changes during renewals | |
305 | $sql = " | |
306 | SELECT membership_type_id | |
307 | FROM civicrm_membership_log | |
308 | WHERE membership_id=$membership->id | |
309 | ORDER BY id DESC | |
310 | LIMIT 1;"; | |
311 | $dao = new CRM_Core_DAO; | |
312 | $dao->query($sql); | |
313 | if ($dao->fetch()) { | |
314 | if (!empty($dao->membership_type_id)) { | |
315 | $membership->membership_type_id = $dao->membership_type_id; | |
316 | $membership->save(); | |
317 | } | |
318 | // else fall back to using current membership type | |
319 | } | |
320 | // else fall back to using current membership type | |
321 | $dao->free(); | |
322 | ||
323 | if ($currentMembership) { | |
324 | /* | |
325 | * Fixed FOR CRM-4433 | |
326 | * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status | |
327 | * when Contribution mode is notify and membership is for renewal ) | |
328 | */ | |
329 | CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday); | |
330 | ||
331 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, | |
332 | $changeToday | |
333 | ); | |
334 | $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format); | |
335 | } | |
336 | else { | |
337 | $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->membership_type_id); | |
338 | } | |
339 | ||
340 | //get the status for membership. | |
341 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], | |
342 | $dates['end_date'], | |
343 | $dates['join_date'], | |
344 | 'today', | |
345 | TRUE | |
346 | ); | |
347 | ||
348 | $formatedParams = array('status_id' => CRM_Utils_Array::value('id', $calcStatus, 2), | |
349 | 'join_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $dates), $format), | |
350 | 'start_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $dates), $format), | |
351 | 'end_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $dates), $format), | |
352 | ); | |
353 | //we might be renewing membership, | |
354 | //so make status override false. | |
355 | $formatedParams['is_override'] = FALSE; | |
356 | $membership->copyValues($formatedParams); | |
357 | $membership->save(); | |
358 | ||
359 | //updating the membership log | |
360 | $membershipLog = array(); | |
361 | $membershipLog = $formatedParams; | |
362 | ||
363 | $logStartDate = $formatedParams['start_date']; | |
364 | if (CRM_Utils_Array::value('log_start_date', $dates)) { | |
365 | $logStartDate = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); | |
366 | $logStartDate = CRM_Utils_Date::isoToMysql($logStartDate); | |
367 | } | |
368 | ||
369 | $membershipLog['start_date'] = $logStartDate; | |
370 | $membershipLog['membership_id'] = $membership->id; | |
371 | $membershipLog['modified_id'] = $membership->contact_id; | |
372 | $membershipLog['modified_date'] = date('Ymd'); | |
373 | $membershipLog['membership_type_id'] = $membership->membership_type_id; | |
374 | ||
375 | CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); | |
376 | ||
377 | //update related Memberships. | |
378 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formatedParams); | |
d63f4fc3 | 379 | |
6a488035 TO |
380 | //update the membership type key of membership relatedObjects array |
381 | //if it has changed after membership update | |
382 | if ($membershipTypeIdKey != $membership->membership_type_id) { | |
383 | $membershipsUpdate[$membership->membership_type_id] = $membership; | |
384 | $contribution->_relatedObjects['membership'][$membership->membership_type_id] = $membership; | |
385 | unset($contribution->_relatedObjects['membership'][$membershipTypeIdKey]); | |
386 | unset($memberships[$membershipTypeIdKey]); | |
387 | } | |
388 | } | |
389 | } | |
390 | //update the memberships object with updated membershipTypeId data | |
391 | //if membershipTypeId has changed after membership update | |
392 | if (!empty($membershipsUpdate)) { | |
393 | $memberships = $memberships + $membershipsUpdate; | |
394 | } | |
395 | } | |
396 | } | |
397 | else { | |
398 | // event | |
399 | $eventParams = array('id' => $objects['event']->id); | |
400 | $values['event'] = array(); | |
401 | ||
402 | CRM_Event_BAO_Event::retrieve($eventParams, $values['event']); | |
403 | ||
404 | //get location details | |
405 | $locationParams = array('entity_id' => $objects['event']->id, 'entity_table' => 'civicrm_event'); | |
406 | $values['location'] = CRM_Core_BAO_Location::getValues($locationParams); | |
407 | ||
408 | $ufJoinParams = array( | |
409 | 'entity_table' => 'civicrm_event', | |
410 | 'entity_id' => $ids['event'], | |
411 | 'module' => 'CiviEvent', | |
412 | ); | |
d63f4fc3 | 413 | |
6a488035 TO |
414 | list($custom_pre_id, |
415 | $custom_post_ids | |
416 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
d63f4fc3 | 417 | |
6a488035 TO |
418 | $values['custom_pre_id'] = $custom_pre_id; |
419 | $values['custom_post_id'] = $custom_post_ids; | |
420 | ||
421 | $contribution->source = ts('Online Event Registration') . ': ' . $values['event']['title']; | |
422 | ||
423 | if ($values['event']['is_email_confirm']) { | |
424 | $contribution->receipt_date = self::$_now; | |
425 | $values['is_email_receipt'] = 1; | |
426 | } | |
427 | if (!CRM_Utils_Array::value('skipComponentSync', $input)) { | |
428 | $participant->status_id = 1; | |
429 | } | |
430 | $participant->save(); | |
431 | } | |
432 | ||
433 | if (CRM_Utils_Array::value('net_amount', $input, 0) == 0 && | |
434 | CRM_Utils_Array::value('fee_amount', $input, 0) != 0 | |
435 | ) { | |
436 | $input['net_amount'] = $input['amount'] - $input['fee_amount']; | |
437 | } | |
438 | $addLineItems = FALSE; | |
439 | if (empty($contribution->id)) { | |
440 | $addLineItems = TRUE; | |
441 | } | |
d63f4fc3 | 442 | |
6a488035 TO |
443 | $contribution->contribution_status_id = 1; |
444 | $contribution->is_test = $input['is_test']; | |
445 | $contribution->fee_amount = CRM_Utils_Array::value('fee_amount', $input, 0); | |
446 | $contribution->net_amount = CRM_Utils_Array::value('net_amount', $input, 0); | |
447 | $contribution->trxn_id = $input['trxn_id']; | |
448 | $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date); | |
449 | $contribution->thankyou_date = CRM_Utils_Date::isoToMysql($contribution->thankyou_date); | |
450 | $contribution->cancel_date = 'null'; | |
451 | ||
452 | if (CRM_Utils_Array::value('check_number', $input)) { | |
453 | $contribution->check_number = $input['check_number']; | |
454 | } | |
455 | ||
456 | if (CRM_Utils_Array::value('payment_instrument_id', $input)) { | |
457 | $contribution->payment_instrument_id = $input['payment_instrument_id']; | |
458 | } | |
d63f4fc3 | 459 | |
6a488035 TO |
460 | if ($contribution->id) { |
461 | $contributionId['id'] = $contribution->id; | |
462 | $input['prevContribution'] = CRM_Contribute_BAO_Contribution::getValues($contributionId, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); | |
463 | } | |
464 | $contribution->save(); | |
465 | ||
466 | //add lineitems for recurring payments | |
467 | if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id && $addLineItems) { | |
468 | $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); | |
469 | } | |
470 | ||
471 | // next create the transaction record | |
472 | $paymentProcessor = $paymentProcessorId = ''; | |
473 | if (isset($objects['paymentProcessor'])) { | |
474 | if (is_array($objects['paymentProcessor'])) { | |
475 | $paymentProcessor = $objects['paymentProcessor']['payment_processor_type']; | |
476 | $paymentProcessorId = $objects['paymentProcessor']['id']; | |
477 | } | |
478 | else { | |
479 | $paymentProcessor = $objects['paymentProcessor']->payment_processor_type; | |
480 | $paymentProcessorId = $objects['paymentProcessor']->id; | |
481 | } | |
482 | } | |
483 | ||
484 | if ($contribution->id) { | |
485 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
d63f4fc3 | 486 | if (!$input['prevContribution']->is_pay_later && |
6a488035 TO |
487 | $input['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatuses)) { |
488 | $input['payment_processor'] = $paymentProcessorId; | |
489 | } | |
490 | $input['total_amount'] = $input['amount']; | |
491 | $input['contribution'] = $contribution; | |
a1f5ffcc | 492 | $input['financial_type_id'] = $contribution->financial_type_id; |
493 | ||
6a488035 TO |
494 | if (CRM_Utils_Array::value('participant', $contribution->_relatedObjects)) { |
495 | $input['contribution_mode'] = 'participant'; | |
496 | $input['participant_id'] = $contribution->_relatedObjects['participant']->id; | |
0ba0addf | 497 | $input['skipLineItem'] = 1; |
f92fd420 | 498 | } |
499 | ||
6a488035 TO |
500 | CRM_Contribute_BAO_Contribution::recordFinancialAccounts($input, NULL); |
501 | } | |
502 | ||
503 | self::updateRecurLinkedPledge($contribution); | |
504 | ||
505 | // create an activity record | |
506 | if ($input['component'] == 'contribute') { | |
507 | //CRM-4027 | |
508 | $targetContactID = NULL; | |
509 | if (CRM_Utils_Array::value('related_contact', $ids)) { | |
510 | $targetContactID = $contribution->contact_id; | |
511 | $contribution->contact_id = $ids['related_contact']; | |
512 | } | |
513 | CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID); | |
514 | // event | |
515 | } | |
516 | else { | |
517 | CRM_Activity_BAO_Activity::addActivity($participant); | |
518 | } | |
519 | ||
520 | CRM_Core_Error::debug_log_message("Contribution record updated successfully"); | |
521 | $transaction->commit(); | |
522 | ||
523 | // CRM-9132 legacy behaviour was that receipts were sent out in all instances. Still sending | |
524 | // when array_key 'is_email_receipt doesn't exist in case some instances where is needs setting haven't been set | |
525 | if (!array_key_exists('is_email_receipt', $values) || | |
526 | $values['is_email_receipt'] == 1 | |
527 | ) { | |
528 | self::sendMail($input, $ids, $objects, $values, $recur, FALSE); | |
bf014492 | 529 | CRM_Core_Error::debug_log_message("Receipt sent"); |
6a488035 TO |
530 | } |
531 | ||
bf014492 | 532 | CRM_Core_Error::debug_log_message("Success: Database updated"); |
6a488035 TO |
533 | } |
534 | ||
535 | function getBillingID(&$ids) { | |
536 | // get the billing location type | |
537 | $locationTypes = CRM_Core_PseudoConstant::locationType(); | |
538 | // CRM-8108 remove the ts around the Billing locationtype | |
539 | //$ids['billing'] = array_search( ts('Billing'), $locationTypes ); | |
540 | $ids['billing'] = array_search('Billing', $locationTypes); | |
541 | if (!$ids['billing']) { | |
542 | CRM_Core_Error::debug_log_message(ts('Please set a location type of %1', array(1 => 'Billing'))); | |
543 | echo "Failure: Could not find billing location type<p>"; | |
544 | return FALSE; | |
545 | } | |
546 | return TRUE; | |
547 | } | |
548 | ||
549 | /* | |
550 | * Send receipt from contribution. Note that the compose message part has been moved to contribution | |
551 | * In general LoadObjects is called first to get the objects but the composeMessageArray function now calls it | |
552 | * | |
553 | * @params array $input Incoming data from Payment processor | |
554 | * @params array $ids Related object IDs | |
555 | * @params array $values values related to objects that have already been loaded | |
556 | * @params bool $recur is it part of a recurring contribution | |
557 | * @params bool $returnMessageText Should text be returned instead of sent. This | |
558 | * is because the function is also used to generate pdfs | |
559 | */ | |
560 | function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) { | |
561 | $contribution = &$objects['contribution']; | |
562 | $input['is_recur'] = $recur; | |
563 | // set receipt from e-mail and name in value | |
564 | if (!$returnMessageText) { | |
565 | $session = CRM_Core_Session::singleton(); | |
566 | $userID = $session->get('userID'); | |
567 | if (!empty($userID)) { | |
568 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
569 | $values['receipt_from_email'] = $userEmail; | |
570 | $values['receipt_from_name'] = $userName; | |
571 | } | |
572 | } | |
573 | return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText); | |
574 | } | |
575 | ||
576 | function updateContributionStatus(&$params) { | |
577 | // get minimum required values. | |
578 | $statusId = CRM_Utils_Array::value('contribution_status_id', $params); | |
579 | $componentId = CRM_Utils_Array::value('component_id', $params); | |
580 | $componentName = CRM_Utils_Array::value('componentName', $params); | |
581 | $contributionId = CRM_Utils_Array::value('contribution_id', $params); | |
582 | ||
583 | if (!$contributionId || !$componentId || !$componentName || !$statusId) { | |
584 | return; | |
585 | } | |
586 | ||
587 | $input = $ids = $objects = array(); | |
588 | ||
589 | //get the required ids. | |
590 | $ids['contribution'] = $contributionId; | |
591 | ||
592 | if (!$ids['contact'] = CRM_Utils_Array::value('contact_id', $params)) { | |
593 | $ids['contact'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
594 | $contributionId, | |
595 | 'contact_id' | |
596 | ); | |
597 | } | |
598 | ||
599 | if ($componentName == 'Event') { | |
600 | $name = 'event'; | |
601 | $ids['participant'] = $componentId; | |
602 | ||
603 | if (!$ids['event'] = CRM_Utils_Array::value('event_id', $params)) { | |
604 | $ids['event'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
605 | $componentId, | |
606 | 'event_id' | |
607 | ); | |
608 | } | |
609 | } | |
610 | ||
611 | if ($componentName == 'Membership') { | |
612 | $name = 'contribute'; | |
613 | $ids['membership'] = $componentId; | |
614 | } | |
615 | $ids['contributionPage'] = NULL; | |
616 | $ids['contributionRecur'] = NULL; | |
617 | $input['component'] = $name; | |
618 | ||
619 | $baseIPN = new CRM_Core_Payment_BaseIPN(); | |
620 | $transaction = new CRM_Core_Transaction(); | |
621 | ||
622 | // reset template values. | |
623 | $template = CRM_Core_Smarty::singleton(); | |
624 | $template->clearTemplateVars(); | |
625 | ||
626 | if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) { | |
627 | CRM_Core_Error::fatal(); | |
628 | } | |
629 | ||
630 | $contribution = &$objects['contribution']; | |
631 | ||
632 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
633 | $input['skipComponentSync'] = CRM_Utils_Array::value('skipComponentSync', $params); | |
634 | if ($statusId == array_search('Cancelled', $contributionStatuses)) { | |
635 | $baseIPN->cancelled($objects, $transaction, $input); | |
636 | $transaction->commit(); | |
637 | return $statusId; | |
638 | } | |
639 | elseif ($statusId == array_search('Failed', $contributionStatuses)) { | |
640 | $baseIPN->failed($objects, $transaction, $input); | |
641 | $transaction->commit(); | |
642 | return $statusId; | |
643 | } | |
644 | ||
645 | // status is not pending | |
646 | if ($contribution->contribution_status_id != array_search('Pending', $contributionStatuses)) { | |
647 | $transaction->commit(); | |
648 | return; | |
649 | } | |
650 | ||
651 | //set values for ipn code. | |
652 | foreach (array( | |
653 | 'fee_amount', 'check_number', 'payment_instrument_id') as $field) { | |
654 | if (!$input[$field] = CRM_Utils_Array::value($field, $params)) { | |
655 | $input[$field] = $contribution->$field; | |
656 | } | |
657 | } | |
658 | if (!$input['trxn_id'] = CRM_Utils_Array::value('trxn_id', $params)) { | |
659 | $input['trxn_id'] = $contribution->invoice_id; | |
660 | } | |
661 | if (!$input['amount'] = CRM_Utils_Array::value('total_amount', $params)) { | |
662 | $input['amount'] = $contribution->total_amount; | |
663 | } | |
664 | $input['is_test'] = $contribution->is_test; | |
665 | $input['net_amount'] = $contribution->net_amount; | |
666 | if (CRM_Utils_Array::value('fee_amount', $input) && CRM_Utils_Array::value('amount', $input)) { | |
667 | $input['net_amount'] = $input['amount'] - $input['fee_amount']; | |
668 | } | |
669 | ||
670 | //complete the contribution. | |
671 | $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE); | |
672 | ||
673 | // reset template values before processing next transactions | |
674 | $template->clearTemplateVars(); | |
675 | ||
676 | return $statusId; | |
677 | } | |
678 | ||
679 | /* | |
680 | * Update pledge associated with a recurring contribution | |
681 | * | |
682 | * If the contribution has a pledge_payment record pledge, then update the pledge_payment record & pledge based on that linkage. | |
683 | * | |
684 | * If a previous contribution in the recurring contribution sequence is linked with a pledge then we assume this contribution | |
685 | * should be linked with the same pledge also. Currently only back-office users can apply a recurring payment to a pledge & | |
686 | * it should be assumed they | |
687 | * do so with the intention that all payments will be linked | |
688 | * | |
689 | * The pledge payment record should already exist & will need to be updated with the new contribution ID. | |
690 | * If not the contribution will also need to be linked to the pledge | |
691 | */ | |
692 | function updateRecurLinkedPledge(&$contribution) { | |
693 | $returnProperties = array('id', 'pledge_id'); | |
694 | $paymentDetails = $paymentIDs = array(); | |
695 | ||
696 | if (CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $contribution->id, | |
697 | $paymentDetails, $returnProperties | |
698 | )) { | |
699 | foreach ($paymentDetails as $key => $value) { | |
700 | $paymentIDs[] = $value['id']; | |
701 | $pledgeId = $value['pledge_id']; | |
702 | } | |
703 | } | |
704 | else { | |
705 | //payment is not already linked - if it is linked with a pledge we need to create a link. | |
706 | // return if it is not recurring contribution | |
707 | if (!$contribution->contribution_recur_id) { | |
708 | return; | |
709 | } | |
710 | ||
711 | $relatedContributions = new CRM_Contribute_DAO_Contribution(); | |
712 | $relatedContributions->contribution_recur_id = $contribution->contribution_recur_id; | |
713 | $relatedContributions->find(); | |
714 | ||
715 | while ($relatedContributions->fetch()) { | |
716 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $relatedContributions->id, | |
717 | $paymentDetails, $returnProperties | |
718 | ); | |
719 | } | |
720 | ||
721 | if (empty($paymentDetails)) { | |
722 | // payment is not linked with a pledge and neither are any other contributions on this | |
723 | return; | |
724 | } | |
725 | ||
726 | foreach ($paymentDetails as $key => $value) { | |
727 | $pledgeId = $value['pledge_id']; | |
728 | } | |
729 | ||
730 | // we have a pledge now we need to get the oldest unpaid payment | |
731 | $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
e3f3156b | 732 | if(empty($paymentDetails['id'])){ |
733 | // we can assume this pledge is now completed | |
734 | // return now so we don't create a core error & roll back | |
735 | return; | |
736 | } | |
6a488035 TO |
737 | $paymentDetails['contribution_id'] = $contribution->id; |
738 | $paymentDetails['status_id'] = $contribution->contribution_status_id; | |
739 | $paymentDetails['actual_amount'] = $contribution->total_amount; | |
740 | ||
741 | // put contribution against it | |
742 | $payment = CRM_Pledge_BAO_PledgePayment::add($paymentDetails); | |
743 | $paymentIDs[] = $payment->id; | |
744 | } | |
745 | ||
746 | // update pledge and corresponding payment statuses | |
747 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIDs, $contribution->contribution_status_id, | |
748 | NULL, $contribution->total_amount | |
749 | ); | |
750 | } | |
751 | ||
752 | function addrecurLineItems($recurId, $contributionId) { | |
753 | $lineSets = $lineItems = array(); | |
754 | ||
755 | //Get the first contribution id with recur id | |
756 | if ($recurId) { | |
757 | $contriID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id'); | |
758 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($contriID, 'contribution'); | |
759 | if (!empty($lineItems)) { | |
760 | foreach ($lineItems as $key => $value) { | |
761 | $pricesetID = new CRM_Price_DAO_Field(); | |
762 | $pricesetID->id = $value['price_field_id']; | |
763 | $pricesetID->find(TRUE); | |
764 | $lineSets[$pricesetID->price_set_id][] = $value; | |
765 | } | |
766 | } | |
767 | ||
768 | CRM_Price_BAO_LineItem::processPriceSet($contributionId, $lineSets); | |
769 | } | |
770 | } | |
771 | } | |
772 |