Fix BillingName to be assigned in email receipts IF available
authoreileen <emcnaughton@wikimedia.org>
Fri, 1 Nov 2019 04:28:45 +0000 (17:28 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 1 Nov 2019 06:30:47 +0000 (19:30 +1300)
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
api/v3/PaymentProcessor.php
tests/phpunit/api/v3/ContributionTest.php

index 8aafcee8eea5de98c5d5148e556ba3060c7381a4..e894d15208ef047ba1d9fb64f360d681f4e3d78f 100644 (file)
@@ -3054,23 +3054,20 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
    */
   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 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
     $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 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
    * @return array
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
+   * @throws \Exception
    */
   public static function sendMail(&$input, &$ids, $contributionID, &$values,
                                   $returnMessageText = FALSE) {
index 9b3833d622e5edb471e6351e95528e29d1443282..8373cc7ec7c8174995f81830715f85980aba15a9 100644 (file)
@@ -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'),
index 5e9e5f820d36b308623a0fe7de563fd7fd593b53..0a3b198d213185d70c1e4133620615cc42cf2bc7 100644 (file)
@@ -3346,12 +3346,25 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * 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 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       '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']);
   }
 
   /**