Switch to calling Payment.create api when processing a refund from AdditionalPayment...
authoreileen <emcnaughton@wikimedia.org>
Fri, 24 May 2019 01:46:52 +0000 (13:46 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 24 May 2019 03:52:22 +0000 (15:52 +1200)
This is in conjunction with a series of changes consolidating & adding testing for creating payments with the goal
being that the logic is all in the Payment.create api/BAO & none of the logic remains in the CRM_Contribute_Form_AdditionalPayment form

This change completes switching the refund action over. There is logic in there to update the Participant status to
Registered when a refund is processed - seemingly regardless of what it's original status is or the appropriate status
afterwards.

I tested through the UI and if you create a participant record & then change the line items, thus reducing the cost
and changing the contribution status to 'Pending Refund' the participant status remains 'Completed' so I think
this was not working / not required already & I simply dropped it

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/AdditionalPayment.php
CRM/Financial/BAO/Payment.php

index ad3e88caa426b2b2ceabe610b0fe01f10de25cc1..596c405443736dc24707e80e784f4823fd3e7151 100644 (file)
@@ -912,7 +912,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  protected static function recordPaymentActivity($contributionId, $participantId, $totalAmount, $currency, $trxnDate) {
+  public static function recordPaymentActivity($contributionId, $participantId, $totalAmount, $currency, $trxnDate) {
     $activityType = ($totalAmount < 0) ? 'Refund' : 'Payment';
 
     if ($participantId) {
@@ -3974,34 +3974,25 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
    * @param int $participantId
    * @param bool $updateStatus
    *
-   * @return null|object
+   * @return int
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public static function recordAdditionalPayment($contributionId, $trxnsData, $paymentType = 'owed', $participantId = NULL, $updateStatus = TRUE) {
 
     if ($paymentType == 'owed') {
       $financialTrxn = CRM_Financial_BAO_Payment::recordPayment($contributionId, $trxnsData, $participantId);
+      if (!empty($financialTrxn)) {
+        self::recordPaymentActivity($contributionId, $participantId, $financialTrxn->total_amount, $financialTrxn->currency, $financialTrxn->trxn_date);
+        return $financialTrxn->id;
+      }
     }
     elseif ($paymentType == 'refund') {
       $trxnsData['total_amount'] = -$trxnsData['total_amount'];
-      $financialTrxn = CRM_Financial_BAO_Payment::recordRefundPayment($contributionId, $trxnsData, $updateStatus);
-      if ($participantId) {
-        // update participant status
-        // @todo this doesn't make sense...
-        $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
-        $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
-        foreach ($ids as $val) {
-          $participantUpdate['id'] = $val;
-          $participantUpdate['status_id'] = array_search('Registered', $participantStatuses);
-          CRM_Event_BAO_Participant::add($participantUpdate);
-        }
-      }
+      $trxnsData['participant_id'] = $participantId;
+      return civicrm_api3('Payment', 'create', $trxnsData)['id'];
     }
-
-    if (!empty($financialTrxn)) {
-      self::recordPaymentActivity($contributionId, $participantId, $financialTrxn->total_amount, $financialTrxn->currency, $financialTrxn->trxn_date);
-      return $financialTrxn;
-    }
-
   }
 
   /**
index 1c37915b58032459827fece99db4e4a9dd989da2..1c580d37fd7fe0fe80deb889454cf32b474cead9 100644 (file)
@@ -362,10 +362,12 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       'id' => $this->_contributionId,
     ]);
     $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
-    $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $this->_params, $this->_paymentType, $participantId);
+    $paymentID = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $this->_params, $this->_paymentType, $participantId);
     // Fetch the contribution & do proportional line item assignment
     $params = ['id' => $this->_contributionId];
     $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
+    // @todo - this line needs to be moved to the Payment.create api - it's not form layer appropriate.
+    // testing required.
     CRM_Contribute_BAO_Contribution::addPayments([$contribution], $contributionStatusId);
     if ($this->_contributionId && CRM_Core_Permission::access('CiviMember')) {
       $membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', ['contribution_id' => $this->_contributionId]);
@@ -382,8 +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'])) {
-      $sendResult = civicrm_api3('Payment', 'sendconfirmation', ['id' => $result->id])['values'][$result->id];
+    if (!empty($paymentID) && !empty($this->_params['is_email_receipt'])) {
+      $sendResult = civicrm_api3('Payment', 'sendconfirmation', ['id' => $paymentID])['values'][$paymentID];
       if ($sendResult['is_sent']) {
         $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
       }
index 664c193e0cc4f8d7d8533f540730384015f15dad..1683e2e31b957e3b63218fea17a314423cd1a05a 100644 (file)
@@ -102,6 +102,7 @@ class CRM_Financial_BAO_Payment {
     }
     elseif ($params['total_amount'] < 0) {
       $trxn = self::recordRefundPayment($params['contribution_id'], $params, FALSE);
+      CRM_Contribute_BAO_Contribution::recordPaymentActivity($params['contribution_id'], CRM_Utils_Array::value('participant_id', $params), $params['total_amount'], $trxn->currency, $trxn->trxn_date);
     }
 
     if ($isPaymentCompletesContribution) {
@@ -319,7 +320,7 @@ class CRM_Financial_BAO_Payment {
    *
    * @return CRM_Financial_DAO_FinancialTrxn
    */
-  public static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
+  protected static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
     list($contributionDAO, $params) = self::getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId);
 
     $params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnData, CRM_Utils_Array::value('payment_instrument_id', $params));