From: Eileen McNaughton Date: Thu, 20 May 2021 01:48:18 +0000 (+1200) Subject: Extract getContributionObject X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6c19604880fde0a04bece3a741603c470da1b697;p=civicrm-core.git Extract getContributionObject --- diff --git a/CRM/Core/Payment/PayPalProIPN.php b/CRM/Core/Payment/PayPalProIPN.php index 51f97288c3..5c8e8f69b2 100644 --- a/CRM/Core/Payment/PayPalProIPN.php +++ b/CRM/Core/Payment/PayPalProIPN.php @@ -51,7 +51,12 @@ class CRM_Core_Payment_PayPalProIPN extends CRM_Core_Payment_BaseIPN { */ protected $contributionRecurObject; - + /** + * Contribution object. + * + * @var \CRM_Contribute_BAO_Contribution + */ + protected $contributionObject; /** * Contribution ID. * @@ -463,7 +468,7 @@ class CRM_Core_Payment_PayPalProIPN extends CRM_Core_Payment_BaseIPN { $this->_component = $input['component'] = self::getValue('m'); $input['invoice'] = self::getValue('i', TRUE); // get the contribution and contact ids from the GET params - $ids['contact'] = self::getValue('c', TRUE); + $ids['contact'] = $this->getContactID(); $ids['contribution'] = $this->getContributionID(); $this->getInput($input); @@ -504,14 +509,7 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr if (!$paymentProcessorID) { $paymentProcessorID = self::getPayPalPaymentProcessorID(); } - - // Check if the contribution exists - // make sure contribution exists and is valid - $contribution = new CRM_Contribute_BAO_Contribution(); - $contribution->id = $ids['contribution']; - if (!$contribution->find(TRUE)) { - throw new CRM_Core_Exception('Failure: Could not find contribution record for ' . (int) $contribution->id, NULL, ['context' => "Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE)]); - } + $contribution = $this->getContributionObject(); // make sure contact exists and is valid // use the contact id from the contribution record as the id in the IPN may not be valid anymore. @@ -665,12 +663,7 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr // Check if the contribution exists // make sure contribution exists and is valid - $contribution = new CRM_Contribute_BAO_Contribution(); - $contribution->id = $this->getContributionID(); - if (!$contribution->find(TRUE)) { - throw new CRM_Core_Exception('Failure: Could not find contribution record for ' . (int) $contribution->id, NULL, ['context' => "Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE)]); - } - + $contribution = $this->getContributionObject(); $objects['contribution'] = &$contribution; // CRM-19478: handle oddity when p=null is set in place of contribution page ID, @@ -721,4 +714,32 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr return $this->contributionRecurObject; } + /** + * @return \CRM_Contribute_BAO_Contribution + * @throws \CRM_Core_Exception + */ + protected function getContributionObject(): CRM_Contribute_BAO_Contribution { + if (!$this->contributionObject) { + // Check if the contribution exists + // make sure contribution exists and is valid + $contribution = new CRM_Contribute_BAO_Contribution(); + $contribution->id = $this->getContributionID(); + if (!$contribution->find(TRUE)) { + throw new CRM_Core_Exception('Failure: Could not find contribution record'); + } + $this->contributionObject = $contribution; + } + return $this->contributionObject; + } + + /** + * Get the relevant contact ID. + * + * @return int + * @throws \CRM_Core_Exception + */ + protected function getContactID(): int { + return $this->getValue('c', TRUE); + } + }