[REF] Fix ContributionPage::recurringNotify to use apiv4, simplify params
authoreileen <emcnaughton@wikimedia.org>
Sun, 21 Feb 2021 10:33:25 +0000 (23:33 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 22 Feb 2021 04:55:47 +0000 (17:55 +1300)
Note that the unit test that covers this is
CRM_Core_Payment_AuthorizeNetIPNTest.testIPNPaymentMembershipRecurSuccessNoLeakage

CRM/Contribute/BAO/ContributionPage.php
CRM/Contribute/BAO/ContributionRecur.php
CRM/Core/Payment/AuthorizeNetIPN.php
CRM/Core/Payment/PayPalIPN.php
CRM/Core/Payment/PayPalProIPN.php

index 6b1854f0f3b956b864544f2c69e2fb826e0b34e7..94da29ee8afa96abce1a76018382e46a97e72f07 100644 (file)
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\Contribution;
+
 /**
  * This class contains Contribution Page related functions.
  */
@@ -309,7 +311,7 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
             $userID = $values['related_contact'] ?? NULL;
           }
         }
-        list($values['customPre_grouptitle'], $values['customPre']) = self::getProfileNameAndFields($preID, $userID, $params['custom_pre_id']);
+        [$values['customPre_grouptitle'], $values['customPre']] = self::getProfileNameAndFields($preID, $userID, $params['custom_pre_id']);
       }
       $userID = $contactID;
       if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) {
@@ -507,42 +509,39 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
   /**
    * Send the emails for Recurring Contribution Notification.
    *
+   * @param int $contributionID
    * @param string $type
    *   TxnType.
-   * @param int $contactID
-   *   Contact id for contributor.
-   * @param int $pageID
    *   Contribution page id.
    * @param object $recur
    *   Object of recurring contribution table.
    * @param bool|object $autoRenewMembership is it a auto renew membership.
+   *
+   * @throws \API_Exception
    */
-  public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
-    $value = [];
-    $isEmailReceipt = FALSE;
-    if ($pageID) {
-      CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, [
-        'title',
-        'is_email_receipt',
-        'receipt_from_name',
-        'receipt_from_email',
-        'cc_receipt',
-        'bcc_receipt',
-      ]);
-      $isEmailReceipt = $value[$pageID]['is_email_receipt'] ?? NULL;
-    }
-    elseif ($recur->id) {
-      // This means we are coming from back-office - ie. no page ID, but recurring.
-      // Ideally this information would be passed into the function clearly rather than guessing by convention.
-      $isEmailReceipt = TRUE;
-    }
-
-    if ($isEmailReceipt) {
-      if ($pageID) {
-        $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
-
-        $receiptFromName = $value[$pageID]['receipt_from_name'];
-        $receiptFromEmail = $value[$pageID]['receipt_from_email'];
+  public static function recurringNotify($contributionID, $type, $recur, $autoRenewMembership = FALSE): void {
+    $contribution = Contribution::get(FALSE)
+      ->addWhere('id', '=', $contributionID)
+      ->setSelect([
+        'contribution_page_id',
+        'contact_id',
+        'contribution_recur_id',
+        'contribution_recur.is_email_receipt',
+        'contribution_page.title',
+        'contribution_page.is_email_receipt',
+        'contribution_page.receipt_from_name',
+        'contribution_page.receipt_from_email',
+        'contribution_page.cc_receipt',
+        'contribution_page.bcc_receipt',
+      ])
+      ->execute()->first();
+
+    if ($contribution['contribution_recur.is_email_receipt'] || $contribution['contribution_page.is_email_receipt']) {
+      if ($contribution['contribution_page.receipt_from_email']) {
+        $receiptFrom = '"' . $contribution['contribution_page.receipt_from_name'] . '" <' . $contribution['contribution_page.receipt_from_email'] . '>';
+
+        $receiptFromName = $contribution['contribution_page.receipt_from_name'];
+        $receiptFromEmail = $contribution['contribution_page.receipt_from_email'];
       }
       else {
         $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
@@ -551,11 +550,11 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
         $receiptFromEmail = $domainValues[1];
       }
 
-      list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
+      [$displayName, $email] = CRM_Contact_BAO_Contact_Location::getEmailDetails($contribution['contact_id'], FALSE);
       $templatesParams = [
         'groupName' => 'msg_tpl_workflow_contribution',
         'valueName' => 'contribution_recurring_notify',
-        'contactId' => $contactID,
+        'contactId' => $contribution['contact_id'],
         'tplParams' => [
           'recur_frequency_interval' => $recur->frequency_interval,
           'recur_frequency_unit' => $recur->frequency_unit,
@@ -574,10 +573,8 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
         'toEmail' => $email,
       ];
       //CRM-13811
-      if ($pageID) {
-        $templatesParams['cc'] = $value[$pageID]['cc_receipt'] ?? NULL;
-        $templatesParams['bcc'] = $value[$pageID]['bcc_receipt'] ?? NULL;
-      }
+      $templatesParams['cc'] = $contribution['contribution_page.cc_receipt'];
+      $templatesParams['bcc'] = $contribution['contribution_page.cc_receipt'];
       if ($recur->id) {
         // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
         // like in PaypalPro, & therefore we set it here additionally.
index 161cac49bb89622187e2fa3829f6a90f78eff654..5762a2a0499bff651db8bb100df1b823acad1c20 100644 (file)
@@ -559,9 +559,9 @@ INNER JOIN civicrm_contribution       con ON ( con.id = mp.contribution_id )
       }
 
       //send recurring Notification email for user
-      CRM_Contribute_BAO_ContributionPage::recurringNotify($isFirstOrLastRecurringPayment,
-        $ids['contact'],
-        $ids['contributionPage'],
+      CRM_Contribute_BAO_ContributionPage::recurringNotify(
+        $ids['contribution'],
+        $isFirstOrLastRecurringPayment,
         $recur,
         $autoRenewMembership
       );
index 42a744a9b467a55fac1a43a0d166231901afc8cc..70140f6c4856dfd1a236418d578a0d10d060f4d4 100644 (file)
@@ -88,9 +88,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
 
       if ($isFirstOrLastRecurringPayment) {
         //send recurring Notification email for user
-        CRM_Contribute_BAO_ContributionPage::recurringNotify(TRUE,
-          $contributionRecur->contact_id,
-          $ids['contributionPage'],
+        CRM_Contribute_BAO_ContributionPage::recurringNotify($contributionID, TRUE,
           $contributionRecur,
           (bool) $this->getMembershipID($contributionID, $contributionRecur->id)
         );
index 06888f4fce9933b7a15d1e058d2f61f35e2e5f32..6c1d56db99f7e648242cb09c64dbdf3d54fd13cd 100644 (file)
@@ -348,9 +348,9 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
         $this->recur($input, $ids, $contributionRecur, $contribution, $first);
         if ($this->getFirstOrLastInSeriesStatus()) {
           //send recurring Notification email for user
-          CRM_Contribute_BAO_ContributionPage::recurringNotify($this->getFirstOrLastInSeriesStatus(),
-            $ids['contact'],
-            $ids['contributionPage'],
+          CRM_Contribute_BAO_ContributionPage::recurringNotify(
+            $ids['contribution'],
+            $this->getFirstOrLastInSeriesStatus(),
             $contributionRecur,
             !empty($ids['membership'])
           );
index 4789c67d35d6a8f2b7e1064d865d75b2175001bd..0ce996e222a1f1088524d541aca142b8cff831e3 100644 (file)
@@ -258,9 +258,9 @@ class CRM_Core_Payment_PayPalProIPN extends CRM_Core_Payment_BaseIPN {
 
     if ($sendNotification) {
       //send recurring Notification email for user
-      CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus,
-        $ids['contact'],
-        $ids['contributionPage'],
+      CRM_Contribute_BAO_ContributionPage::recurringNotify(
+        $ids['contribution'],
+        $subscriptionPaymentStatus,
         $recur,
         !empty($ids['membership'])
       );