Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Extract code to get the from address for a recurring contribution. #21357

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] . '>';
}

}
30 changes: 14 additions & 16 deletions CRM/Contribute/Form/UpdateSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -337,4 +326,13 @@ public function postProcess() {
}
}

/**
* Get the recurring contribution ID.
*
* @return int
*/
protected function getContributionRecurID(): int {
return $this->_subscriptionDetails->recur_id;
}

}
31 changes: 26 additions & 5 deletions tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -44,14 +44,14 @@ public function testMail(): void {
public function getExpectedMailStrings(): array {
return [
'MIME-Version: 1.0',
'From: FIXME <[email protected]>',
'From: "Bob" <[email protected]>',
'To: Anthony Anderson <[email protected]>',
'Subject: Recurring Contribution Update Notification - Mr. Anthony Anderson II',
'Return-Path: [email protected]',
'Return-Path: [email protected]',
'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 <[email protected]>.',
'If you have questions please contact us at "Bob" <[email protected]>.',
];
}

Expand All @@ -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,
],
]);
}

Expand All @@ -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' => '[email protected]',
'financial_type_id' => 'Donation',
])['id'];
}
return $this->ids['ContributionPage'][0];
}

}