From: Eileen Date: Tue, 12 Apr 2016 13:13:13 +0000 (+0000) Subject: CRM-18303 call sendConfirmation api from completeOrder to make template params more... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ec7e395452267920d9cd37c27bac5505350ba7e1;p=civicrm-core.git CRM-18303 call sendConfirmation api from completeOrder to make template params more reliable, api reloads the contribution --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 3459480dd8..ee66e967b1 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4393,9 +4393,6 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) $values['amount'] = $recurContrib->amount; $values['financial_type_id'] = $objects['contributionType']->id; $values['title'] = $source = ts('Offline Recurring Contribution'); - $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); - $values['receipt_from_name'] = $domainValues[0]; - $values['receipt_from_email'] = $domainValues[1]; } if ($recurContrib && $recurContrib->id && !isset($input['is_email_receipt'])) { @@ -4573,7 +4570,10 @@ LIMIT 1;"; if (!array_key_exists('is_email_receipt', $values) || $values['is_email_receipt'] == 1 ) { - self::sendMail($input, $ids, $objects['contribution'], $values, $recur, FALSE); + civicrm_api3('Contribution', 'sendconfirmation', array( + 'id' => $contribution->id, + 'payment_processor_id' => $paymentProcessorId, + )); CRM_Core_Error::debug_log_message("Receipt sent"); } @@ -4596,7 +4596,7 @@ LIMIT 1;"; * Incoming data from Payment processor. * @param array $ids * Related object IDs. - * @param CRM_Contribute_BAO_Contribution $contribution + * @param int $contributionID * @param array $values * Values related to objects that have already been loaded. * @param bool $recur @@ -4606,10 +4606,19 @@ LIMIT 1;"; * is because the function is also used to generate pdfs * * @return array + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public static function sendMail(&$input, &$ids, $contribution, &$values, $recur = FALSE, $returnMessageText = FALSE) { + public static function sendMail(&$input, &$ids, $contributionID, &$values, $recur = FALSE, + $returnMessageText = FALSE) { $input['is_recur'] = $recur; - $input['receipt_date'] = $contribution->receipt_date; + + $contribution = new CRM_Contribute_BAO_Contribution(); + $contribution->id = $contributionID; + if (!$contribution->find(TRUE)) { + throw new CRM_Core_Exception('Contribution does not exist'); + } + $contribution->loadRelatedObjects($input, $ids, TRUE); // set receipt from e-mail and name in value if (!$returnMessageText) { $session = CRM_Core_Session::singleton(); @@ -4623,6 +4632,7 @@ LIMIT 1;"; // Contribution ID should really always be set. But ? if (!$returnMessageText && (!isset($input['receipt_update']) || $input['receipt_update'])) { civicrm_api3('Contribution', 'create', array('receipt_date' => 'now', 'id' => $contribution->id)); + $values['receipt_date'] = date('Y-m-d H:i:s'); } return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText); } diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 990eefb846..50d9722610 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -381,6 +381,7 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio 'useForMember' => $values['useForMember'], 'membership_assign' => $values['membership_assign'], 'amount' => $values['amount'], + 'receipt_date' => !$values['receipt_date'] ?: date('YmdHis', strtotime($values['receipt_date'])), ); if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) { @@ -445,6 +446,10 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio ); } + if (empty($values['receipt_from_name']) && empty($values['receipt_from_name'])) { + list($values['receipt_from_name'], $values['receipt_from_email']) = CRM_Core_BAO_Domain::getNameAndEmail(); + } + if ($values['is_email_receipt']) { $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>'; $sendTemplateParams['toName'] = $displayName; diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 6450cf416e..c01b6679cd 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -2302,7 +2302,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr * @param array $result * @param int $contributionID * - * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Exception */ protected function completeTransaction($result, $contributionID) { if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index 19cd0974b0..ffb8cec42c 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -187,7 +187,7 @@ AND {$this->_componentClause}"; $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date); $values = array(); - $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution'], $values, FALSE, + $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, FALSE, $elements['createPdf']); if ($mail['html']) { diff --git a/CRM/Core/Payment/BaseIPN.php b/CRM/Core/Payment/BaseIPN.php index 150c897e64..0c8f9f70a4 100644 --- a/CRM/Core/Payment/BaseIPN.php +++ b/CRM/Core/Payment/BaseIPN.php @@ -491,7 +491,7 @@ class CRM_Core_Payment_BaseIPN { * @return array */ public function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) { - return CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution'], $values, $recur, + return CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, $recur, $returnMessageText); } diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index a95bf6a0e4..f7903d6d35 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -386,14 +386,22 @@ function civicrm_api3_contribution_transact($params) { * @throws Exception */ function civicrm_api3_contribution_sendconfirmation($params) { - $contribution = new CRM_Contribute_BAO_Contribution(); - $contribution->id = $params['id']; - if (!$contribution->find(TRUE)) { - throw new Exception('Contribution does not exist'); + $input = $ids = $values = array(); + $passThroughParams = array( + 'receipt_from_email', + 'receipt_from_name', + 'receipt_update', + 'cc_receipt', + 'bcc_receipt', + 'receipt_text', + 'payment_processor_id', + ); + foreach ($passThroughParams as $key) { + if (isset($params[$key])) { + $input[$key] = $params[$key]; + } } - $input = $ids = $cvalues = array('receipt_from_email' => $params['receipt_from_email']); - $contribution->loadRelatedObjects($input, $ids, TRUE); - $contribution->composeMessageArray($input, $ids, $cvalues, FALSE, FALSE); + CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $params['id'], $values); } /** @@ -407,30 +415,38 @@ function civicrm_api3_contribution_sendconfirmation($params) { function _civicrm_api3_contribution_sendconfirmation_spec(&$params) { $params['id'] = array( 'api.required' => 1, - 'title' => 'Contribution ID', + 'title' => ts('Contribution ID'), 'type' => CRM_Utils_Type::T_INT, ); $params['receipt_from_email'] = array( - 'api.required' => 1, - 'title' => 'From Email address (string) required until someone provides a patch :-)', + 'title' => ts('From Email address (string)'), 'type' => CRM_Utils_Type::T_STRING, ); $params['receipt_from_name'] = array( - 'title' => 'From Name (string)', + 'title' => ts('From Name (string)'), 'type' => CRM_Utils_Type::T_STRING, ); $params['cc_receipt'] = array( - 'title' => 'CC Email address (string)', + 'title' => ts('CC Email address (string)'), 'type' => CRM_Utils_Type::T_STRING, ); $params['bcc_receipt'] = array( - 'title' => 'BCC Email address (string)', + 'title' => ts('BCC Email address (string)'), 'type' => CRM_Utils_Type::T_STRING, ); $params['receipt_text'] = array( - 'title' => 'Message (string)', + 'title' => ts('Message (string)'), 'type' => CRM_Utils_Type::T_STRING, ); + $params['receipt_update'] = array( + 'title' => ts('Update the Receipt Date'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'api.default' => TRUE, + ); + $params['payment_processor_id'] = array( + 'title' => ts('Payment processor Id (avoids mis-guesses)'), + 'type' => CRM_Utils_Type::T_INT, + ); } /** diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 7f9fdbf078..5f4000be55 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -67,6 +67,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ protected $_eventID; + /** + * @var CiviMailUtils + */ + protected $mut; + /** * Setup function. */ @@ -1572,6 +1577,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ public function testCompleteTransaction() { $mut = new CiviMailUtils($this, TRUE); + $this->swapMessageTemplateForTestTemplate(); $this->createLoggedInUser(); $params = array_merge($this->_params, array('contribution_status_id' => 2)); $contribution = $this->callAPISuccess('contribution', 'create', $params); @@ -1583,11 +1589,18 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals('Completed', $contribution['contribution_status']); $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date']))); $mut->checkMailLog(array( - 'Receipt - Contribution', - 'Please print this confirmation for your records.', - 'May 11th, 2012', + 'email:::anthony_anderson@civicrm.org', + 'is_monetary:::1', + 'amount:::100.00', + 'currency:::USD', + 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])), + 'receipt_date:::' . date('Ymd'), + 'contributeMode:::notify', + 'title:::Contribution', + 'displayName:::Mr. Anthony Anderson II', )); $mut->stop(); + $this->revertTemplateToReservedTemplate(); } /** @@ -1999,17 +2012,14 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ public function testCompleteTransactionWithTestTemplate() { $this->swapMessageTemplateForTestTemplate(); - $mut = new CiviMailUtils($this, TRUE); - $this->createLoggedInUser(); - $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now')); - $contribution = $this->callAPISuccess('contribution', 'create', $params); + $contribution = $this->setUpForCompleteTransaction(); $this->callAPISuccess('contribution', 'completetransaction', array( 'id' => $contribution['id'], 'trxn_date' => date('2011-04-09'), 'trxn_id' => 'kazam', )); $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date')); - $mut->checkMailLog(array( + $this->mut->checkMailLog(array( 'email:::anthony_anderson@civicrm.org', 'is_monetary:::1', 'amount:::100.00', @@ -2020,15 +2030,40 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'title:::Contribution', 'displayName:::Mr. Anthony Anderson II', 'trxn_id:::kazam', - 'contactID:::' . $params['contact_id'], + 'contactID:::' . $this->_params['contact_id'], 'contributionID:::' . $contribution['id'], 'financialTypeId:::1', 'financialTypeName:::Donation', )); - $mut->stop(); + $this->mut->stop(); $this->revertTemplateToReservedTemplate(); } + /** + * Complete the transaction using the template with all the possible. + */ + public function testCompleteTransactionContributionPageFromAddress() { + $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array( + 'receipt_from_name' => 'Mickey Mouse', + 'receipt_from_email' => 'mickey@mouse.com', + 'title' => "Test Contribution Page", + 'financial_type_id' => 1, + 'currency' => 'NZD', + 'goal_amount' => 50, + 'is_pay_later' => 1, + 'is_monetary' => TRUE, + 'is_email_receipt' => TRUE, + )); + $this->_params['contribution_page_id'] = $contributionPage['id']; + $contribution = $this->setUpForCompleteTransaction(); + $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'])); + $this->mut->checkMailLog(array( + 'mickey@mouse.com', + 'Mickey Mouse <', + )); + $this->mut->stop(); + } + /** * Test completing first transaction in a recurring series. * @@ -2730,4 +2765,17 @@ class api_v3_ContributionTest extends CiviUnitTestCase { return $originalContribution; } + /** + * Common set up routine. + * + * @return array + */ + protected function setUpForCompleteTransaction() { + $this->mut = new CiviMailUtils($this, TRUE); + $this->createLoggedInUser(); + $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now')); + $contribution = $this->callAPISuccess('contribution', 'create', $params); + return $contribution; + } + }