From 49cba3ad843fced81edf83130932ad8c420a01bd Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 1 Nov 2019 17:28:45 +1300 Subject: [PATCH] Fix BillingName to be assigned in email receipts IF available Previously it was assigned at the form layer, now we assign it in the email sending routine, depending on whether the contribution has address_id & there is a value assigned --- CRM/Contribute/BAO/Contribution.php | 21 ++++++++++----------- api/v3/PaymentProcessor.php | 1 + tests/phpunit/api/v3/ContributionTest.php | 22 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 8aafcee8eea5..e894d15208ef 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3054,23 +3054,20 @@ public function composeMessageArray(&$input, &$ids, &$values, $returnMessageText */ public function _gatherMessageValues($input, &$values, $ids = []) { // set display address of contributor + $values['billingName'] = ''; if ($this->address_id) { - $addressParams = ['id' => $this->address_id]; - $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id'); - $addressDetails = array_values($addressDetails); + $addressDetails = CRM_Core_BAO_Address::getValues(['id' => $this->address_id], FALSE, 'id'); + $addressDetails = reset($addressDetails); + $values['billingName'] = $addressDetails['name'] ?? ''; } // Else we assign the billing address of the contribution contact. else { - $addressParams = ['contact_id' => $this->contact_id, 'is_billing' => 1]; - $addressDetails = (array) CRM_Core_BAO_Address::getValues($addressParams); - $addressDetails = array_values($addressDetails); + $addressDetails = (array) CRM_Core_BAO_Address::getValues(['contact_id' => $this->contact_id, 'is_billing' => 1]); + $addressDetails = reset($addressDetails); } + $values['address'] = $addressDetails['display'] ?? ''; - if (!empty($addressDetails[0]['display'])) { - $values['address'] = $addressDetails[0]['display']; - } - - if ($this->_component == 'contribute') { + if ($this->_component === 'contribute') { //get soft contributions $softContributions = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($this->id, TRUE); if (!empty($softContributions)) { @@ -3187,6 +3184,7 @@ public function _assignMessageVariablesToTemplate(&$values, $input, $returnMessa $template->assign('first_name', $this->_relatedObjects['contact']->first_name); $template->assign('last_name', $this->_relatedObjects['contact']->last_name); $template->assign('displayName', $this->_relatedObjects['contact']->display_name); + $template->assign('billingName', $values['billingName']); // For some unit tests contribution cannot contain paymentProcessor information $billingMode = empty($this->_relatedObjects['paymentProcessor']) ? CRM_Core_Payment::BILLING_MODE_NOTIFY : $this->_relatedObjects['paymentProcessor']['billing_mode']; @@ -4668,6 +4666,7 @@ public static function completeOrder(&$input, &$ids, $objects, $transaction, $re * @return array * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception + * @throws \Exception */ public static function sendMail(&$input, &$ids, $contributionID, &$values, $returnMessageText = FALSE) { diff --git a/api/v3/PaymentProcessor.php b/api/v3/PaymentProcessor.php index 9b3833d622e5..8373cc7ec7c8 100644 --- a/api/v3/PaymentProcessor.php +++ b/api/v3/PaymentProcessor.php @@ -173,6 +173,7 @@ function _civicrm_api3_payment_processor_pay_spec(&$params) { 'api.required' => TRUE, 'title' => ts('Contribution ID'), 'type' => CRM_Utils_Type::T_INT, + 'api.aliases' => ['order_id'], ]; $params['contact_id'] = [ 'title' => ts('Contact ID'), diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 5e9e5f820d36..0a3b198d2131 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -3346,12 +3346,25 @@ public function setUpPendingContribution($priceFieldValueID, $contriParams = []) * Test sending a mail via the API. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function testSendMail() { $mut = new CiviMailUtils($this, TRUE); - $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); + $orderParams = $this->_params; + $orderParams['contribution_status_id'] = 'Pending'; + $orderParams['api.PaymentProcessor.pay'] = [ + 'payment_processor_id' => $this->paymentProcessorID, + 'credit_card_type' => 'Visa', + 'credit_card_number' => 41111111111111, + 'amount' => 5, + ]; + + $order = $this->callAPISuccess('Order', 'create', $orderParams); + $this->callAPISuccess('Payment', 'create', ['total_amount' => 5, 'is_send_notification' => 0, 'order_id' => $order['id']]); + $address = $this->callAPISuccess('Address', 'create', ['contribution_id' => $order['id'], 'name' => 'bob', 'contact_id' => 1, 'street_address' => 'blah']); + $this->callAPISuccess('Contribution', 'create', ['id' => $order['id'], 'address_id' => $address['id']]); $this->callAPISuccess('contribution', 'sendconfirmation', [ - 'id' => $contribution['id'], + 'id' => $order['id'], 'receipt_from_email' => 'api@civicrm.org', ]); $mut->checkMailLog([ @@ -3361,8 +3374,11 @@ public function testSendMail() { 'Event', ]); - $this->checkCreditCardDetails($mut, $contribution['id']); + $this->checkCreditCardDetails($mut, $order['id']); $mut->stop(); + $tplVars = CRM_Core_Smarty::singleton()->get_template_vars(); + $this->assertEquals('bob', $tplVars['billingName']); + $this->assertEquals("bob\nblah\n", $tplVars['address']); } /**