Switch Additional Payment to call Payment.send_confirmation api, strip out text
authoreileen <emcnaughton@wikimedia.org>
Wed, 20 Feb 2019 13:13:44 +0000 (02:13 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 22 Feb 2019 00:35:48 +0000 (13:35 +1300)
CRM/Contribute/Form/AdditionalPayment.php
tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php

index d7c49fa1b3fec58f5c72894ee01c3f27d19e23af..93cb1e0ee9a76e835b8635ea90bc72db39b03de6 100644 (file)
@@ -384,16 +384,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     $statusMsg = ts('The payment record has been processed.');
     // send email
     if (!empty($result) && !empty($this->_params['is_email_receipt'])) {
-      $this->_params['contact_id'] = $this->_contactId;
-      $this->_params['contribution_id'] = $this->_contributionId;
-
-      // These 2 rows are temporarily added for sequencing of adding commits. They won't be needed when we
-      // switch to Payment.send_confirmation api
-      $contact = civicrm_api3('Contact', 'getsingle', ['id' => $this->_contactId, 'return' => 'email_greeting']);
-      $this->assign('emailGreeting', $contact['email_greeting_display']);
-
-      $sendReceipt = $this->emailReceipt($this->_params);
-      if ($sendReceipt) {
+      $sendResult = civicrm_api3('Payment', 'sendconfirmation', ['id' => $result->id])['values'][$result->id];
+      if ($sendResult['is_sent']) {
         $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
       }
     }
@@ -503,88 +495,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     }
   }
 
-  /**
-   * Function to send email receipt.
-   *
-   * @param array $params
-   *
-   * @return bool
-   */
-  public function emailReceipt(&$params) {
-    $templateEngine = CRM_Core_Smarty::singleton();
-    // email receipt sending
-    list($contributorDisplayName, $contributorEmail, $doNotMail) = CRM_Contact_BAO_Contact::getContactDetails($params['contact_id']);
-    if (!$contributorEmail || $doNotMail) {
-      return FALSE;
-    }
-    $templateEngine->assign('contactDisplayName', $contributorDisplayName);
-    // send message template
-    if ($this->_component == 'event') {
-
-      // fetch event information from participant ID using API
-      $eventId = civicrm_api3('Participant', 'getvalue', array(
-        'return' => "event_id",
-        'id' => $this->_id,
-      ));
-      $event = civicrm_api3('Event', 'getsingle', array('id' => $eventId));
-
-      $templateEngine->assign('event', $event);
-      $templateEngine->assign('isShowLocation', $event['is_show_location']);
-      if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
-        $locationParams = array(
-          'entity_id' => $eventId,
-          'entity_table' => 'civicrm_event',
-        );
-        $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
-        $templateEngine->assign('location', $location);
-      }
-    }
-
-    // assign payment info here
-    $paymentConfig['confirm_email_text'] = CRM_Utils_Array::value('confirm_email_text', $params);
-    $templateEngine->assign('paymentConfig', $paymentConfig);
-
-    $templateEngine->assign('totalAmount', $this->_amtTotal);
-
-    $isRefund = ($this->_paymentType == 'refund') ? TRUE : FALSE;
-    $templateEngine->assign('isRefund', $isRefund);
-    if ($isRefund) {
-      $templateEngine->assign('totalPaid', $this->_amtPaid);
-      $templateEngine->assign('refundAmount', $params['total_amount']);
-    }
-    else {
-      $balance = $this->_amtTotal - ($this->_amtPaid + $params['total_amount']);
-      $paymentsComplete = ($balance == 0) ? 1 : 0;
-      $templateEngine->assign('amountOwed', $balance);
-      $templateEngine->assign('paymentAmount', $params['total_amount']);
-      $templateEngine->assign('paymentsComplete', $paymentsComplete);
-    }
-
-    // assign trxn details
-    $templateEngine->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $params));
-    $templateEngine->assign('receive_date', CRM_Utils_Array::value('trxn_date', $params));
-    $templateEngine->assign('paidBy', CRM_Core_PseudoConstant::getLabel(
-      'CRM_Contribute_BAO_Contribution',
-      'payment_instrument_id',
-      $params['payment_instrument_id']
-    ));
-    $templateEngine->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
-
-    $sendTemplateParams = array(
-      'groupName' => 'msg_tpl_workflow_contribution',
-      'valueName' => 'payment_or_refund_notification',
-      'contactId' => $params['contact_id'],
-      'PDFFilename' => ts('notification') . '.pdf',
-    );
-
-    $sendTemplateParams['from'] = $params['from_email_address'];
-    $sendTemplateParams['toName'] = $contributorDisplayName;
-    $sendTemplateParams['toEmail'] = $contributorEmail;
-
-    list($mailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
-    return $mailSent;
-  }
-
   /**
    * Wrapper for unit testing the post process submit function.
    *
index a23b090250ed8c8d39ce7aa693ba36bb8176edd1..362dc43875de297db32011cffb01995f988c4887 100644 (file)
@@ -122,6 +122,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
    */
   public function tearDown() {
     $this->quickCleanUpFinancialEntities();
+    CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_spool ORDER BY id DESC');
     parent::tearDown();
   }
 
@@ -150,6 +151,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     ]);
 
     $mut->stop();
+    $mut->clearMessages();
   }
 
   /**
@@ -185,6 +187,14 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
   public function testMultiplePaymentForPartiallyPaidContributionWithOneCreditCardPayment() {
     $mut = new CiviMailUtils($this, TRUE);
     $this->createContribution('Partially paid');
+    // In general when there is tpl leakage we try to fix. At the moment, however,
+    // the tpl leakage on credit card related things is kind of 'by-design' - or
+    // at least we haven't found a way to replace the way in with Payment.send_confirmation
+    // picks them up from the form process so we will just clear templates here to stop leakage
+    // from previous tests causing a fail.
+    // The reason this is hard to fix is that we save a billing address per contribution not
+    // per payment so it's a problem with the data model
+    CRM_Core_Smarty::singleton()->clearTemplateVars();
 
     // pay additional amount
     $this->submitPayment(50, NULL, TRUE);
@@ -195,7 +205,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     $this->submitPayment(20, 'live');
     $this->checkResults(array(30, 50, 20), 3);
     $mut->assertSubjects(array('Payment Receipt -'));
-    $mut->checkMailLog(array(
+    $mut->checkMailLog([
       'Dear Anthony,',
       'A payment has been received',
       'Total Fees: $ 100.00',
@@ -203,8 +213,13 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
       'Balance Owed: $ 20.00 ',
       'Paid By: Check',
       'Check Number: check-12345',
-    ));
+    ],
+    [
+      'Billing Name and Address',
+      'Visa',
+    ]);
     $mut->stop();
+    $mut->clearMessages();
   }
 
   /**