[REF] Extract code to get the from address for a recurring contribution.
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Sep 2021 04:56:15 +0000 (16:56 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Sep 2021 05:19:41 +0000 (17:19 +1200)
This makes the retrieval generally available, since is it part of the data model,
rather than tied to the form. It could possibly be a token, or at least retrievable on
the recurring edit workflow template - but that is beyond the scope of this

CRM/Contribute/BAO/ContributionRecur.php
CRM/Contribute/Form/UpdateSubscription.php
tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php

index b468a8fa37c8d3730fe5672eec0287d211e0af55..2b1bfb05fbb894f0f8f112ff6fb618a730b72b70 100644 (file)
@@ -1085,4 +1085,32 @@ INNER JOIN civicrm_contribution       con ON ( con.id = mp.contribution_id )
     return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
   }
 
+  /**
+   * Get the from address to use for the recurring contribution.
+   *
+   * This uses the contribution page id, if there is one, or the default domain one.
+   *
+   * @param int $id
+   *   Recurring contribution ID.
+   *
+   * @internal
+   *
+   * @return string
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public static function getRecurFromAddress(int $id): string {
+    $details = Contribution::get(FALSE)
+      ->addWhere('contribution_recur_id', '=', $id)
+      ->addWhere('contribution_page_id', 'IS NOT NULL')
+      ->addSelect('contribution_page_id.receipt_from_name', 'contribution_page_id.receipt_from_email')
+      ->addOrderBy('receive_date', 'DESC')
+      ->execute()->first();
+    if (empty($details['contribution_page_id.receipt_from_email'])) {
+      $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
+      return "$domainValues[0] <$domainValues[1]>";
+    }
+    return '"' . $details['contribution_page_id.receipt_from_name'] . '" <' . $details['contribution_page_id.receipt_from_email'] . '>';
+  }
+
 }
index 1766a613da16e339fc76e99ee0c6da03c200a578..1d305c8a280fa0c33936c9d29056d13bdc4d884b 100644 (file)
@@ -192,6 +192,9 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_Contrib
 
   /**
    * Called after the user submits the form.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
    */
   public function postProcess() {
     // store the submitted values in an array
@@ -203,7 +206,7 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_Contrib
     }
 
     // if this is an update of an existing recurring contribution, pass the ID
-    $params['id'] = $this->_subscriptionDetails->recur_id;
+    $params['id'] = $this->getContributionRecurID();
     $message = '';
 
     $params['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;
@@ -279,21 +282,7 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_Contrib
       CRM_Activity_BAO_Activity::create($activityParams);
 
       if (!empty($params['is_notify'])) {
-        // send notification
-        if ($this->_subscriptionDetails->contribution_page_id) {
-          CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
-            $this->_subscriptionDetails->contribution_page_id, $value, [
-              'title',
-              'receipt_from_name',
-              'receipt_from_email',
-            ]
-          );
-          $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>';
-        }
-        else {
-          $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
-          $receiptFrom = "$domainValues[0] <$domainValues[1]>";
-        }
+        $receiptFrom = CRM_Contribute_BAO_ContributionRecur::getRecurFromAddress($this->getContributionRecurID());
 
         [$donorDisplayName, $donorEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID);
 
@@ -337,4 +326,13 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_Contrib
     }
   }
 
+  /**
+   * Get the recurring contribution ID.
+   *
+   * @return int
+   */
+  protected function getContributionRecurID(): int {
+    return $this->_subscriptionDetails->recur_id;
+  }
+
 }
index 6829085dc037ca8b4d2305d46b9286b054ec0cba..9c97d094652b6b675a2a759cac5c0db4f79502b6 100644 (file)
@@ -17,7 +17,7 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
   /**
    * Test the mail sent on update.
    *
-   * @throws \CRM_Core_Exception
+   * @throws \CRM_Core_Exception|\API_Exception
    */
   public function testMail(): void {
     $mut = new CiviMailUtils($this, TRUE);
@@ -44,14 +44,14 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
   public function getExpectedMailStrings(): array {
     return [
       'MIME-Version: 1.0',
-      'From: FIXME <info@EXAMPLE.ORG>',
+      'From: "Bob" <bob@example.org>',
       'To: Anthony Anderson <anthony_anderson@civicrm.org>',
       'Subject: Recurring Contribution Update Notification - Mr. Anthony Anderson II',
-      'Return-Path: info@EXAMPLE.ORG',
+      'Return-Path: bob@example.org',
       'Dear Anthony,',
       'Your recurring contribution has been updated as requested:',
       'Recurring contribution is for $ 10.00, every 1 month(s) for 12 installments.',
-      'If you have questions please contact us at FIXME <info@EXAMPLE.ORG>.',
+      'If you have questions please contact us at "Bob" <bob@example.org>.',
     ];
   }
 
@@ -77,7 +77,12 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
       'contribution_recur_id' => $this->getContributionRecurID(),
       'financial_type_id' => 'Donation',
       'total_amount' => 10,
-      'api.Payment.create' => ['total_amount' => 10, 'payment_processor_id' => $this->paymentProcessorId],
+      'contribution_page_id' => $this->getContributionPageID(),
+      'api.Payment.create' => [
+        'total_amount' => 10,
+        'payment_processor_id' => $this->paymentProcessorId,
+        'is_send_contribution_notification' => FALSE,
+      ],
     ]);
   }
 
@@ -99,4 +104,20 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
     return $this->ids['ContributionRecur'][0];
   }
 
+  /**
+   * Get a contribution page id.
+   *
+   * @return int
+   */
+  public function getContributionPageID(): int {
+    if (!isset($this->ids['ContributionPage'][0])) {
+      $this->ids['ContributionPage'][0] = $this->callAPISuccess('ContributionPage', 'create', [
+        'receipt_from_name' => 'Bob',
+        'receipt_from_email' => 'bob@example.org',
+        'financial_type_id' => 'Donation',
+      ])['id'];
+    }
+    return $this->ids['ContributionPage'][0];
+  }
+
 }