From 8159df5f76906dd62f1be7ae1fbd00854d050cbb Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 3 Sep 2021 16:56:15 +1200 Subject: [PATCH] [REF] Extract code to get the from address for a recurring contribution. 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 | 28 +++++++++++++++++ CRM/Contribute/Form/UpdateSubscription.php | 30 +++++++++--------- .../Form/UpdateSubscriptionTest.php | 31 ++++++++++++++++--- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index b468a8fa37c8..2b1bfb05fbb8 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -1085,4 +1085,32 @@ public static function buildOptions($fieldName, $context = NULL, $props = []) { 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'] . '>'; + } + } diff --git a/CRM/Contribute/Form/UpdateSubscription.php b/CRM/Contribute/Form/UpdateSubscription.php index 1766a613da16..1d305c8a280f 100644 --- a/CRM/Contribute/Form/UpdateSubscription.php +++ b/CRM/Contribute/Form/UpdateSubscription.php @@ -192,6 +192,9 @@ public function buildQuickForm() { /** * 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 @@ public function postProcess() { } // 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 @@ public function postProcess() { 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 @@ public function postProcess() { } } + /** + * Get the recurring contribution ID. + * + * @return int + */ + protected function getContributionRecurID(): int { + return $this->_subscriptionDetails->recur_id; + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php index 6829085dc037..9c97d094652b 100644 --- a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php @@ -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 @@ public function testMail(): void { public function getExpectedMailStrings(): array { return [ 'MIME-Version: 1.0', - 'From: FIXME ', + 'From: "Bob" ', 'To: Anthony Anderson ', '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 .', + 'If you have questions please contact us at "Bob" .', ]; } @@ -77,7 +77,12 @@ public function addContribution(): void { '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 @@ public function getContributionRecurID(): int { 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]; + } + }