Skip to content

Commit

Permalink
Merge pull request #15551 from civicrm/5.19
Browse files Browse the repository at this point in the history
5.19  to master
  • Loading branch information
eileenmcnaughton authored Oct 19, 2019
2 parents dd9e078 + 6193a75 commit 9aac918
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
16 changes: 8 additions & 8 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract

protected $fromEmailId = NULL;

protected $_fromEmails = NULL;

protected $_view = NULL;

public $_action = NULL;
Expand All @@ -94,18 +92,14 @@ public function preProcess() {
CRM_Utils_System::setTitle($title);
return;
}
$this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();

$entityType = 'contribution';
if ($this->_component == 'event') {
$entityType = 'participant';
$this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
$eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
$this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($eventId);
}
else {
$this->_contributionId = $this->_id;
$this->_fromEmails['from_email_id'] = CRM_Core_BAO_Email::getFromEmail();
}

$paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, FALSE, TRUE);
Expand Down Expand Up @@ -222,7 +216,11 @@ public function buildQuickForm() {
//add receipt for offline contribution
$this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));

$this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
if ($this->_component === 'event') {
$eventID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
}

$this->add('select', 'from_email_address', ts('Receipt From'), CRM_Financial_BAO_Payment::getValidFromEmailsForPayment($eventID));

$this->add('textarea', 'receipt_text', ts('Confirmation Message'));

Expand Down Expand Up @@ -330,8 +328,10 @@ public function postProcess() {

/**
* Process Payments.
*
* @param array $submittedValues
*
* @throws \CiviCRM_API3_Exception
*/
public function submit($submittedValues) {
$this->_params = $submittedValues;
Expand Down Expand Up @@ -375,7 +375,7 @@ public function submit($submittedValues) {
$statusMsg = ts('The payment record has been processed.');
// send email
if (!empty($paymentID) && !empty($this->_params['is_email_receipt'])) {
$sendResult = civicrm_api3('Payment', 'sendconfirmation', ['id' => $paymentID])['values'][$paymentID];
$sendResult = civicrm_api3('Payment', 'sendconfirmation', ['id' => $paymentID, 'from' => $submittedValues['from_email_address']])['values'][$paymentID];
if ($sendResult['is_sent']) {
$statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public static function sendTemplate($params) {
];
$params = array_merge($defaults, $params);

// Core#644 - handle contact ID passed as "From".
// Core#644 - handle Email ID passed as "From".
if (isset($params['from'])) {
$params['from'] = CRM_Utils_Mail::formatFromAddress($params['from']);
}
Expand Down
26 changes: 26 additions & 0 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,35 @@ public static function sendConfirmation($params) {
'toEmail' => $entities['contact']['email'],
'tplParams' => self::getConfirmationTemplateParameters($entities),
];
if (!empty($params['from']) && !empty($params['check_permissions'])) {
// Filter from against permitted emails.
$validEmails = self::getValidFromEmailsForPayment($entities['event']['id'] ?? NULL);
if (!isset($validEmails[$params['from']])) {
// Ignore unpermitted parameter.
unset($params['from']);
}
}
$sendTemplateParams['from'] = $params['from'] ?? key(CRM_Core_BAO_Email::domainEmails());
return CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
}

/**
* Get valid from emails for payment.
*
* @param int $eventID
*
* @return array
*/
public static function getValidFromEmailsForPayment($eventID = NULL) {
if ($eventID) {
$emails = CRM_Event_BAO_Event::getFromEmailIds($eventID);
}
else {
$emails['from_email_id'] = CRM_Core_BAO_Email::getFromEmail();
}
return $emails['from_email_id'];
}

/**
* Load entities related to the current payment id.
*
Expand Down
11 changes: 6 additions & 5 deletions api/v3/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,9 @@ function _civicrm_api3_payment_cancel_spec(&$params) {
*/
function civicrm_api3_payment_sendconfirmation($params) {
$allowedParams = [
'receipt_from_email',
'receipt_from_name',
'cc_receipt',
'bcc_receipt',
'receipt_text',
'from',
'id',
'check_permissions',
];
$input = array_intersect_key($params, array_flip($allowedParams));
// use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
Expand Down Expand Up @@ -307,4 +304,8 @@ function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
'title' => ts('Payment ID'),
'type' => CRM_Utils_Type::T_INT,
];
$params['from_email_address'] = [
'title' => ts('From email; an email string or the id of a valid email'),
'type' => CRM_Utils_Type::T_STRING,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function testAddPaymentUsingCreditCardForPartialyPaidContribution() {
$this->checkResults([30, 70], 2);
$mut->assertSubjects(['Payment Receipt -']);
$mut->checkMailLog([
'From: site@something.com',
'Dear Anthony,',
'Payment Details',
'Total Fees: $ 100.00',
Expand Down
7 changes: 6 additions & 1 deletion tests/phpunit/api/v3/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public function testPaymentEmailReceipt() {
$this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
$mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
$mut->checkMailLog([
'From: "FIXME" <info@EXAMPLE.ORG>',
'Dear Anthony,',
'Total Fees: $ 300.00',
'This Payment Amount: $ 50.00',
Expand All @@ -204,6 +205,7 @@ public function testPaymentEmailReceipt() {
*/
public function testPaymentEmailReceiptFullyPaid() {
$mut = new CiviMailUtils($this);
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
list($lineItems, $contribution) = $this->createParticipantWithContribution();

$params = [
Expand All @@ -212,9 +214,12 @@ public function testPaymentEmailReceiptFullyPaid() {
];
$payment = $this->callAPISuccess('payment', 'create', $params);

$this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
// Here we set the email to an invalid email & use check_permissions, domain email should be used.
$email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
$this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
$mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet', 'Registration Confirmation - Annual CiviCRM meet']);
$mut->checkMailLog([
'From: "FIXME" <info@EXAMPLE.ORG>',
'Dear Anthony,',
'A payment has been received.',
'Total Fees: $ 300.00',
Expand Down

0 comments on commit 9aac918

Please sign in to comment.