From 24274d4dda2ae66c435870e55f744c3ca1034449 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 3 Jun 2019 11:40:44 +1200 Subject: [PATCH] Switch recordAdditionalPayment fully over to api Use payment.create api from recordAdditionalPayment form as part of general consolidation --- CRM/Contribute/BAO/Contribution.php | 33 ----------------------- CRM/Contribute/Form/AdditionalPayment.php | 22 +++++++-------- CRM/Financial/BAO/Payment.php | 6 ++--- api/v3/Payment.php | 16 +++++++---- 4 files changed, 22 insertions(+), 55 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 6a81bc618587..79c595a4b6c5 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3986,39 +3986,6 @@ public static function validateFinancialType($financialTypeId, $relationName = ' 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; - return civicrm_api3('Payment', 'create', $trxnsData)['id']; - } - } - /** * @param int $targetCid * @param $activityType diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index 1c580d37fd7f..c030d96bd660 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -356,19 +356,15 @@ public function submit($submittedValues) { $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; + // 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) { diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 1683e2e31b95..37fbde40d929 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -102,7 +102,6 @@ public static function create($params) { } 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) { @@ -118,7 +117,7 @@ public static function create($params) { ); } else { - civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]); + civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id'], 'is_email_receipt' => $params['is_send_contribution_notification']]); // Get the trxn $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); $ftParams = ['id' => $trxnId['financialTrxnId']]; @@ -133,6 +132,7 @@ public static function create($params) { ] ); } + CRM_Contribute_BAO_Contribution::recordPaymentActivity($params['contribution_id'], CRM_Utils_Array::value('participant_id', $params), $params['total_amount'], $trxn->currency, $trxn->trxn_date); return $trxn; } @@ -316,8 +316,6 @@ public static function filterUntestedTemplateVariables($params) { * @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) { diff --git a/api/v3/Payment.php b/api/v3/Payment.php index bab232ab4945..158fbf73dbab 100644 --- a/api/v3/Payment.php +++ b/api/v3/Payment.php @@ -156,28 +156,34 @@ function _civicrm_api3_payment_create_spec(&$params) { $params = [ 'contribution_id' => [ 'api.required' => 1, - 'title' => 'Contribution ID', + 'title' => ts('Contribution ID'), 'type' => CRM_Utils_Type::T_INT, ], 'total_amount' => [ 'api.required' => 1, - 'title' => 'Total Payment Amount', + 'title' => ts('Total Payment Amount'), 'type' => CRM_Utils_Type::T_FLOAT, ], 'payment_processor_id' => [ - 'title' => 'Payment Processor ID', + 'title' => ts('Payment Processor ID'), 'type' => CRM_Utils_Type::T_INT, 'description' => ts('Payment processor ID - required for payment processor payments'), ], 'id' => [ - 'title' => 'Payment ID', + 'title' => ts('Payment ID'), 'type' => CRM_Utils_Type::T_INT, 'api.aliases' => ['payment_id'], ], 'trxn_date' => [ - 'title' => 'Cancel Date', + '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, + ], ]; }