CRM-16523 towards calling hooks throughout completetransaction
authoreileenmcnaugton <eileen@fuzion.co.nz>
Mon, 3 Aug 2015 13:06:03 +0000 (01:06 +1200)
committereileenmcnaugton <eileen@fuzion.co.nz>
Tue, 4 Aug 2015 05:30:26 +0000 (17:30 +1200)
CRM/Contribute/BAO/Contribution.php

index a7459869ebd3830128ae85302f3830d701958bf1..fe12852abbb772e158b30da007fa9a29401bd251 100644 (file)
@@ -1478,6 +1478,11 @@ LEFT JOIN  civicrm_contribution contribution ON ( componentPayment.contribution_
   /**
    * Update contribution as well as related objects.
    *
+   * @deprecated
+   *
+   * Use api contribute.completetransaction
+   * For failures use failPayment (preferably exposing by api in the process).
+   *
    * @param array $params
    * @param bool $processContributionObject
    *
@@ -4001,12 +4006,14 @@ WHERE con.id = {$contributionId}
       if (!empty($memberships)) {
         $membershipsUpdate = array();
         foreach ($memberships as $membershipTypeIdKey => $membership) {
+          $membershipParams = (array) $membership;
           if ($membership) {
             $format = '%Y%m%d';
 
-            $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membership->contact_id,
-              $membership->membership_type_id,
-              $membership->is_test, $membership->id
+            $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membershipParams['contact_id'],
+              $membershipParams['membership_type_id'],
+              $membershipParams['is_test'],
+              $membershipParams['id']
             );
 
             // CRM-8141 update the membership type with the value recorded in log when membership created/renewed
@@ -4014,22 +4021,18 @@ WHERE con.id = {$contributionId}
             $sql = "
 SELECT    membership_type_id
 FROM      civicrm_membership_log
-WHERE     membership_id=$membership->id
+WHERE     membership_id={$membershipParams['id']}
 ORDER BY  id DESC
 LIMIT 1;";
-            $dao = new CRM_Core_DAO();
-            $dao->query($sql);
+            $dao = CRM_Core_DAO::executeQuery($sql);
             if ($dao->fetch()) {
               if (!empty($dao->membership_type_id)) {
-                $membership->membership_type_id = $dao->membership_type_id;
-                $membership->save();
+                $membershipParams['membership_type_id'] = $dao->membership_type_id;
               }
-              // else fall back to using current membership type
             }
-            // else fall back to using current membership type
             $dao->free();
 
-            $num_terms = $contribution->getNumTermsByContributionAndMembershipType($membership->membership_type_id, $primaryContributionID);
+            $num_terms = $contribution->getNumTermsByContributionAndMembershipType($membershipParams['membership_type_id'], $primaryContributionID);
             if ($currentMembership) {
               /*
                * Fixed FOR CRM-4433
@@ -4040,14 +4043,16 @@ LIMIT 1;";
 
               // @todo - we should pass membership_type_id instead of null here but not
               // adding as not sure of testing
-              $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id,
+              $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membershipParams['id'],
                 $changeDate, NULL, $num_terms
               );
 
-              $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
+              $dates['join_date'] = $currentMembership['join_date'];
             }
             else {
-              $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->membership_type_id, NULL, NULL, NULL, $num_terms);
+              $membershipParams = array_merge($membershipParams,
+                CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipParams['membership_type_id'], NULL, NULL, NULL, $num_terms)
+              );
             }
 
             //get the status for membership.
@@ -4056,27 +4061,19 @@ LIMIT 1;";
               $dates['join_date'],
               'today',
               TRUE,
-              $membership->membership_type_id,
-              (array) $membership
+              $membershipParams['membership_type_id'],
+              $membershipParams
             );
 
-            $formatedParams = array(
-              'status_id' => CRM_Utils_Array::value('id', $calcStatus, 2),
-              'join_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $dates), $format),
-              'start_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $dates), $format),
-              'end_date' => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $dates), $format),
-            );
+            $membershipParams['status_id'] = CRM_Utils_Array::value('id', $calcStatus, 2);
             //we might be renewing membership,
             //so make status override false.
-            $formatedParams['is_override'] = FALSE;
-            $membership->copyValues($formatedParams);
-            $membership->save();
+            $membershipParams['is_override'] = FALSE;
+            civicrm_api3('Membership', 'create', $membershipParams);
 
-            //updating the membership log
-            $membershipLog = array();
-            $membershipLog = $formatedParams;
+            $membershipLog = $membershipParams;
 
-            $logStartDate = $formatedParams['start_date'];
+            $logStartDate = $membershipParams['start_date'];
             if (!empty($dates['log_start_date'])) {
               $logStartDate = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
               $logStartDate = CRM_Utils_Date::isoToMysql($logStartDate);
@@ -4091,7 +4088,7 @@ LIMIT 1;";
             CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray);
 
             //update related Memberships.
-            CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formatedParams);
+            CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $membershipParams);
 
             //update the membership type key of membership relatedObjects array
             //if it has changed after membership update