Example #1
0
 /**
  * Load objects relations to contribution object.
  * Objects are stored in the $_relatedObjects property
  * In the first instance we are just moving functionality from BASEIpn -
  * @see http://issues.civicrm.org/jira/browse/CRM-9996
  *
  * Note that the unit test for the BaseIPN class tests this function
  *
  * @param array $input
  *   Input as delivered from Payment Processor.
  * @param array $ids
  *   Ids as Loaded by Payment Processor.
  * @param bool $loadAll
  *   Load all related objects - even where id not passed in? (allows API to call this).
  *
  * @return bool
  * @throws Exception
  */
 public function loadRelatedObjects(&$input, &$ids, $loadAll = FALSE)
 {
     if ($loadAll) {
         $ids = array_merge($this->getComponentDetails($this->id), $ids);
         if (empty($ids['contact']) && isset($this->contact_id)) {
             $ids['contact'] = $this->contact_id;
         }
     }
     if (empty($this->_component)) {
         if (!empty($ids['event'])) {
             $this->_component = 'event';
         } else {
             $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute'));
         }
     }
     // If the object is not fully populated then make sure it is - this is a more about legacy paths & cautious
     // refactoring than anything else, and has unit test coverage.
     if (empty($this->financial_type_id)) {
         $this->find(TRUE);
     }
     $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value('paymentProcessor', $ids));
     if (!$paymentProcessorID && $this->contribution_page_id) {
         $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor');
         if ($paymentProcessorID) {
             $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage;
         }
     }
     $ids['contributionType'] = $this->financial_type_id;
     $ids['financialType'] = $this->financial_type_id;
     $entities = array('contact' => 'CRM_Contact_BAO_Contact', 'contributionRecur' => 'CRM_Contribute_BAO_ContributionRecur', 'contributionType' => 'CRM_Financial_BAO_FinancialType', 'financialType' => 'CRM_Financial_BAO_FinancialType');
     foreach ($entities as $entity => $bao) {
         if (!empty($ids[$entity])) {
             $this->_relatedObjects[$entity] = new $bao();
             $this->_relatedObjects[$entity]->id = $ids[$entity];
             if (!$this->_relatedObjects[$entity]->find(TRUE)) {
                 throw new CRM_Core_Exception($entity . ' could not be loaded');
             }
         }
     }
     if (!empty($ids['contributionRecur']) && !$paymentProcessorID) {
         $paymentProcessorID = $this->_relatedObjects['contributionRecur']->payment_processor_id;
     }
     if (!empty($ids['pledge_payment'])) {
         foreach ($ids['pledge_payment'] as $key => $paymentID) {
             if (empty($paymentID)) {
                 continue;
             }
             $payment = new CRM_Pledge_BAO_PledgePayment();
             $payment->id = $paymentID;
             if (!$payment->find(TRUE)) {
                 throw new Exception("Could not find pledge payment record: " . $paymentID);
             }
             $this->_relatedObjects['pledge_payment'][] = $payment;
         }
     }
     if ($this->_component == 'contribute') {
         // retrieve the other optional objects first so
         // stuff down the line can use this info and do things
         // CRM-6056
         //in any case get the memberships associated with the contribution
         //because we now support multiple memberships w/ price set
         // see if there are any other memberships to be considered for same contribution.
         $query = "\n            SELECT membership_id\n            FROM   civicrm_membership_payment\nWHERE  contribution_id = %1 ";
         $params = array(1 => array($this->id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($query, $params);
         while ($dao->fetch()) {
             if ($dao->membership_id) {
                 if (!is_array($ids['membership'])) {
                     $ids['membership'] = array();
                 }
                 $ids['membership'][] = $dao->membership_id;
             }
         }
         if (array_key_exists('membership', $ids) && is_array($ids['membership'])) {
             foreach ($ids['membership'] as $id) {
                 if (!empty($id)) {
                     $membership = new CRM_Member_BAO_Membership();
                     $membership->id = $id;
                     if (!$membership->find(TRUE)) {
                         throw new Exception("Could not find membership record: {$id}");
                     }
                     $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date);
                     $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date);
                     $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date);
                     $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership;
                     $membership->free();
                 }
             }
         }
     } else {
         // we are in event mode
         // make sure event exists and is valid
         $event = new CRM_Event_BAO_Event();
         $event->id = $ids['event'];
         if ($ids['event'] && !$event->find(TRUE)) {
             throw new Exception("Could not find event: " . $ids['event']);
         }
         $this->_relatedObjects['event'] =& $event;
         $participant = new CRM_Event_BAO_Participant();
         $participant->id = $ids['participant'];
         if ($ids['participant'] && !$participant->find(TRUE)) {
             throw new Exception("Could not find participant: " . $ids['participant']);
         }
         $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date);
         $this->_relatedObjects['participant'] =& $participant;
         // get the payment processor id from event - this is inaccurate see CRM-16923
         // in future we should look at throwing an exception here rather than an dubious guess.
         if (!$paymentProcessorID) {
             $paymentProcessorID = $this->_relatedObjects['event']->payment_processor;
             if ($paymentProcessorID) {
                 $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromEvent;
             }
         }
     }
     if ($paymentProcessorID) {
         $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live');
         $ids['paymentProcessor'] = $paymentProcessorID;
         $this->_relatedObjects['paymentProcessor'] = $paymentProcessor;
     }
     return TRUE;
 }
 /**
  * Load objects relations to contribution object.
  * Objects are stored in the $_relatedObjects property
  * In the first instance we are just moving functionality from BASEIpn -
  * @see http://issues.civicrm.org/jira/browse/CRM-9996
  *
  * Note that the unit test for the BaseIPN class tests this function
  *
  * @param array $input
  *   Input as delivered from Payment Processor.
  * @param array $ids
  *   Ids as Loaded by Payment Processor.
  * @param bool $required
  *   Is Payment processor / contribution page required.
  * @param bool $loadAll
  *   Load all related objects - even where id not passed in? (allows API to call this).
  *
  * @return bool
  * @throws Exception
  */
 public function loadRelatedObjects(&$input, &$ids, $required = FALSE, $loadAll = FALSE)
 {
     if ($loadAll) {
         $ids = array_merge($this->getComponentDetails($this->id), $ids);
         if (empty($ids['contact']) && isset($this->contact_id)) {
             $ids['contact'] = $this->contact_id;
         }
     }
     if (empty($this->_component)) {
         if (!empty($ids['event'])) {
             $this->_component = 'event';
         } else {
             $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute'));
         }
     }
     $paymentProcessorID = CRM_Utils_Array::value('paymentProcessor', $ids);
     $contributionType = new CRM_Financial_BAO_FinancialType();
     $contributionType->id = $this->financial_type_id;
     $contributionType->find(TRUE);
     if (!empty($ids['contact'])) {
         $this->_relatedObjects['contact'] = new CRM_Contact_BAO_Contact();
         $this->_relatedObjects['contact']->id = $ids['contact'];
         $this->_relatedObjects['contact']->find(TRUE);
     }
     $this->_relatedObjects['contributionType'] = $contributionType;
     if ($this->_component == 'contribute') {
         // retrieve the other optional objects first so
         // stuff down the line can use this info and do things
         // CRM-6056
         //in any case get the memberships associated with the contribution
         //because we now support multiple memberships w/ price set
         // see if there are any other memberships to be considered for same contribution.
         $query = "\n            SELECT membership_id\n            FROM   civicrm_membership_payment\nWHERE  contribution_id = %1 ";
         $params = array(1 => array($this->id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($query, $params);
         while ($dao->fetch()) {
             if ($dao->membership_id) {
                 if (!is_array($ids['membership'])) {
                     $ids['membership'] = array();
                 }
                 $ids['membership'][] = $dao->membership_id;
             }
         }
         if (array_key_exists('membership', $ids) && is_array($ids['membership'])) {
             foreach ($ids['membership'] as $id) {
                 if (!empty($id)) {
                     $membership = new CRM_Member_BAO_Membership();
                     $membership->id = $id;
                     if (!$membership->find(TRUE)) {
                         throw new Exception("Could not find membership record: {$id}");
                     }
                     $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date);
                     $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date);
                     $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date);
                     $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership;
                     $membership->free();
                 }
             }
         }
         if (!empty($ids['pledge_payment'])) {
             foreach ($ids['pledge_payment'] as $key => $paymentID) {
                 if (empty($paymentID)) {
                     continue;
                 }
                 $payment = new CRM_Pledge_BAO_PledgePayment();
                 $payment->id = $paymentID;
                 if (!$payment->find(TRUE)) {
                     throw new Exception("Could not find pledge payment record: " . $paymentID);
                 }
                 $this->_relatedObjects['pledge_payment'][] = $payment;
             }
         }
         if (!empty($ids['contributionRecur'])) {
             $recur = new CRM_Contribute_BAO_ContributionRecur();
             $recur->id = $ids['contributionRecur'];
             if (!$recur->find(TRUE)) {
                 throw new Exception("Could not find recur record: " . $ids['contributionRecur']);
             }
             $this->_relatedObjects['contributionRecur'] =& $recur;
             //get payment processor id from recur object.
             $paymentProcessorID = $recur->payment_processor_id;
         }
         //for normal contribution get the payment processor id.
         if (!$paymentProcessorID) {
             if ($this->contribution_page_id) {
                 // get the payment processor id from contribution page
                 $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor');
             } elseif (empty($ids['pledge_payment'])) {
                 $loadObjectSuccess = TRUE;
                 if ($required) {
                     throw new Exception("Could not find contribution page for contribution record: " . $this->id);
                 }
                 return $loadObjectSuccess;
             }
         }
     } else {
         // we are in event mode
         // make sure event exists and is valid
         $event = new CRM_Event_BAO_Event();
         $event->id = $ids['event'];
         if ($ids['event'] && !$event->find(TRUE)) {
             throw new Exception("Could not find event: " . $ids['event']);
         }
         $this->_relatedObjects['event'] =& $event;
         $participant = new CRM_Event_BAO_Participant();
         $participant->id = $ids['participant'];
         if ($ids['participant'] && !$participant->find(TRUE)) {
             throw new Exception("Could not find participant: " . $ids['participant']);
         }
         $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date);
         $this->_relatedObjects['participant'] =& $participant;
         if (!$paymentProcessorID) {
             $paymentProcessorID = $this->_relatedObjects['event']->payment_processor;
         }
     }
     if ($paymentProcessorID) {
         $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live');
         $ids['paymentProcessor'] = $paymentProcessorID;
         $this->_relatedObjects['paymentProcessor'] = $paymentProcessor;
     } elseif ($required) {
         throw new Exception("Could not find payment processor for contribution record: " . $this->id);
     }
     return TRUE;
 }
 /**
  * Retrieve DB object based on input parameters.
  *
  * It also stores all the retrieved values in the default array.
  *
  * @param array $params
  *   (reference ) an assoc array of name/value pairs.
  * @param array $defaults
  *   (reference ) an assoc array to hold the flattened values.
  *
  * @return CRM_Pledge_BAO_PledgePayment
  */
 public static function retrieve(&$params, &$defaults)
 {
     $payment = new CRM_Pledge_BAO_PledgePayment();
     $payment->copyValues($params);
     if ($payment->find(TRUE)) {
         CRM_Core_DAO::storeValues($payment, $defaults);
         return $payment;
     }
     return NULL;
 }
