Switch recordAdditionalPayment fully over to api
authoreileen <emcnaughton@wikimedia.org>
Sun, 2 Jun 2019 23:40:44 +0000 (11:40 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 19 Aug 2019 20:01:44 +0000 (08:01 +1200)
Use payment.create api from recordAdditionalPayment form as
part of general consolidation

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

index 9dcc2224c29dfbb08ca57a731c078bd97b6705b4..a1de71b483e0f96ef3eeb3c1977f9d3af9c50e4d 100644 (file)
@@ -3901,40 +3901,6 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
     return FALSE;
   }
 
-  /**
-   * Function to record additional payment for partial and refund contributions.
-   *
-   * @param int $contributionId
-   *   is the invoice contribution id (got created after processing participant payment).
-   * @param array $trxnsData
-   *   to take user provided input of transaction details.
-   * @param string $paymentType
-   *   'owed' for purpose of recording partial payments, 'refund' for purpose of recording refund payments.
-   * @param int $participantId
-   * @param bool $updateStatus
-   *
-   * @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'];
-      $trxnsData['participant_id'] = $participantId;
-      $trxnsData['contribution_id'] = $contributionId;
-      return civicrm_api3('Payment', 'create', $trxnsData)['id'];
-    }
-  }
-
   /**
    * @param int $targetCid
    * @param $activityType
index f34854f597859a9c326c1dc911fb96ce9d88b2da..fdb7c478f3a9c81eb86813aa1df0568dee07aaf0 100644 (file)
@@ -73,6 +73,11 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
 
   public $_action = NULL;
 
+  /**
+   * Pre process form.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function preProcess() {
 
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
@@ -118,11 +123,11 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       $this->_paymentType = 'owed';
     }
     else {
-      CRM_Core_Error::fatal(ts('No payment information found for this record'));
+      throw new CRM_Core_Exception(ts('No payment information found for this record'));
     }
 
     if (!empty($this->_mode) && $this->_paymentType == 'refund') {
-      CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use'));
+      throw new CRM_Core_Exception(ts('Credit card payment is not for Refund payments use'));
     }
 
     list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
@@ -139,6 +144,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
    * Is this function being called from a datatable selector.
    *
    * If so we don't want to show the buttons.
+   *
+   * @throws \CRM_Core_Exception
    */
   protected function isBeingCalledFromSelectorContext() {
     return CRM_Utils_Request::retrieve('selector', 'Positive');
@@ -356,19 +363,16 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       $this->processCreditCard();
     }
 
-    $defaults = [];
-    $contribution = civicrm_api3('Contribution', 'getsingle', [
-      'return' => ["contribution_status_id"],
-      'id' => $this->_contributionId,
-    ]);
-    $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
-    $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);
+    $trxnsData = $this->_params;
+    if ($this->_paymentType == 'refund') {
+      $trxnsData['total_amount'] = -$trxnsData['total_amount'];
+    }
+    $trxnsData['participant_id'] = $participantId;
+    $trxnsData['contribution_id'] = $this->_contributionId;
+    // From the
+    $trxnsData['is_send_contribution_notification'] = FALSE;
+    $paymentID = civicrm_api3('Payment', 'create', $trxnsData)['id'];
+
     if ($this->_contributionId && CRM_Core_Permission::access('CiviMember')) {
       $membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', ['contribution_id' => $this->_contributionId]);
       if ($membershipPaymentCount) {
index 630f95915bc55b36e21e3469a80aec2dc53f6da1..5b641fec44529c9f0c6a867d8f4f0adb052c5ca8 100644 (file)
@@ -162,6 +162,7 @@ class CRM_Financial_BAO_Payment {
         civicrm_api3('Contribution', 'completetransaction', [
           'id' => $contribution['id'],
           'is_post_payment_create' => TRUE,
+          'is_email_receipt' => $params['is_send_contribution_notification'],
         ]);
         // Get the trxn
         $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
@@ -360,8 +361,6 @@ class CRM_Financial_BAO_Payment {
    * @param $updateStatus
    *   - deprecate this param
    *
-   * @todo  - make this protected once recordAdditionalPayment no longer calls it.
-   *
    * @return CRM_Financial_DAO_FinancialTrxn
    */
   protected static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
index 6114835fa267a0b2b7a522817a5bb0796cd4b387..4ac10c070fc8714e0f2984ee44ba7ae22b6e8fd8 100644 (file)
@@ -178,6 +178,12 @@ function _civicrm_api3_payment_create_spec(&$params) {
       'title' => ts('Cancel Date'),
       'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
     ],
+    'is_send_contribution_notification' => [
+      'title' => ts('Send out notifications based on contribution status change?'),
+      'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'),
+      'type' => CRM_Utils_Type::T_BOOLEAN,
+      'api.default' => TRUE,
+    ],
   ];
 }