Merge pull request #18728 from eileenmcnaughton/aip
[civicrm-core.git] / CRM / Core / Payment / BaseIPN.php
index d3f56e9f70fcf0365f5bc03756de9da342c8b540..614568b51889bbecade7c5dd2a84e7677f68ac86 100644 (file)
@@ -92,9 +92,7 @@ class CRM_Core_Payment_BaseIPN {
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->id = $ids['contribution'];
     if (!$contribution->find(TRUE)) {
-      CRM_Core_Error::debug_log_message("Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE));
-      echo "Failure: Could not find contribution record for {$contribution->id}<p>";
-      return FALSE;
+      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)]);
     }
 
     // make sure contact exists and is valid
@@ -145,51 +143,16 @@ class CRM_Core_Payment_BaseIPN {
    * @param array $objects
    * @param bool $required
    * @param int $paymentProcessorID
-   * @param array $error_handling
    *
    * @return bool|array
+   * @throws \CRM_Core_Exception
    */
-  public function loadObjects($input, &$ids, &$objects, $required, $paymentProcessorID, $error_handling = NULL) {
-    if (empty($error_handling)) {
-      // default options are that we log an error & echo it out
-      // note that we should refactor this error handling into error code @ some point
-      // but for now setting up enough separation so we can do unit tests
-      $error_handling = [
-        'log_error' => 1,
-        'echo_error' => 1,
-      ];
-    }
+  public function loadObjects($input, &$ids, &$objects, $required, $paymentProcessorID) {
+    $contribution = &$objects['contribution'];
     $ids['paymentProcessor'] = $paymentProcessorID;
-    if (is_a($objects['contribution'], 'CRM_Contribute_BAO_Contribution')) {
-      $contribution = &$objects['contribution'];
-    }
-    else {
-      //legacy support - functions are 'used' to be able to pass in a DAO
-      $contribution = new CRM_Contribute_BAO_Contribution();
-      $contribution->id = $ids['contribution'] ?? NULL;
-      $contribution->find(TRUE);
-      $objects['contribution'] = &$contribution;
-    }
-    try {
-      $success = $contribution->loadRelatedObjects($input, $ids);
-      if ($required && empty($contribution->_relatedObjects['paymentProcessor'])) {
-        throw new CRM_Core_Exception("Could not find payment processor for contribution record: " . $contribution->id);
-      }
-    }
-    catch (Exception $e) {
-      $success = FALSE;
-      if (!empty($error_handling['log_error'])) {
-        CRM_Core_Error::debug_log_message($e->getMessage());
-      }
-      if (!empty($error_handling['echo_error'])) {
-        echo $e->getMessage();
-      }
-      if (!empty($error_handling['return_error'])) {
-        return [
-          'is_error' => 1,
-          'error_message' => ($e->getMessage()),
-        ];
-      }
+    $success = $contribution->loadRelatedObjects($input, $ids);
+    if ($required && empty($contribution->_relatedObjects['paymentProcessor'])) {
+      throw new CRM_Core_Exception("Could not find payment processor for contribution record: " . $contribution->id);
     }
     $objects = array_merge($objects, $contribution->_relatedObjects);
     return $success;
@@ -199,13 +162,11 @@ class CRM_Core_Payment_BaseIPN {
    * Set contribution to failed.
    *
    * @param array $objects
-   * @param object $transaction
-   * @param array $input
    *
    * @return bool
-   * @throws \CiviCRM_API3_Exception
+   * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
    */
-  public function failed(&$objects, $transaction = NULL, $input = []) {
+  public function failed($objects) {
     $contribution = &$objects['contribution'];
     $memberships = [];
     if (!empty($objects['membership'])) {
@@ -243,9 +204,6 @@ class CRM_Core_Payment_BaseIPN {
       $this->cancelParticipant($participant->id);
     }
 
-    if ($transaction) {
-      $transaction->commit();
-    }
     Civi::log()->debug("Setting contribution status to Failed");
     return TRUE;
   }
@@ -272,13 +230,11 @@ class CRM_Core_Payment_BaseIPN {
    * Process cancelled payment outcome.
    *
    * @param array $objects
-   * @param CRM_Core_Transaction $transaction
-   * @param array $input
    *
    * @return bool
-   * @throws \CiviCRM_API3_Exception
+   * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
    */
-  public function cancelled(&$objects, $transaction = NULL, $input = []) {
+  public function cancelled($objects) {
     $contribution = &$objects['contribution'];
     $memberships = [];
     if (!empty($objects['membership'])) {
@@ -301,7 +257,6 @@ class CRM_Core_Payment_BaseIPN {
     ]);
     $contribution->contribution_status_id = $contributionStatuses['Cancelled'];
     $contribution->cancel_date = self::$_now;
-    $contribution->cancel_reason = $input['reasonCode'] ?? NULL;
     $contribution->save();
 
     // Add line items for recurring payments.
@@ -328,9 +283,6 @@ class CRM_Core_Payment_BaseIPN {
       $this->cancelParticipant($participant->id);
     }
 
-    if ($transaction) {
-      $transaction->commit();
-    }
     Civi::log()->debug("Setting contribution status to Cancelled");
     return TRUE;
   }
@@ -459,11 +411,12 @@ class CRM_Core_Payment_BaseIPN {
    * @throws \CiviCRM_API3_Exception
    */
   public function completeTransaction($input, $ids, $objects) {
+    CRM_Core_Error::deprecatedFunctionWarning('Use Payment.create api');
     CRM_Contribute_BAO_Contribution::completeOrder($input, [
       'related_contact' => $ids['related_contact'] ?? NULL,
       'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
       'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
-    ], $objects);
+    ], $objects['contribution']);
   }
 
   /**