Example #4
0
 /**
  * Load objects relations to contribution object.
  * Objects are stored in the $_relatedObjects property
  * In the first instance we are just moving functionality from BASEIpn -
  * @see http://issues.civicrm.org/jira/browse/CRM-9996
  *
  * Note that the unit test for the BaseIPN class tests this function
  *
  * @param array $input
  *   Input as delivered from Payment Processor.
  * @param array $ids
  *   Ids as Loaded by Payment Processor.
  * @param bool $loadAll
  *   Load all related objects - even where id not passed in? (allows API to call this).
  *
  * @return bool
  * @throws Exception
  */
 public function loadRelatedObjects(&$input, &$ids, $loadAll = FALSE)
 {
     if ($loadAll) {
         $ids = array_merge($this->getComponentDetails($this->id), $ids);
         if (empty($ids['contact']) && isset($this->contact_id)) {
             $ids['contact'] = $this->contact_id;
         }
     }
     if (empty($this->_component)) {
         if (!empty($ids['event'])) {
             $this->_component = 'event';
         } else {
             $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute'));
         }
     }
     // If the object is not fully populated then make sure it is - this is a more about legacy paths & cautious
     // refactoring than anything else, and has unit test coverage.
     if (empty($this->financial_type_id)) {
         $this->find(TRUE);
     }
     $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value('paymentProcessor', $ids));
     if (!$paymentProcessorID && $this->contribution_page_id) {
         $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor');
         if ($paymentProcessorID) {
             $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage;
         }
     }
     $ids['contributionType'] = $this->financial_type_id;
     $ids['financialType'] = $this->financial_type_id;
     $entities = array('contact' => 'CRM_Contact_BAO_Contact', 'contributionRecur' => 'CRM_Contribute_BAO_ContributionRecur', 'contributionType' => 'CRM_Financial_BAO_FinancialType', 'financialType' => 'CRM_Financial_BAO_FinancialType');
     foreach ($entities as $entity => $bao) {
         if (!empty($ids[$entity])) {
             $this->_relatedObjects[$entity] = new $bao();
             $this->_relatedObjects[$entity]->id = $ids[$entity];
             if (!$this->_relatedObjects[$entity]->find(TRUE)) {
                 throw new CRM_Core_Exception($entity . ' could not be loaded');
             }
         }
     }
     if (!empty($ids['contributionRecur']) && !$paymentProcessorID) {
         $paymentProcessorID = $this->_relatedObjects['contributionRecur']->payment_processor_id;
     }
     if (!empty($ids['pledge_payment'])) {
         foreach ($ids['pledge_payment'] as $key => $paymentID) {
             if (empty($paymentID)) {
                 continue;
             }
             $payment = new CRM_Pledge_BAO_PledgePayment();
             $payment->id = $paymentID;
             if (!$payment->find(TRUE)) {
                 throw new Exception("Could not find pledge payment record: " . $paymentID);
             }
             $this->_relatedObjects['pledge_payment'][] = $payment;
         }
     }
     $this->loadRelatedMembershipObjects($ids);
     if ($this->_component != 'contribute') {
         // we are in event mode
         // make sure event exists and is valid
         $event = new CRM_Event_BAO_Event();
         $event->id = $ids['event'];
         if ($ids['event'] && !$event->find(TRUE)) {
             throw new Exception("Could not find event: " . $ids['event']);
         }
         $this->_relatedObjects['event'] =& $event;
         $participant = new CRM_Event_BAO_Participant();
         $participant->id = $ids['participant'];
         if ($ids['participant'] && !$participant->find(TRUE)) {
             throw new Exception("Could not find participant: " . $ids['participant']);
         }
         $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date);
         $this->_relatedObjects['participant'] =& $participant;
         // get the payment processor id from event - this is inaccurate see CRM-16923
         // in future we should look at throwing an exception here rather than an dubious guess.
         if (!$paymentProcessorID) {
             $paymentProcessorID = $this->_relatedObjects['event']->payment_processor;
             if ($paymentProcessorID) {
                 $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromEvent;
             }
         }
     }
     if ($paymentProcessorID) {
         $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live');
         $ids['paymentProcessor'] = $paymentProcessorID;
         $this->_relatedObjects['paymentProcessor'] = $paymentProcessor;
     }
     return TRUE;
 }