3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Core_Payment_BaseIPN
{
38 function __construct() {
39 self
::$_now = date('YmdHis');
42 function validateData(&$input, &$ids, &$objects, $required = TRUE, $paymentProcessorID = NULL) {
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>";
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>";
61 $contribution->receive_date
= CRM_Utils_Date
::isoToMysql($contribution->receive_date
);
63 $objects['contact'] = &$contact;
64 $objects['contribution'] = &$contribution;
65 if (!$this->loadObjects($input, $ids, $objects, $required, $paymentProcessorID)) {
72 function createContact(&$input, &$ids, &$objects) {
74 $billingID = $ids['billing'];
78 "street_address-{$billingID}",
81 "postal_code-{$billingID}",
82 "country-{$billingID}",
84 foreach ($lookup as $name) {
85 $params[$name] = $input[$name];
87 if (!empty($params)) {
88 // update contact record
89 $contact = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray, $ids['contact']);
96 * Load objects related to contribution
98 * @input array information from Payment processor
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(
110 $ids['paymentProcessor'] = $paymentProcessorID;
111 if (is_a($objects['contribution'], 'CRM_Contribute_BAO_Contribution')) {
112 $contribution = &$objects['contribution'];
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;
122 $success = $contribution->loadRelatedObjects($input, $ids, $required);
125 if (CRM_Utils_Array
::value('log_error', $error_handling)) {
126 CRM_Core_Error
::debug_log_message($e->getMessage());
128 if (CRM_Utils_Array
::value('echo_error', $error_handling)) {
129 echo ($e->getMessage());
131 if (CRM_Utils_Array
::value('return_error', $error_handling)) {
134 'error_message' => ($e->getMessage()),
138 $objects = array_merge($objects, $contribution->_relatedObjects
);
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']);
152 $addLineItems = FALSE;
153 if (empty($contribution->id
)) {
154 $addLineItems = TRUE;
156 $participant = &$objects['participant'];
158 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
159 $contribution->contribution_status_id
= array_search('Failed', $contributionStatus);
160 $contribution->save();
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
);
167 if (!CRM_Utils_Array
::value('skipComponentSync', $input)) {
168 if (!empty($memberships)) {
169 foreach ($memberships as $membership) {
171 $membership->status_id
= 4;
174 //update related Memberships.
175 $params = array('status_id' => 4);
176 CRM_Member_BAO_Membership
::updateRelatedMemberships($membership->id
, $params);
182 $participant->status_id
= 4;
183 $participant->save();
187 $transaction->commit();
188 CRM_Core_Error
::debug_log_message("Setting contribution status to failed");
189 //echo "Success: Setting contribution status to failed<p>";
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>";
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']);
207 $participant = &$objects['participant'];
208 $addLineItems = FALSE;
209 if (empty($contribution->id
)) {
210 $addLineItems = TRUE;
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();
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
);
225 if (!CRM_Utils_Array
::value('skipComponentSync', $input)) {
226 if (!empty($memberships)) {
227 foreach ($memberships as $membership) {
229 $membership->status_id
= 6;
232 //update related Memberships.
233 $params = array('status_id' => 6);
234 CRM_Member_BAO_Membership
::updateRelatedMemberships($membership->id
, $params);
240 $participant->status_id
= 4;
241 $participant->save();
244 $transaction->commit();
245 CRM_Core_Error
::debug_log_message("Setting contribution status to cancelled");
246 //echo "Success: Setting contribution status to cancelled<p>";
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>";
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']);
264 $participant = &$objects['participant'];
265 $event = &$objects['event'];
266 $changeToday = CRM_Utils_Array
::value('trxn_date', $input, self
::$_now);
267 $recurContrib = &$objects['contributionRecur'];
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'];
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];
287 $contribution->source
= $source;
288 if (CRM_Utils_Array
::value('is_email_receipt', $values)) {
289 $contribution->receipt_date
= self
::$_now;
292 if (!empty($memberships)) {
293 $membershipsUpdate = array( );
294 foreach ($memberships as $membershipTypeIdKey => $membership) {
298 $currentMembership = CRM_Member_BAO_Membership
::getContactMembership($membership->contact_id
,
299 $membership->membership_type_id
,
300 $membership->is_test
, $membership->id
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
306 SELECT membership_type_id
307 FROM civicrm_membership_log
308 WHERE membership_id=$membership->id
311 $dao = new CRM_Core_DAO
;
314 if (!empty($dao->membership_type_id
)) {
315 $membership->membership_type_id
= $dao->membership_type_id
;
318 // else fall back to using current membership type
320 // else fall back to using current membership type
323 if ($currentMembership) {
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 )
329 CRM_Member_BAO_Membership
::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
331 $dates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType($membership->id
,
334 $dates['join_date'] = CRM_Utils_Date
::customFormat($currentMembership['join_date'], $format);
337 $dates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membership->membership_type_id
);
340 //get the status for membership.
341 $calcStatus = CRM_Member_BAO_MembershipStatus
::getMembershipStatusByDate($dates['start_date'],
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),
353 //we might be renewing membership,
354 //so make status override false.
355 $formatedParams['is_override'] = FALSE;
356 $membership->copyValues($formatedParams);
359 //updating the membership log
360 $membershipLog = array();
361 $membershipLog = $formatedParams;
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);
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
;
375 CRM_Member_BAO_MembershipLog
::add($membershipLog, CRM_Core_DAO
::$_nullArray);
377 //update related Memberships.
378 CRM_Member_BAO_Membership
::updateRelatedMemberships($membership->id
, $formatedParams);
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]);
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;
399 $eventParams = array('id' => $objects['event']->id
);
400 $values['event'] = array();
402 CRM_Event_BAO_Event
::retrieve($eventParams, $values['event']);
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);
408 $ufJoinParams = array(
409 'entity_table' => 'civicrm_event',
410 'entity_id' => $ids['event'],
411 'module' => 'CiviEvent',
416 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
418 $values['custom_pre_id'] = $custom_pre_id;
419 $values['custom_post_id'] = $custom_post_ids;
421 $contribution->source
= ts('Online Event Registration') . ': ' . $values['event']['title'];
423 if ($values['event']['is_email_confirm']) {
424 $contribution->receipt_date
= self
::$_now;
425 $values['is_email_receipt'] = 1;
427 if (!CRM_Utils_Array
::value('skipComponentSync', $input)) {
428 $participant->status_id
= 1;
430 $participant->save();
433 if (CRM_Utils_Array
::value('net_amount', $input, 0) == 0 &&
434 CRM_Utils_Array
::value('fee_amount', $input, 0) != 0
436 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
438 $addLineItems = FALSE;
439 if (empty($contribution->id
)) {
440 $addLineItems = TRUE;
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';
452 if (CRM_Utils_Array
::value('check_number', $input)) {
453 $contribution->check_number
= $input['check_number'];
456 if (CRM_Utils_Array
::value('payment_instrument_id', $input)) {
457 $contribution->payment_instrument_id
= $input['payment_instrument_id'];
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);
464 $contribution->save();
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
);
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'];
479 $paymentProcessor = $objects['paymentProcessor']->payment_processor_type
;
480 $paymentProcessorId = $objects['paymentProcessor']->id
;
484 if ($contribution->id
) {
485 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
486 if (!$input['prevContribution']->is_pay_later
&&
487 $input['prevContribution']->contribution_status_id
== array_search('Pending', $contributionStatuses)) {
488 $input['payment_processor'] = $paymentProcessorId;
490 $input['total_amount'] = $input['amount'];
491 $input['contribution'] = $contribution;
492 if (CRM_Utils_Array
::value('participant', $contribution->_relatedObjects
)) {
493 $input['contribution_mode'] = 'participant';
494 $input['participant_id'] = $contribution->_relatedObjects
['participant']->id
;
497 CRM_Contribute_BAO_Contribution
::recordFinancialAccounts($input, NULL);
500 self
::updateRecurLinkedPledge($contribution);
502 // create an activity record
503 if ($input['component'] == 'contribute') {
505 $targetContactID = NULL;
506 if (CRM_Utils_Array
::value('related_contact', $ids)) {
507 $targetContactID = $contribution->contact_id
;
508 $contribution->contact_id
= $ids['related_contact'];
510 CRM_Activity_BAO_Activity
::addActivity($contribution, NULL, $targetContactID);
514 CRM_Activity_BAO_Activity
::addActivity($participant);
517 CRM_Core_Error
::debug_log_message("Contribution record updated successfully");
518 $transaction->commit();
520 // CRM-9132 legacy behaviour was that receipts were sent out in all instances. Still sending
521 // when array_key 'is_email_receipt doesn't exist in case some instances where is needs setting haven't been set
522 if (!array_key_exists('is_email_receipt', $values) ||
523 $values['is_email_receipt'] == 1
525 self
::sendMail($input, $ids, $objects, $values, $recur, FALSE);
528 CRM_Core_Error
::debug_log_message("Success: Database updated and mail sent");
531 function getBillingID(&$ids) {
532 // get the billing location type
533 $locationTypes = CRM_Core_PseudoConstant
::locationType();
534 // CRM-8108 remove the ts around the Billing locationtype
535 //$ids['billing'] = array_search( ts('Billing'), $locationTypes );
536 $ids['billing'] = array_search('Billing', $locationTypes);
537 if (!$ids['billing']) {
538 CRM_Core_Error
::debug_log_message(ts('Please set a location type of %1', array(1 => 'Billing')));
539 echo "Failure: Could not find billing location type<p>";
546 * Send receipt from contribution. Note that the compose message part has been moved to contribution
547 * In general LoadObjects is called first to get the objects but the composeMessageArray function now calls it
549 * @params array $input Incoming data from Payment processor
550 * @params array $ids Related object IDs
551 * @params array $values values related to objects that have already been loaded
552 * @params bool $recur is it part of a recurring contribution
553 * @params bool $returnMessageText Should text be returned instead of sent. This
554 * is because the function is also used to generate pdfs
556 function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) {
557 $contribution = &$objects['contribution'];
558 $input['is_recur'] = $recur;
559 // set receipt from e-mail and name in value
560 if (!$returnMessageText) {
561 $session = CRM_Core_Session
::singleton();
562 $userID = $session->get('userID');
563 if (!empty($userID)) {
564 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
565 $values['receipt_from_email'] = $userEmail;
566 $values['receipt_from_name'] = $userName;
569 return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText);
572 function updateContributionStatus(&$params) {
573 // get minimum required values.
574 $statusId = CRM_Utils_Array
::value('contribution_status_id', $params);
575 $componentId = CRM_Utils_Array
::value('component_id', $params);
576 $componentName = CRM_Utils_Array
::value('componentName', $params);
577 $contributionId = CRM_Utils_Array
::value('contribution_id', $params);
579 if (!$contributionId ||
!$componentId ||
!$componentName ||
!$statusId) {
583 $input = $ids = $objects = array();
585 //get the required ids.
586 $ids['contribution'] = $contributionId;
588 if (!$ids['contact'] = CRM_Utils_Array
::value('contact_id', $params)) {
589 $ids['contact'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
595 if ($componentName == 'Event') {
597 $ids['participant'] = $componentId;
599 if (!$ids['event'] = CRM_Utils_Array
::value('event_id', $params)) {
600 $ids['event'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant',
607 if ($componentName == 'Membership') {
608 $name = 'contribute';
609 $ids['membership'] = $componentId;
611 $ids['contributionPage'] = NULL;
612 $ids['contributionRecur'] = NULL;
613 $input['component'] = $name;
615 $baseIPN = new CRM_Core_Payment_BaseIPN();
616 $transaction = new CRM_Core_Transaction();
618 // reset template values.
619 $template = CRM_Core_Smarty
::singleton();
620 $template->clearTemplateVars();
622 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
623 CRM_Core_Error
::fatal();
626 $contribution = &$objects['contribution'];
628 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
629 $input['skipComponentSync'] = CRM_Utils_Array
::value('skipComponentSync', $params);
630 if ($statusId == array_search('Cancelled', $contributionStatuses)) {
631 $baseIPN->cancelled($objects, $transaction, $input);
632 $transaction->commit();
635 elseif ($statusId == array_search('Failed', $contributionStatuses)) {
636 $baseIPN->failed($objects, $transaction, $input);
637 $transaction->commit();
641 // status is not pending
642 if ($contribution->contribution_status_id
!= array_search('Pending', $contributionStatuses)) {
643 $transaction->commit();
647 //set values for ipn code.
649 'fee_amount', 'check_number', 'payment_instrument_id') as $field) {
650 if (!$input[$field] = CRM_Utils_Array
::value($field, $params)) {
651 $input[$field] = $contribution->$field;
654 if (!$input['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $params)) {
655 $input['trxn_id'] = $contribution->invoice_id
;
657 if (!$input['amount'] = CRM_Utils_Array
::value('total_amount', $params)) {
658 $input['amount'] = $contribution->total_amount
;
660 $input['is_test'] = $contribution->is_test
;
661 $input['net_amount'] = $contribution->net_amount
;
662 if (CRM_Utils_Array
::value('fee_amount', $input) && CRM_Utils_Array
::value('amount', $input)) {
663 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
666 //complete the contribution.
667 $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE);
669 // reset template values before processing next transactions
670 $template->clearTemplateVars();
676 * Update pledge associated with a recurring contribution
678 * If the contribution has a pledge_payment record pledge, then update the pledge_payment record & pledge based on that linkage.
680 * If a previous contribution in the recurring contribution sequence is linked with a pledge then we assume this contribution
681 * should be linked with the same pledge also. Currently only back-office users can apply a recurring payment to a pledge &
682 * it should be assumed they
683 * do so with the intention that all payments will be linked
685 * The pledge payment record should already exist & will need to be updated with the new contribution ID.
686 * If not the contribution will also need to be linked to the pledge
688 function updateRecurLinkedPledge(&$contribution) {
689 $returnProperties = array('id', 'pledge_id');
690 $paymentDetails = $paymentIDs = array();
692 if (CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $contribution->id
,
693 $paymentDetails, $returnProperties
695 foreach ($paymentDetails as $key => $value) {
696 $paymentIDs[] = $value['id'];
697 $pledgeId = $value['pledge_id'];
701 //payment is not already linked - if it is linked with a pledge we need to create a link.
702 // return if it is not recurring contribution
703 if (!$contribution->contribution_recur_id
) {
707 $relatedContributions = new CRM_Contribute_DAO_Contribution();
708 $relatedContributions->contribution_recur_id
= $contribution->contribution_recur_id
;
709 $relatedContributions->find();
711 while ($relatedContributions->fetch()) {
712 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $relatedContributions->id
,
713 $paymentDetails, $returnProperties
717 if (empty($paymentDetails)) {
718 // payment is not linked with a pledge and neither are any other contributions on this
722 foreach ($paymentDetails as $key => $value) {
723 $pledgeId = $value['pledge_id'];
726 // we have a pledge now we need to get the oldest unpaid payment
727 $paymentDetails = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
728 if(empty($paymentDetails['id'])){
729 // we can assume this pledge is now completed
730 // return now so we don't create a core error & roll back
733 $paymentDetails['contribution_id'] = $contribution->id
;
734 $paymentDetails['status_id'] = $contribution->contribution_status_id
;
735 $paymentDetails['actual_amount'] = $contribution->total_amount
;
737 // put contribution against it
738 $payment = CRM_Pledge_BAO_PledgePayment
::add($paymentDetails);
739 $paymentIDs[] = $payment->id
;
742 // update pledge and corresponding payment statuses
743 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId, $paymentIDs, $contribution->contribution_status_id
,
744 NULL, $contribution->total_amount
748 function addrecurLineItems($recurId, $contributionId) {
749 $lineSets = $lineItems = array();
751 //Get the first contribution id with recur id
753 $contriID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id');
754 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contriID, 'contribution');
755 if (!empty($lineItems)) {
756 foreach ($lineItems as $key => $value) {
757 $pricesetID = new CRM_Price_DAO_Field();
758 $pricesetID->id
= $value['price_field_id'];
759 $pricesetID->find(TRUE);
760 $lineSets[$pricesetID->price_set_id
][] = $value;
764 CRM_Price_BAO_LineItem
::processPriceSet($contributionId, $lineSets);