CRM-16737 4.7 version, only payment_status_id accepted as return parameter from payme...
[civicrm-core.git] / CRM / Member / BAO / Membership.php
index d5f82230a163bbae64f538d0a42c649949fb8f7c..bc4b216bdaee5d068c9ef4e720dc8bd454d37c88 100644 (file)
@@ -311,7 +311,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
     $params['membership_id'] = $membership->id;
     if (isset($ids['membership'])) {
       $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment',
-        $ids['membership'],
+        $membership->id,
         'contribution_id',
         'membership_id'
       );
@@ -1123,7 +1123,7 @@ AND civicrm_membership.is_test = %2";
    *
    * @param bool $isProcessSeparateMembershipTransaction
    *
-   * @param int $defaultContributionTypeID
+   * @param int $financialTypeID
    * @param array $membershipLineItems
    *   Line items specific to membership payment that is separate to contribution.
    * @param bool $isPayLater
@@ -1134,23 +1134,11 @@ AND civicrm_membership.is_test = %2";
   public static function postProcessMembership(
     $membershipParams, $contactID, &$form, $premiumParams,
     $customFieldsFormatted = NULL, $includeFieldTypes = NULL, $membershipDetails, $membershipTypeIDs, $isPaidMembership, $membershipID,
-    $isProcessSeparateMembershipTransaction, $defaultContributionTypeID, $membershipLineItems, $isPayLater, $isPending) {
+    $isProcessSeparateMembershipTransaction, $financialTypeID, $membershipLineItems, $isPayLater, $isPending) {
     $result = $membershipContribution = NULL;
     $isTest = CRM_Utils_Array::value('is_test', $membershipParams, FALSE);
     $errors = $createdMemberships = $paymentResult = array();
 
-    //@todo move this into the calling function & pass in the correct financialTypeID
-    if (isset($paymentParams['financial_type'])) {
-      $financialTypeID = $paymentParams['financial_type'];
-    }
-    else {
-      $financialTypeID = $defaultContributionTypeID;
-    }
-
-    if (CRM_Utils_Array::value('membership_source', $form->_params)) {
-      $membershipParams['contribution_source'] = $form->_params['membership_source'];
-    }
-
     if ($isPaidMembership) {
       if ($isProcessSeparateMembershipTransaction) {
         // If we have 2 transactions only one can use the invoice id.
@@ -1320,7 +1308,9 @@ AND civicrm_membership.is_test = %2";
     }
 
     if ($form->_contributeMode == 'direct') {
-      if (CRM_Utils_Array::value('contribution_status_id', $paymentResult) == 1) {
+      if (CRM_Utils_Array::value('payment_status_id', $paymentResult) == 1) {
+        // Refer to CRM-16737. Payment processors 'should' return payment_status_id
+        // to denote the outcome of the transaction.
         try {
           civicrm_api3('contribution', 'completetransaction', array(
             'id' => $paymentResult['contribution']->id,
@@ -1334,6 +1324,8 @@ AND civicrm_membership.is_test = %2";
           CRM_Core_Error::debug_log_message('contribution ' . $membershipContribution->id . ' not completed with trxn_id ' . $membershipContribution->trxn_id . ' and message ' . $e->getMessage());
         }
       }
+      // Do not send an email if Recurring transaction is done via Direct Mode
+      // Email will we sent when the IPN is received.
       return;
     }
 
@@ -1752,14 +1744,23 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
   }
 
   /**
+   * Build an array of available membership types.
+   *
    * @param CRM_Core_Form $form
    * @param int $membershipTypeID
+   * @param bool $activeOnly
+   *   Do we only want active ones?
+   *   (probably this should default to TRUE but as a newly added parameter we are leaving default b
+   *   behaviour unchanged).
    *
    * @return array
    */
-  public static function &buildMembershipTypeValues(&$form, $membershipTypeID = NULL) {
+  public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL, $activeOnly = FALSE) {
     $whereClause = " WHERE domain_id = " . CRM_Core_Config::domainID();
 
+    if ($activeOnly) {
+      $whereClause .= " AND is_active = 1 ";
+    }
     if (is_array($membershipTypeID)) {
       $allIDs = implode(',', $membershipTypeID);
       $whereClause .= " AND id IN ( $allIDs )";
@@ -1790,6 +1791,8 @@ FROM   civicrm_membership_type
       'relationship_type_id',
       'relationship_direction',
       'max_related',
+      'duration_unit',
+      'duration_interval',
     );
 
     while ($dao->fetch()) {
@@ -2013,9 +2016,7 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
   public static function processSecondaryFinancialTransaction($contactID, &$form, $tempParams, $isTest, $lineItems, $minimumFee, $financialTypeID) {
     $financialType = new CRM_Financial_DAO_FinancialType();
     $financialType->id = $financialTypeID;
-    if (!$financialType->find(TRUE)) {
-      CRM_Core_Error::fatal(ts("Could not find a system table"));
-    }
+    $financialType->find(TRUE);
     $tempParams['amount'] = $minimumFee;
     $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE));
 
@@ -2713,14 +2714,14 @@ WHERE      civicrm_membership.is_test = 0";
    * @param CRM_Core_Form $qf
    * @param array $membershipType
    *   Array with membership type and organization.
-   * @param int $priceSetId
    *
+   * @return int $priceSetId
    */
-  public static function createLineItems(&$qf, $membershipType, &$priceSetId) {
+  public static function createLineItems(&$qf, $membershipType) {
     $qf->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
-    if ($priceSetId) {
-      $qf->_priceSet = $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
-    }
+    $qf->_priceSet = $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
+
+    // The name of the price field corresponds to the membership_type organization contact.
     $editedFieldParams = array(
       'price_set_id' => $priceSetId,
       'name' => $membershipType[0],
@@ -2747,6 +2748,7 @@ WHERE      civicrm_membership.is_test = 0";
 
     $fieldID = key($qf->_priceSet['fields']);
     $qf->_params['price_' . $fieldID] = CRM_Utils_Array::value('id', $editedResults);
+    return $priceSetId;
   }
 
   /**