Skip to content

Commit

Permalink
Extract code to set isEmailReceipt in Contribution.completeOrder
Browse files Browse the repository at this point in the history
This consolidates all the code in the completeOrder that determines whether or not to send an email receipt.

Note that
- testCompleteTransactionWithEmailReceiptInput tests that input is respected
- for events the event::sendMail function will not send unless it's configured for the event - which
explains our final default of TRUE (testCompleteTransactionWithParticipantRecord)
- testCompleteTransaction covers the back-office case which also defaults to true
- I figure the cost of reloading the value for contribution_recur is pretty cheap compared to the confusion of passing values around
- testpayLater covers contribution_page with is_email_receipt
  • Loading branch information
eileenmcnaughton committed Aug 2, 2020
1 parent a12be26 commit 1e3f201
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

use Civi\Api4\Activity;
use Civi\Api4\ContributionPage;
use Civi\Api4\ContributionRecur;

/**
*
Expand Down Expand Up @@ -1345,6 +1347,38 @@ protected static function getContributionTransactionInformation($contributionId,
return $rows;
}

/**
* Should an email receipt be sent for this contribution.
*
* @param array $input
* @param int $contributionPageID
* @param int $recurringContributionID
*
* @return bool
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
protected static function isEmailReceipt(array $input, $contributionPageID, $recurringContributionID): bool {
if (isset($input['is_email_receipt'])) {
return (bool) $input['is_email_receipt'];
}
if ($recurringContributionID) {
//CRM-13273 - is_email_receipt setting on recurring contribution should take precedence over contribution page setting
// but CRM-16124 if $input['is_email_receipt'] is set then that should not be overridden.
// dev/core#1245 this maybe not the desired effect because the default value for is_email_receipt is set to 0 rather than 1 in
// Instance that had the table added via an upgrade in 4.1
// see also https://github.com/civicrm/civicrm-svn/commit/7f39befd60bc735408d7866b02b3ac7fff1d4eea#diff-9ad8e290180451a2d6eacbd3d1ca7966R354
// https://lab.civicrm.org/dev/core/issues/1245
return (bool) ContributionRecur::get(FALSE)->setWhere(['id' => $recurringContributionID])->addSelect('is_email_receipt')->execute()->first()['is_email_receipt'];
}
if ($contributionPageID) {
return (bool) ContributionPage::get(FALSE)->setWhere(['id' => $contributionPageID])->addSelect('is_email_receipt')->execute()->first()['is_email_receipt'];
}
// This would be the case for backoffice (where is_email_receipt is not passed in) or events, where Event::sendMail will filter
// again anyway.
return TRUE;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -4489,25 +4523,7 @@ public static function completeOrder($input, &$ids, $objects, $transaction = NUL
self::repeatTransaction($contribution, $input, $contributionParams, $paymentProcessorId);
$contributionParams['financial_type_id'] = $contribution->financial_type_id;

$values = [];

if ($input['component'] == 'contribute') {
if ($contribution->contribution_page_id) {
CRM_Contribute_BAO_ContributionPage::setValues($contribution->contribution_page_id, $values);
// The only thing we use from this is is_email_receipt so this makes it explicit.
$values = array_key_exists('is_email_receipt', $values) ? ['is_email_receipt' => $values['is_email_receipt']] : [];
}

if ($recurContrib && $recurringContributionID) {
//CRM-13273 - is_email_receipt setting on recurring contribution should take precedence over contribution page setting
// but CRM-16124 if $input['is_email_receipt'] is set then that should not be overridden.
// dev/core#1245 this maybe not the desired effect because the default value for is_email_receipt is set to 0 rather than 1 in
// Instance that had the table added via an upgrade in 4.1
// see also https://github.com/civicrm/civicrm-svn/commit/7f39befd60bc735408d7866b02b3ac7fff1d4eea#diff-9ad8e290180451a2d6eacbd3d1ca7966R354
// https://lab.civicrm.org/dev/core/issues/1245
$values['is_email_receipt'] = $recurContrib->is_email_receipt;
}

if ($contributionParams['contribution_status_id'] === $completedContributionStatusID) {
self::updateMembershipBasedOnCompletionOfContribution(
$contribution,
Expand Down Expand Up @@ -4568,13 +4584,7 @@ public static function completeOrder($input, &$ids, $objects, $transaction = NUL
CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID);
}

$isEmailReceipt = !array_key_exists('is_email_receipt', $values) || $values['is_email_receipt'] == 1;
if (isset($input['is_email_receipt'])) {
$isEmailReceipt = $input['is_email_receipt'];
}
// CRM-9132 legacy behaviour was that receipts were sent out in all instances. Still sending
// when array_key 'is_email_receipt doesn't exist in case some instances where is needs setting haven't been set
if ($isEmailReceipt) {
if (self::isEmailReceipt($input, $contribution->contribution_page_id, $recurringContributionID)) {
civicrm_api3('Contribution', 'sendconfirmation', [
'id' => $contribution->id,
'payment_processor_id' => $paymentProcessorId,
Expand Down

0 comments on commit 1e3f201

Please sign in to comment.