Merge pull request #16093 from civicrm/5.21
[civicrm-core.git] / CRM / Core / Payment / BaseIPN.php
index 30af3434a4874cacd9bec8e53597ac7d3c46d57e..192f347e68f144e8a247f29c5a68819dc93c62b8 100644 (file)
@@ -1,27 +1,11 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
@@ -256,28 +240,14 @@ class CRM_Core_Payment_BaseIPN {
 
     if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) {
       if (!empty($memberships)) {
-        // if transaction is failed then set "Cancelled" as membership status
-        $membershipStatuses = CRM_Core_PseudoConstant::get('CRM_Member_DAO_Membership', 'status_id', [
-          'labelColumn' => 'name',
-          'flip' => 1,
-        ]);
-        // @fixme Should we cancel only Pending memberships? per cancelled()
         foreach ($memberships as $membership) {
-          if ($membership) {
-            $membership->status_id = $membershipStatuses['Cancelled'];
-            $membership->save();
-
-            //update related Memberships.
-            $params = ['status_id' => $membershipStatuses['Cancelled']];
-            CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params);
-          }
+          // @fixme Should we cancel only Pending memberships? per cancelled()
+          $this->cancelMembership($membership, $membership->status_id, FALSE);
         }
       }
 
       if ($participant) {
-        $participantParams['id'] = $participant->id;
-        $participantParams['status_id'] = 'Cancelled';
-        civicrm_api3('Participant', 'create', $participantParams);
+        $this->cancelParticipant($participant->id);
       }
     }
 
@@ -354,30 +324,15 @@ class CRM_Core_Payment_BaseIPN {
 
     if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) {
       if (!empty($memberships)) {
-        // if transaction is failed then set "Cancelled" as membership status
-        $membershipStatuses = CRM_Core_PseudoConstant::get('CRM_Member_DAO_Membership', 'status_id', [
-          'labelColumn' => 'name',
-          'flip' => 1,
-        ]);
-        // Cancel only Pending memberships
-        // CRM-18688
-        $pendingStatusId = $membershipStatuses['Pending'];
         foreach ($memberships as $membership) {
-          if ($membership && ($membership->status_id == $pendingStatusId)) {
-            $membership->status_id = $membershipStatuses['Cancelled'];
-            $membership->save();
-
-            //update related Memberships.
-            $params = ['status_id' => $membershipStatuses['Cancelled']];
-            CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params);
+          if ($membership) {
+            $this->cancelMembership($membership, $membership->status_id);
           }
         }
       }
 
       if ($participant) {
-        $participantParams['id'] = $participant->id;
-        $participantParams['status_id'] = 'Cancelled';
-        civicrm_api3('Participant', 'create', $participantParams);
+        $this->cancelParticipant($participant->id);
       }
     }
     $transaction->commit();
@@ -400,6 +355,55 @@ class CRM_Core_Payment_BaseIPN {
     return FALSE;
   }
 
+  /**
+   * Logic to cancel a participant record when the related contribution changes to failed/cancelled.
+   * @todo This is part of a bigger refactor for dev/core/issues/927 - "duplicate" functionality exists in CRM_Contribute_BAO_Contribution::cancel()
+   *
+   * @param $participantID
+   *
+   * @throws \CiviCRM_API3_Exception
+   */
+  private function cancelParticipant($participantID) {
+    // @fixme https://lab.civicrm.org/dev/core/issues/927 Cancelling membership etc is not desirable for all use-cases and we should be able to disable it
+    $participantParams['id'] = $participantID;
+    $participantParams['status_id'] = 'Cancelled';
+    civicrm_api3('Participant', 'create', $participantParams);
+  }
+
+  /**
+   * Logic to cancel a membership record when the related contribution changes to failed/cancelled.
+   * @todo This is part of a bigger refactor for dev/core/issues/927 - "duplicate" functionality exists in CRM_Contribute_BAO_Contribution::cancel()
+   * @param \CRM_Member_BAO_Membership $membership
+   * @param int $membershipStatusID
+   * @param boolean $onlyCancelPendingMembership
+   *   Do we only cancel pending memberships? OR memberships in any status? (see CRM-18688)
+   * @fixme Historically failed() cancelled membership in any status, cancelled() cancelled only pending memberships so we retain that behaviour for now.
+   *
+   */
+  private function cancelMembership($membership, $membershipStatusID, $onlyCancelPendingMembership = TRUE) {
+    // @fixme https://lab.civicrm.org/dev/core/issues/927 Cancelling membership etc is not desirable for all use-cases and we should be able to disable it
+    // Cancel only Pending memberships
+    $pendingMembershipStatusId = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending');
+    if (($membershipStatusID == $pendingMembershipStatusId) || ($onlyCancelPendingMembership == FALSE)) {
+      $cancelledMembershipStatusId = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled');
+
+      $membership->status_id = $cancelledMembershipStatusId;
+      $membership->save();
+
+      $params = ['status_id' => $cancelledMembershipStatusId];
+      CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $params);
+
+      // @todo Convert the above to API
+      // $membershipParams = [
+      //   'id' => $membership->id,
+      //   'status_id' => $cancelledMembershipStatusId,
+      // ];
+      // civicrm_api3('Membership', 'create', $membershipParams);
+      // CRM_Member_BAO_Membership::updateRelatedMemberships($membershipParams['id'], ['status_id' => $cancelledMembershipStatusId]);
+    }
+
+  }
+
   /**
    * @deprecated
    *
@@ -453,15 +457,14 @@ class CRM_Core_Payment_BaseIPN {
    * @param array $ids
    * @param array $objects
    * @param CRM_Core_Transaction $transaction
-   * @param bool $recur
    *
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function completeTransaction(&$input, &$ids, &$objects, &$transaction, $recur = FALSE) {
+  public function completeTransaction(&$input, &$ids, &$objects, &$transaction) {
     $contribution = &$objects['contribution'];
 
-    CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $transaction, $recur, $contribution);
+    CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $transaction, $contribution);
   }
 
   /**